0

I have done some custom work where I put a message on top of the cart and let people know they can get 20% discount when they add 1 more item to their cart.

This is a coupon code, let's say number 145. I call that within a custom block inside a CMS page:

{{block type="core/template" name="applieddiscount" CheckCoupon="145" template="customphp/getCartCouponApplied.phtml"}}

Then, I Create local.xml in /app/design/frontend/[package]/[theme]/layout/local.xml

<checkout_cart_index>
    <reference name="checkout.cart.form.before">
    <block type="cms/block" name="promotion_banner_cart" after="checkout.cart.top_methods" >
    <action method="setBlockId"><block_id>promotion_top_of_cart</block_id></action>
    </block>
    </reference>
</checkout_cart_index>

This is the phtml file inside the directory /app/design/frontend/[package]/default/template/customphp/getCartCouponApplied.phtml:

<?php
    $appliedRuleIds = Mage::getSingleton('checkout/session')->getQuote()->getAppliedRuleIds();
    $checkCoupon = $this->getCheckCoupon();
    /*
    This will give you the ids of the rules applied to the quote separated by comma. You can turn them into an array like this:
    */
    $appliedRuleIds = explode(',', $appliedRuleIds);
    if (in_array($checkCoupon, $appliedRuleIds)) {
            $myMessage = $this->__('GREAT, YOUR DISCOUNT IS AUTOMATICALLY APPLIED. WE ARE SURE YOU WILL ENJOY YOUR PURCHASE!');
            echo $this->getLayout()->createBlock('core/messages')->addSuccess($myMessage)->toHtml();
    }else{
            $myMessage = $this->__('ADD AT LEAST ONE OTHER ITEM TO YOUR CART AND RECEIVE 20% DISCOUNT ON ALL OF THEM!');
            echo $this->getLayout()->createBlock('core/messages')->addNotice($myMessage)->toHtml();
    }
?>

This all works, this was my intro. I'm trying to better my life and make this into an extension. I'm just about done, but now i realize I have to know a method to actually mimic/copy this to a working extension.

{{block type="core/template" name="applieddiscount" CheckCoupon="145" template="customphp/getCartCouponApplied.phtml"}}

What i have now is:

<?xml version="1.0" ?>
<config>
    <modules>
        <MyExt_ShowCartBenefits>
            <version>1.0.0</version>
        </MyExt_ShowCartBenefits>   
    </modules>  
    <global>
        <models>
            <MyExt_ShowCartBenefits>
                <class>MyExt_ShowCartBenefits_Model</class>
            </MyExt_ShowCartBenefits>   
        </models>
        <events>
            <checkout_cart_product_add_after>
              <observers>
                <MyExt_ShowCartBenefits_Model_Observer>
                  <type>singleton</type>
                  <class>MyExt_ShowCartBenefits_Model_Observer</class>
                  <method>catchAddToCart</method>
                </MyExt_ShowCartBenefits_Model_Observer>
              </observers>
            </checkout_cart_product_add_after>
        </events>   
    </global>

    <frontend>  
    <layout>
        <updates>
            <MyExt_ShowCartBenefits>
                <file>MyExt/MyExt_ShowCartBenefits.xml</file>
            </MyExt_ShowCartBenefits>
        </updates>
    </layout>
    </frontend>
</config>

The Observer.php (app\code\local\MyExt\ShowCartBenefits\model\Observer.php) is a copy of the same php file I called in here (customphp/getCartCouponApplied.phtml):

{{block type="core/template" name="couponadded" CheckCoupon="145" template="customphp/getCartCouponApplied.phtml"}}

What I would like to know is, how can I get the coupon code number that i used to call with the variable inside the CMS block i showed earlier?

I really need a hand to help me get forward.

What is confusing me is the part what I should use? Do I leave out the Observer.php, or is there a way to leave out the entire CMS block, but then how and/or when do I pass the variable(s) CheckCoupon="145"?

Am I overthinking this and just stick with my old method?

Thank you.

  • I simplified, apparently I wanted too much from it that was not needed. Still one question: I have setup the config.xml and layout updates and used the CMS block as I did in the first place. One issue remains: The block above the cart is always called AFTER a page refresh, or displayed when i move to another place. So it is a click too late;-). The original version did not do that. I checked with all checkout_cart_* events. None worked. Any idea? It is obviously called after the coupon is checked, how do i change that order? – Kim K. Jan 21 '17 at 18:09
0

try this please:

{{block type="core/template" name="applieddiscount" check_coupon="145" template="customphp/getCartCouponApplied.phtml"}}
  • Thanks, but... You copied my own block tag, with the exception of renaming the check coupon variable. It was working fine like that, but i need to know how to implement the calling of a variable inside a module/extension. – Kim K. Jan 19 '17 at 19:45
0

I spent long enough trying to get it to work, only to find out that this does not work well:

echo Mage::getSingleton('checkout/session')->addSuccess($myMessage);

And this DOES display the message immediately. No idea why but if anybody cares to explain that is educative;-)

echo $this->getLayout()->createBlock('core/messages')->addSuccess($myMessage)->toHtml();    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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