I have a strange problem on my test system. I'm quite new to magento and think this problem might exist, because it is a test system.
I have some products with related products. If I try to add a product to the shopping cart with it's related product(s). The related product(s) do not get added to the cart.
My first guess was a broken javascript but everything seems to be ok there. My next step was to get myself temper data for firefox. I have verified, that the PostData contains:
product = $id_of_product
related_product = $id_of_related_product
qty = $qty_of_product
Still the related product does not get added to the cart. Could this be a problem with the setup of the test system? Changing the quantities works fine for the normal product. Changing the id of the related_product to the id of another product does not add anything to the cart, too.
Thanks in advance
EDIT
Ok, as others might have the same problem I leave the original question and describe here how to debug this.
The first thing to do is to deactivate all modules in local and community. The easiest way is to temporarily rename the folders app/code/local
and app/code/community
and test if it works afterwards. If it does not work you have to search further.
If the error still persists check the post message sent to the server. An easy way to do this is to get the Tamper Data addon for firefox. If the post message does not look like the message in the picture:
The error is probably inside the JavaScript of your app/design/frontend/base/default/template/catalog/product/list/related.phtml
file. Change the path to the theme you are using.
The POSTDATA
field should contain related_product=Comma_Separated_Ids_Of_Selected_Related_Products
If everything looks good so far, the mistake is somewhere in the code of magento. How magento adds products to the cart is described in a blog post here: http://www.ecomdev.org/2012/12/12/magento-checkout-shopping-cart-flow.html
The POSTDATA is computed in app/code/core/Magento/Checkout/controllers/CartController.php
at the lines below
193 $cart->addProduct($product, $params);
194 if (!empty($related)) {
195 $cart->addProductsByIds(explode(',', $related));
196 }
Make sure the $related
variable contains the ids of your related products.
If this is the case this will lead you to app/code/core/Mage/Checkout/Model/Cart.php
where in my case the error was.
Best Answer
Ok, I finally can add the
related_product
to the cart. In thecode/core/Mage/Checkout/Model/Cart.php
file are two functions for adding products into the cart. The first is calledpublic function addProduct($productInfo, $requestInfo=null)
, the second function ispublic function addProductsByIds($productIds)
.The
related_product
is added by id, the others are added with theaddProduct()
function. The adding by id does not seem to work (at least for me it did not. Although magento used the unaltered Cart.php file from 1.7.0.2.I solved this by altering the Cart.php file like this:
So i copied the lines from the other function. Does anyone think this will have some drawbacks? For me it looks like this introduces another check if everything works correct by examining the return string. But i don't know if dispatching the event might have some negative effects. As i stated, I'm new to magento and would like to know if this is solid solution.
Does anyone know, why the functions are different? Because they do basically the same. The only difference is, that the second function can add more than product. Maybe in this case dispatching the event is slow?