Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

SOLVED: Related Product is not added to cart
 
Darokthar
Jr. Member
 
Total Posts:  10
Joined:  2012-08-21
 

I have added the related block to the left column of my product view in the local.xml like this:

<reference name="left">
    <
block type="catalog/product_view_media" name="product.info.media" as="media" template="catalog/product/view/media.phtml"/>
    <
block type="catalog/product_list_related" name="catalog.product.related" as="related" template="catalog/product/list/related.phtml"/>
</
reference>

But checking the checkbox and selecting a product, does not add the related product to the cart. How do i debug this?

I have renamed the whole template folder of my theme but it has no effect on the related product. Therefore I’m thinking, it is not because of a broken template file. I guess it might be a code problem. But i don’t know where to start in the first place. I’m running magento 1.7.0.2 updated from 1.4.1.1.

Any hints?

The html looks like this:

<div class="block block-related">
    <
div class="block-title">
        <
strong><span>Related</span></strong>
    </
div>
    <
div class="block-content">
        <
class="block-subtitle">Select the articles to add to your cart<a href="#" onclick="selectAllRelated(this); return false;">Select all</a></p>
        <
ol class="mini-products-list" id="block-related">
            <
li class="item">
                <
input type="checkbox" class="checkbox related-checkbox" id="related-checkbox61" name="related_products[]" value="61" />
                <
div class="product">
                <
a href="http://path/to/product.html" title="Product name" class="product-image"><img src="path/to/image.jpg" width="50" height="50" alt="" /></a>
                    <
div class="product-details">
                        <
class="product-name"><a href="http://path/to/product.html">Product name</a></p>
                        <
div class="price-box">
                            <
span class="regular-price" id="product-price-61-related">
                                <
span class="price">11,95 €</span>
                            </
span>
                        </
div>
                        <
span class="tax-details">inkl19MwSt., zzgl. <a href="http://path/to/agb/">Shipping</a></span>
                        <
span class="tax-details">inkl19MwSt., zzgl. <a href="http://path/to/agb/">Shipping</a></span>
                    </
div>
                </
div>
            </
li>
        </
ol>
        <
script type="text/javascript">decorateList('block-related''none-recursive')</script>
    
</div>
    <
script type="text/javascript">
    
//<![CDATA[
    
$$('.related-checkbox').each(function(elem){
        Event
.observe(elem'click'addRelatedToProduct)
    
});

    var 
relatedProductsCheckFlag false;
    function 
selectAllRelated(txt){
        
if (relatedProductsCheckFlag == false{
            
$$('.related-checkbox').each(function(elem){
                elem
.checked true;
            
});
            
relatedProductsCheckFlag true;
            
txt.innerHTML="Alle abwählen";
        
else {
            
$$('.related-checkbox').each(function(elem){
                elem
.checked false;
            
});
            
relatedProductsCheckFlag false;
            
txt.innerHTML="Alle wählen";
        
}
        addRelatedToProduct
();
    
}

    
function addRelatedToProduct(){
        
var checkboxes = $$('.related-checkbox');
        var 
values [];
        for(var 
i=0;i<checkboxes.length;i++){
            
if(checkboxes[i].checkedvalues.push(checkboxes[i].value);
        
}
        
if($('related-products-field')){
            
$('related-products-field').value values.join(',');
        
}
    }
    
//]]>
    
</script>
</div>
</
div>

Ok, i have investigated further into this. It seems neither the javascript, nor the template files are the problem. I have tampered with the post data and it seems the related_product field does not get computed at all. It makes no difference which value is given here. On the live system it is working, if i change the data. But no luck on the test system.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Darokthar
Jr. Member
 
Total Posts:  10
Joined:  2012-08-21
 

I have found a solution. But i don\’t know why this was not working before. Magento uses the original file from app/code/core/Mage/Checkout/Model/Cart.php version 1.7.0.2

The related product is added to the cart in the function addProductsById defined at line 307. This function is not dispatching the signal as the above addProduct function does. And the function does not check if <code>$this->getQuote->addProduct($product);</code> returns a string.

If I edit the addProductsById function like this, magento adds the related product.

/**
 * Adding products to cart by ids
 *
 * @param   array $productIds
 * @return  Mage_Checkout_Model_Cart
 */
public function addProductsByIds($productIds)
 
{
  $allAvailable 
true;
  
$allAdded     true;

  if (!empty(
$productIds)) {
    
foreach ($productIds as $productId{
      $productId 
= (int) $productId;
      if (!
$productId{
        
continue;
      
}
      $product 
$this->_getProduct($productId);
      if (
$product->getId() && $product->isVisibleInCatalog()) {
        
try {
          $result 
$this->getQuote()->addProduct($product);
          
Mage::log(\'AddProduct() id: \' $productIdnull\'blume.log\');
        
catch (Exception $e){
           $allAdded 
false;
        
}
        
/**
         * String we can get if prepare process has error
         */
        
if (is_string($result)) {
          $redirectUrl 
= ($product->hasOptionsValidationFail())
          ? 
$product->getUrlModel()->getUrl$product,
                                                                           array(
\'_query\' => array(\'startcustomization\' => 1))
                                                                          )
                                                                          : 
$product->getProductUrl();
          
$this->getCheckoutSession()->setRedirectUrl($redirectUrl);
          if (
$this->getCheckoutSession()->getUseNotice() === null{
            $this
->getCheckoutSession()->setUseNotice(true);
          
}
          Mage
::throwException($result);
          
}
          Mage
::dispatchEvent(\'checkout_cart_product_add_after\', array(\'quote_item\' => $result\'product\' => $product));
          
$this->getCheckoutSession()->setLastAddedProductId($productId);
          
else {
            $allAvailable 
false;
          
}
        }

        
if (!$allAvailable{
          $this
->getCheckoutSession()->addError(
                       
Mage::helper(\'checkout\')->__(\'Some of the requested products are unavailable.\')
                        );
        
}
        
if (!$allAdded{
          $this
->getCheckoutSession()->addError(
                      
Mage::helper(\'checkout\')->__(\'Some of the requested products are not available in the desired quantity.\')
                      );
        
}
      }
      Mage
::log(\'$allAvailable: \' $allAvailable \' $allAdded: \' $allAddednull\'blume.log\');
      return 
$this;
  
}
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top