Magento – Mage::registry Not Updating

blocksregistry

From the info template in the wishlist I have added the following code

Mage::unregister('product');
Mage::register('product', $item->getProduct());
echo $item->getProduct()->getName();
$checkoutLinksBlock = $this->getLayout()->getBlockSingleton('catalog/product_view_type_configurable')->setTemplate('catalog/product/view/type/options/configurable.phtml');
$checkoutLinksBlock->setParentBlock($linksBlock);
echo $checkoutLinksBlock->renderView();

This calls the configurable.phtml file which allows you to select options on the product page. The call is rendered once per product in the wishlist so that each product row has the markup from configurable.phtml relating to that product.

My issue is that the Mage::register('product', $item->getProduct()) seems to only work once and instead of different product options being sent to the configurable.phtml template it only ever uses the first product that i register. $item->getProduct() definitely changes because each row in the wishlist has a the correct product but instead of showing it's own configurable options it applies the configurable options of the first product in the wishlist to each row.

I have tested by outputing $item->getProduct() inside the wishlist template and confirming that it is correct. This seems to me like an issue updating Mage::register more than once during a page load.

Any ideas?

Best Answer

Take a look at register() in /app/Mage.php, You can not assign the same key twice (this should trigger an error);

A better way to accomplish this, would be to pass the product info into the block.

 $this->getLayout()->getBlockSingleton('catalog/product_view_type_configurable')
      ->setTemplate('catalog/product/view/type/options/configurable.phtml')
      ->setProduct($item->getProduct());

Then in your block and template file using $this->getproduct()

But you could also try using Mage::unregister('product')

[loop]
   Mage::register('product', $item->getProduct()) // set
     //display template
   Mage::unregister('product') //
[/loop]

See

/**
 * Register a new variable
 *
 * @param string $key
 * @param mixed $value
 * @param bool $graceful
 * @throws Mage_Core_Exception
 */
public static function register($key, $value, $graceful = false)
{
    if (isset(self::$_registry[$key])) {
        if ($graceful) {
            return;
        }
        self::throwException('Mage registry key "'.$key.'" already exists');
    }
    self::$_registry[$key] = $value;
}
Related Topic