Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After digging a bit into Magento's core code, I found that you need to use <code>$item-&gt;getProduct()-&gt;setIsSuperMode(true)</code> in order to make <code>$item-&gt;setCustomPrice()</code> and <code>$item-&gt;setOriginalPrice()</code> work.</p> <p>Here is some sample code you can use within an Observer that listens for the <code>checkout_cart_product_add_after</code> or <code>checkout_cart_update_items_after</code> events. The code is logically the same except <code>checkout_cart_product_add_after</code> is called for only one item and <code>checkout_cart_update_items_after</code> is called for all items in the cart. This code is separated/duplicated into 2 methods only as an example.</p> <h3>Event: checkout_cart_product_add_after</h3> <pre><code>/** * @param Varien_Event_Observer $observer */ public function applyDiscount(Varien_Event_Observer $observer) { /* @var $item Mage_Sales_Model_Quote_Item */ $item = $observer-&gt;getQuoteItem(); if ($item-&gt;getParentItem()) { $item = $item-&gt;getParentItem(); } // Discounted 25% off $percentDiscount = 0.25; // This makes sure the discount isn't applied over and over when refreshing $specialPrice = $item-&gt;getOriginalPrice() - ($item-&gt;getOriginalPrice() * $percentDiscount); // Make sure we don't have a negative if ($specialPrice &gt; 0) { $item-&gt;setCustomPrice($specialPrice); $item-&gt;setOriginalCustomPrice($specialPrice); $item-&gt;getProduct()-&gt;setIsSuperMode(true); } } </code></pre> <h3>Event: checkout_cart_update_items_after</h3> <pre><code>/** * @param Varien_Event_Observer $observer */ public function applyDiscounts(Varien_Event_Observer $observer) { foreach ($observer-&gt;getCart()-&gt;getQuote()-&gt;getAllVisibleItems() as $item /* @var $item Mage_Sales_Model_Quote_Item */) { if ($item-&gt;getParentItem()) { $item = $item-&gt;getParentItem(); } // Discounted 25% off $percentDiscount = 0.25; // This makes sure the discount isn't applied over and over when refreshing $specialPrice = $item-&gt;getOriginalPrice() - ($item-&gt;getOriginalPrice() * $percentDiscount); // Make sure we don't have a negative if ($specialPrice &gt; 0) { $item-&gt;setCustomPrice($specialPrice); $item-&gt;setOriginalCustomPrice($specialPrice); $item-&gt;getProduct()-&gt;setIsSuperMode(true); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload