Displaying Specific Terms & Conditions Based on Grand Total in Magento
As e-commerce stores grow, it becomes necessary to manage multiple sets of terms and conditions for different customers or transaction amounts. In this guide, we’ll demonstrate how to display specific terms and conditions based on the grand total amount in your Magento store.
Identifying the Problem
When creating multiple terms and conditions in the Magento admin panel (Sales > Terms & Conditions), you might face challenges displaying only one set depending on factors like the checkout’s grand total. Additionally, inserting dynamic values into the conditions text can be tricky.
Solution: Using a Custom Observer for Grand Total
To achieve this functionality, we’ll leverage an observer that triggers when the quote is submitted. This event allows us to modify the terms and conditions based on the quote’s grand total.
Here’s a high-level overview of the steps involved:
- Create a custom module.
- Define a dependency injection configuration for the event listener.
- Implement the event listener method.
Creating the Custom Module
First, create your custom module directory structure under app/code. For this example, let’s name it LaravelCompany_TermsAndConditions:
app/code
âââ LaravelCompany
âââ TermsAndConditions
âââ etc
â âââ configuration.xml
âââ registration.php
âââ etc
âââ module.xml
Configuring the Event Listener
Create a configuration file at etc/configuration.xml to set up your observer:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<event>
<observer name="catalog_product_get_final_price" instance="LaravelCompany\TermsAndConditions\Observer\DisplayDynamicTnc">
<type>sales_quote_submit_all_after</type>
<priority>100</priority>
</observer>
</event>
</default>
</config>
Implementing the Event Listener
Create a file named DisplayDynamicTnc.php under Observer:
namespace LaravelCompany\TermsAndConditions\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Quote;
class DisplayDynamicTnc implements ObserverInterface
{
public function execute(Observer $observer)
{
/** @var Quote $quote */
$quote = $observer->getEvent()->getQuote();
// Get grand total amount
$grandTotal = $quote->getGrandTotal();
if ($grandTotal < 2000) {
// Display first terms and conditions (ID: 1)
$conditionIdToDisplay = 1;
} else {
// Display second terms and conditions (ID: 2)
$conditionIdToDisplay = 2;
}
// TODO: Set the condition ID to the checkout page
return $this;
}
}
Inserting Dynamic Values into Terms & Conditions Text
To insert dynamic values like the grand total amount into your terms and conditions text, you can use Magento’s template variables. For example:
{{var grand_total}}
Add this variable to your terms and conditions text in the admin panel.
Remember that displaying specific terms and conditions based on custom criteria requires careful integration with your checkout process and front-end layout files.
Conclusion
By creating a custom module and leveraging Magento’s event system, you can easily display different sets of terms and conditions depending on the grand total amount. This approach allows for dynamic content management without modifying core files.
For more advanced use cases or assistance in implementing this functionality, feel free to reach out to our team at LaravelCompany.com.