Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd product to cart in observer
    primarykey
    data
    text
    <p>I need to create a possibility for customers to offer a one product free of charge (as an example) in Magento shop. I figured out most of things from <a href="https://stackoverflow.com/questions/5104482/programmatically-add-product-to-cart-with-price-change">this</a> post, but now I have one problem. I am created an Observer method that is executed on <code>checkout_cart_product_add_after</code>. And there I have some code (its just a part of whole thing): </p> <pre><code> $productId = $observer-&gt;getProduct()-&gt;getId(); $cartHelper = Mage::helper('checkout/cart'); $items = $cartHelper-&gt;getCart()-&gt;getItems(); Mage::getModel('core/session')-&gt;addNotice('Product id: '.$productId); foreach ($items as $item) { if ($item-&gt;getProduct()-&gt;getId() == $productId) { $itemId = $item-&gt;getItemId(); $cart = $cartHelper-&gt;getCart()-&gt;removeItem($itemId)-&gt;save() ;//It WORKS!!!!!!!!!!!! $product = Mage::getModel('catalog/product')-&gt;load($productId); $cart-&gt;addProduct($product, $this-&gt;_getRequest()-&gt;getParams()); $cart-&gt;save(); Mage::getSingleton('checkout/session')-&gt;setCartWasUpdated(true); break; } } </code></pre> <p>So first of all my code adds a product to cart with 0.00 price (product as an example) based on some request parameters. Thats part works fine so it is not posted here. The second part (that I posted above) must delete an existing product and replace it with product with normal price. Almost everything works fine except one thing - the line <code>$cart-&gt;addProduct($product, $this-&gt;_getRequest()-&gt;getParams());</code> don't work and I cant understand why (no errors, no exceptions, no logs). <code>_getRequest</code> method looks like this:</p> <pre><code> protected function _getRequest() { return Mage::app()-&gt;getRequest(); } </code></pre> <p>And if I log an <code>$this-&gt;_getRequest()-&gt;getParams()</code> there is will be something like this: </p> <pre><code> 2012-04-09T14:46:56+00:00 DEBUG (7): Array( [uenc] =&gt; aHR0cDovL2xvY2FsaG9zdC93b3AvZmVhdHVyZWQvY2xhc3NpYy1saW5lLXBvbHkuaHRtbA,, [product] =&gt; 50 [related_product] =&gt; [bundle_option] =&gt; Array( [20] =&gt; 75 [21] =&gt; 84 [22] =&gt; 94 ) [qty] =&gt; 1 [send-request] =&gt; 0 ) </code></pre> <p>So why that <code>$cart-&gt;addProduct($product, $this-&gt;_getRequest()-&gt;getParams());</code> will not work? How to do it right? Maybe it is better to use <code>addAction()</code> of <code>Mage_Checkout_CartController</code> (overwrite it)? Sorry for my language. Thanks for answers. Hope somebody can help me...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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