0

I'm trying to execute the following Method with the Observer in my Magento module. But it isn't working and I don't know why.

This is my config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <TimgMage_CustomAddToCart>
      <version>0.0.1</version>
    </TimgMage_CustomAddToCart>
  </modules>
  <global>
    <models>
      <TimgMage_CustomAddToCart>
        <class>TimgMage_CustomAddToCart_Model</class>
      </TimgMage_CustomAddToCart>
    </models>
    <events>
      <!-- START: Add to cart actions -->
      <checkout_cart_product_add_after>
        <observers>
          <timgmage_customaddtocart_model_observer>
            <class>TimgMage_CustomAddToCart_Model_Observer</class>
            <method>checkoutCartProductAddAfter</method>
            <type>singleton</type>
          </timgmage_customaddtocart_model_observer>
        </observers>
      </checkout_cart_product_add_after>
    </events>
  </global>
</config>

And the observer

<?php

    class TimgMage_CustomAddToCart_Model_Observer
    {

        public function checkoutCartProductAddAfter(Varien_Event_Observer $observer) {

            Mage::log('Entered to checkoutCartProductAddAfter.', null, 'timgmage.log');

            /* @var $item Mage_Sales_Model_Quote_Item */
            $item = $observer->getQuoteItem();
            if ($item->getParentItem()) {
                $item = $item->getParentItem();
            }

            // Discounted 25% off
            $percentDiscount = 0.25;

            // This makes sure the discount isn't applied over and over when refreshing
            $specialPrice = $item->getOriginalPrice() - ($item->getOriginalPrice() * $percentDiscount);

            // Make sure we don't have a negative
            if ($specialPrice > 0) {
                $item->setCustomPrice($specialPrice);
                $item->setOriginalCustomPrice($specialPrice);
                $item->getProduct()->setIsSuperMode(true);
            }
        }
    }

Please anybody can help me.

4
  • Are you seeing anything in your log file? Please try the event controller_action_predispatch_checkout_cart_add instead. Commented Sep 26, 2017 at 12:42
  • Thanks @RickyOdinMatthews. I am new with magento and I don't know where is the log file in production. I tested it. But it's occurring nothing. It's not changing the price. Commented Sep 26, 2017 at 14:11
  • Log file will appear in /var/log/ will post an answer that may help you Commented Sep 26, 2017 at 14:16
  • What are you trying to achieve?
    – Black
    Commented Jan 15, 2019 at 14:58

2 Answers 2

1

1) Please replace your module name from TimgMage_CustomAddToCart to Timgmage_Customaddtocart.

2) There are many errors in your config.xml, please update it with :

<?xml version="1.0"?>
<config>
    <modules>
        <Timgmage_Customaddtocart>
            <version>0.0.1</version>
        </Timgmage_Customaddtocart>
    </modules>
    <global>
        <models>
            <timgmage_customaddtocart> //not with an uppercase
                <class>Timgmage_Customaddtocart_Model</class>
            </timgmage_customaddtocart>
        </models>
    </global>
    <frontend>
        <events>
            <checkout_cart_product_add_after> //Im not sure for this event !
                <observers>
                    <timgmage_customaddtocart_observer>
                        <type>singleton</type>
                        <class>timgmage_customaddtocart/observer</class>
                        <method>checkoutCartProductAddAfter</method>
                    </timgmage_customaddtocart_observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    <frontend>
</config>
0

Try the event controller_action_predispatch_checkout_cart_add instead

Try this code:

<?php

class TimgMage_CustomAddToCart_Model_Observer
{

    public function checkoutCartProductAddAfter(Varien_Event_Observer $observer) {

    $quote = Mage::getModel('checkout/cart')->getQuote();
    $quoteItems = $quote->getItems();

    foreach ($quoteItems as $_item) {
       echo $_item->getId();
    }

  }
}

Quick debugging, this should echo out the cart product ids at top of page. You need to edit each quote item then call $quote->save(); at the end.

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.