2

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?

  • 1
    From the looks of it, your problem is the block singleton not the Magento registry. – Melvyn Feb 19 '15 at 18:12
2

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;
}
1

The problem is that Mage::registry() is only called once, namely in the first call to the getProduct() method of the block and from then on stored in and retrieved from an attribute. If you replace

$this->getLayout()->getBlockSingleton('catalog/product_view_type_configurable')

With

$this->getLayout()->getBlock('catalog/product_view_type_configurable')

The block instance won't be reused and the product fetched from the registry each time

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.