0

Hopefully I'm asking the right question. I followed a great tutorial by Excellence and got AJAX cart functionality. Everything works great, except one thing.

After adding the product to cart, I return a new sidebar via:

$this->getLayout()->getBlock('cart_sidebar')->toHtml() and encode that via json

HOWEVER when the product is a configurable, it pulls the configurable image, not the associated. Yet, when I refresh the page (and it doesn't use my custom ajax controller), it pulls the correct image ($this->getProductThumbnail).

In my xml, I have:

  <block type="checkout/cart_sidebar" name="cart_sidebar" as="cartExplorer" template="checkout/cart/sidebar.phtml" before="-">
    <action method="addItemRender">
      <type>configurable</type>
      <block>checkout/cart_item_renderer_configurable</block>
      <template>checkout/cart/sidebar/default.phtml</template>
    </action>
</block>

Without the tag, it never pulls the associated image, so I believe that's the key. So I think if I can somehow add that action to my php code that grabs the layout block, I'll be set. I just don't know how to do that.

Thanks for any assistance!

2

Actions in layout xml configs are just a block method call. So if you see construction like

<block type     = "checkout/cart_sidebar" 
       name     = "cart_sidebar" 
       as       = "cartExplorer" 
       template = "checkout/cart/sidebar.phtml" 
       before   = "-">
<action method="addItemRender">
    <type>configurable</type>
    <block>checkout/cart_item_renderer_configurable</block>
    <template>checkout/cart/sidebar/default.phtml</template>
</action>

That is equivalent to

$this->getLayout()
     ->createBlock('checkout/cart_sidebar', 'cart_sidebar')
     ->setTemplate('checkout/cart/sidebar.phtml');
     ->addItemRender(
         'configurable', 
         'checkout/cart_item_renderer_configurable',
         'checkout/cart/sidebar/default.phtml'
     )

Or if block cart_sidebar was already created in layout xml

$this->getLayout()
     ->getBlock('cart_sidebar')
     ->addItemRender(
         'configurable', 
         'checkout/cart_item_renderer_configurable',
         'checkout/cart/sidebar/default.phtml'
     )
  • Thank you! This answered my question, but did not solve my problem :( – kevando Feb 4 '14 at 18:47
0

I ran into this problem using the same tutorial. Here's my solution in case someone runs into the same issue.

The issue occurs due to how the data is loaded for the child product when it's first added to the cart and the way getProductThumbnail in Mage_Checkout_Block_Cart_Item_Renderer_Configurable gets the thumbnail. This file is in app/code/core/Mage/Checkout/Block/Cart/Item/Renderer/Configurable.php. I modified a copy of the file in app/code/local.

public function getProductThumbnail()
{
    $product = $this->getChildProduct();
    if (!$product || !$product->getData('thumbnail')
        || ($product->getData('thumbnail') == 'no_selection')
        || (Mage::getStoreConfig(self::CONFIGURABLE_PRODUCT_IMAGE) == self::USE_PARENT_IMAGE)) {
        $product = $this->getProduct();
    }
    return $this->helper('catalog/image')->init($product, 'thumbnail');
}

The problem is the thumbnail data is not set for the child product so $product gets changed to the parent product. I added a couple of lines to check for this and load the data for the child product if the thumbnail path is missing.

public function getProductThumbnail()
{
    $product = $this->getChildProduct();
    if ($product && !$product->getData('thumbnail')) {
        $product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($product->getId());
    }
    if (!$product || !$product->getData('thumbnail')
        || ($product->getData('thumbnail') == 'no_selection')
        || (Mage::getStoreConfig(self::CONFIGURABLE_PRODUCT_IMAGE) == self::USE_PARENT_IMAGE)) {
        $product = $this->getProduct();
    }
    return $this->helper('catalog/image')->init($product, 'thumbnail');
}

From tracing the steps to get here, the way the data for the child product is loaded seems to cause the issue. The child product's data is loaded by the call in the controller to add the product to the cart.

$cart->addProduct($product, $params);

The child product's data is loaded based on the attributes in $params and the parent product's id. However this does not load the image paths. This same data is used later with the call to get the thumbnail, causing the parent image to show up.

This doesn't happen to the items already in the cart because they are loaded separately as part of a collection and their image paths are loaded.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.