Dynamic conditions in Magento

I have created two Payment Terms using Admin -> Sales -> Terms & Conditions

. How can I display only one of them based on the total order amount? For example, if the total is less than $ 2,000, then display the first Terms and Conditions. And if the Grand total is more than $ 2,000, then display the second. And the second question: how can I insert the amount into the text of the Terms and Conditions?


source to share

2 answers

The code that filters out agreements is in the Mage_Checkout_Block_Agreements class:

public function getAgreements()
    if (!$this->hasAgreements()) {
        if (!Mage::getStoreConfigFlag('checkout/options/enable_agreements')) {
            $agreements = array();
        } else {
            $agreements = Mage::getModel('checkout/agreement')->getCollection()
                ->addFieldToFilter('is_active', 1);
    return $this->getData('agreements');


This returns a collection of active agreements within the allowed storage area. The template file calls this method (checkout / onepage / agreement.phtml):

<?php foreach ($this->getAgreements() as $_a): ?>
        <div class="agreement-content"<?php echo ($_a->getContentHeight() ? ' style="height:' . $_a->getContentHeight() . '"' : '')?>>
            <?php if ($_a->getIsHtml()):?>
                <?php echo $_a->getContent() ?>
            <?php else:?>
                <?php echo nl2br($this->htmlEscape($_a->getContent())) ?>
            <?php endif; ?>
        <p class="agree">
            <input type="checkbox" id="agreement-<?php echo $_a->getId()?>" name="agreement[<?php echo $_a->getId()?>]" value="1" title="<?php echo $this->htmlEscape($_a->getCheckboxText()) ?>" class="checkbox" /><label for="agreement-<?php echo $_a->getId()?>"><?php echo $_a->getIsHtml() ? $_a->getCheckboxText() : $this->htmlEscape($_a->getCheckboxText()) ?></label>
<?php endforeach ?>


The easiest way to accomplish what you want is probably to modify the template file. So I could imagine logic like this:

$total = Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal();
$agreement_name = ($total < 2000) ? 'lt2000' : 'gt2000';
$agreements = Mage::getModel('checkout/agreement')->getCollection()
    ->addFieldToFilter('is_active', 1)
    ->addFieldToFilter('name', $agreement_name);
<?php foreach ($agreements as $_a): ?>


In this example, you should have two naming conditions "lt2000" and "gt2000" (or more, with the same names but assigned to different areas of the repository / website). However, this code ignores additional conventions that may appear on the checkout page.

To answer your second question, this is a good reference on how to add custom variables to static blocks. In your case, you just need to change the name and value of the variable to whatever you want and pass the $ _a-> getContent () method using the filter method. Then you can specify this variable in the text of the terms and conditions.

So, for example, referencing a link, in your template code, you can add:

$custom_vars = array('total'=>$total);
$filter = Mage::getModel('core/email_template_filter');
<?php echo $filter->filter($_a->getContent()) ?>
<?php echo nl2br($this->htmlEscape($filter->filter($_a->getContent()))) ?>


And then you can add {{var total}} to your terms and conditions in admin.



If you want to filter the conditions that you configured in the admins by name, you need to modify the getAgreements function in the mage control conventions class and the getRequiredAgreementIds function in the Mage Checkout helper data class.

For example, if you want terms for a retail customer group and terms for a wholesale customer group, you should create terms with the word RETAIL and WHOLESALE in the admin term names, and then do something like below where we filter the terms based on the name.

$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();

if ($groupId == 1)
$agreements = Mage::getModel('checkout/agreement')->getCollection()
->addFieldToFilter('is_active', 1)
->addFieldToFilter('name', array('like' => '%RETAIL%'));



If group 1 is a retail group, duplicate the code for other groups.



All Articles