Magento 1.7 – How to Set Block Actions in PHP

ajaxmagento-1.7xml

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!

Best Answer

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'
     )