4

This is a weird request from a client, the products can be purchased with or without logos adding on to them. However, when a product which requires a logo is added to the cart the checkout process needs to be slightly different as additional costs will be added afterwards.

I've tried setting a new session in order to store whether the cart has a product which requires a logo in it and using various if statements to overcome this request, example:

            <?php foreach($this->getItems() as $_item): ?>
                <?php
                    $get_attributes = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
                    $attributes = $get_attributes['attributes_info'];
                    foreach($attributes as $attribute) {
                        if($attribute['value'] == 'Has Logo') {
                            $logo_cart = TRUE;
                        }
                    }
                ?>
                    <?php echo $this->getItemHtml($_item) ?>
                <?php endforeach ?>

Using the $logo_cart variable I set it in the core/session, then use:

Mage::getSingleton('core/session')->getLogoCart();

within if statements to change aspects of the checkout.

This is obviously not an ideal way of achieving this, especially since I am currently only setting the session once the customer hits the cart page so it's quite buggy if they're already on the checkout page.

Are there any suggestions on how I can achieve the above? I'm currently using the One Page Checkout module found here: http://www.magentocommerce.com/magento-connect/express-checkout-one-step-checkout.html

One method I've thought about is switching the entire template depending on what's in the cart, but I'm unsure which files I'd need to change within the module to achieve this.

EDIT: Just to clarify, the entire process is to remain the same. I just need to remove a few options from the checkout, such as the shipping method, the review your order section, and display a new area, which are just template changes anyway.

1 Answer 1

8
+50

The cleanest way to do this is will use observers and custom layout XML. For that you will need to create your own extension. Inchoo has a nice hello world article that should get you started and you can read more about observers in this Magento Board post.

You will need to hook the controller_action_layout_load_before event. Add this to your config.xml.

<controller_action_layout_load_before>
    <observers>
        <[module]>
            <type>singleton</type>
            <class>[module]/observer</class>
            <method>addLayoutHandleObserver</method>
        </[module]>
    </observers>
</controller_action_layout_load_before>

Your Observer would use your code to check for the specific product and if present add a layout handle to the generate Layout XML. This will enable us to target it from a layout xml file.

class [Namespace]_[Module]_Model_Observer
{
    public function addLayoutHandleObserver(Varien_Event_Observer $observer)
    {

        $items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

        $logo_cart = false;
        foreach ($items as $_item) 
        {
            $get_attributes = $_item->getProduct()->getTypeInstance(true)->getOrderOptions($_item->getProduct());
            $attributes = $get_attributes['attributes_info'];
            foreach($attributes as $attribute) {
                if($attribute['value'] == 'Has Logo') {
                    $logo_cart = true;
                    break;
                }
            }
           if ($logo_cart) break;
        }

        if ($logo_cart)
        {
            $layout = $observer->getAction()->getLayout();
            $layout->getUpdate()->addHandle('checkout_has_logo');
            $layout->generateXml();
        }

        return $this;
    }
}

Now we can add some custom layout updates from, for example, the local.xml.

<checkout_has_logo>
    <reference name="checkout.onepage">
        <action method="setTemplate"><template>checkout/onepage_logo.phtml</template></action>
    </reference>
</checkout_has_logo>
10
  • That is a brilliant answer. Will try implementing something based on your advice and let you know how I get on. +1 for the idea though!
    – Karl
    Mar 24, 2014 at 12:31
  • Thanks, I've used it somewhat like this in github.com/magento-hackathon/layered-landing if you want to see it in action. Any questions be sure to ask! Mar 24, 2014 at 12:36
  • @SanderMangel You can simply put break; after $logo_cart = true; in the loop. There is no reason of looping again if the required option is found.
    – MagePsycho
    Mar 24, 2014 at 14:04
  • @MagePsycho updated the answer :) Mar 24, 2014 at 14:20
  • I've got it working so far that it uses the new observer class and $logo_cart is set to true, but it doesn't appear to add the new handle to load in a different template. I've added the checkout_has_logo reference to the local.xml file and created the file within the right location but still the old template is included. Any ideas?
    – Karl
    Mar 24, 2014 at 16:24

Your Answer

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

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