3

I want to change a checkout/onepage layout based on a condition.

Example:

<?php
    $coupon = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
    if(($coupon == "SE50") || ($coupon == "SE00")){
    }
?>

To showcheckout.xml

<action method="setTemplate"><template>page/2columns-right.phtml</template></action>

I mean this page:

Enter image description here

if(empty($coupon))

<action method="setTemplate"><template>page/checkout-onepage.phtml</template></action>

A page like:

Enter image description here

Note: checkout-onepage.phtml is a custom page.

2 Answers 2

4

You can use a helper function to set the template based on a condition. Follow the below code.

Change the action tag in your layout.xml to the below.

 <action method="setTemplate"><template helper="your_module/getLayout" /></action>

In your module's helper file Data.php add the below code.

public function getLayout()
{
    $coupon = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
    if(($coupon == "SE50") || ($coupon == "SE00")){

        return 'page/2columns-right.phtml';
    }
    else
    {
        return 'page/checkout-onepage.phtml';
    }
}
11
  • Thanks for code Bro you mean create Data.php file in app\code\core\Mage\Checkout\Helper
    – Ramesh S
    Commented Oct 9, 2017 at 12:43
  • 1
    No don't modify core files, add it to your existing custom module's Helper/Data.php or create a new custom module
    – Piyush
    Commented Oct 9, 2017 at 12:44
  • 1
    Same idea here ... +1
    – sv3n
    Commented Oct 9, 2017 at 12:45
  • @Piyush: Bro showing Fatal error: Class 'Mage_Your_Module_Helper_Data' not found in D:\xampp\htdocs\ramesh\sliver-exchange\app\Mage.php on line 547
    – Ramesh S
    Commented Oct 9, 2017 at 12:59
  • 1
    please replace your_module in layout file with your module name
    – Piyush
    Commented Oct 9, 2017 at 13:00
3

Either change your checkout.xml (or better add this to your local.xml)

...
<action method="setTemplate">
    <template helper="module/helper/yourMethod" />
</action>
...

Add a custom module with a helper method like ...

public function yourMethod()
{
    $coupon = Mage::getSingleton('checkout/session')->getQuote()->getCouponCode();
    if ($coupon == 'SE50' || $coupon == 'SE00') {
        return 'page/2columns-right.phtml';
    } else {
        return 'page/2columns-left.phtml';
    }
}
3
  • I will try both functions
    – Ramesh S
    Commented Oct 9, 2017 at 12:46
  • Its the same as Piyush posted ... he was just faster :)
    – sv3n
    Commented Oct 9, 2017 at 12:47
  • Ha ha right... :)
    – Ramesh S
    Commented Oct 9, 2017 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.