Magento - get current product

前端 未结 4 1991
天涯浪人
天涯浪人 2021-02-05 08:18

I have a sidebar block in my layout that is being displayed on different pages.

In this block I have a list of products, and I want to select the current product when I

相关标签:
4条回答
  • 2021-02-05 08:27
    $_proId  = $this->getProduct()->getId();
    $_product= Mage::getModel('catalog/product')->load($_proId);
    

    This actually does work. It has been down voted above, however it worked for me. I understand that the product does indeed load when you use getProduct(), however, I was only getting partial information of the product. Getting the ID and reloading the product brought back all the information.

    It might be specific to a version of Magento, however it does work though it looks stupid.

    0 讨论(0)
  • 2021-02-05 08:35

    You can get the current product ID and product details from the below code.

    $_proId  = $this->getProduct()->getId();
    $_product= Mage::getModel('catalog/product')->load($_proId);
    
    0 讨论(0)
  • 2021-02-05 08:37

    If you're not willing to use your own block class inheriting from Mage_Catalog_Block_Navigation, in which you could set your own cache informations (to make it eg depending on the current product), you can get rid of the cache for your block by using this in its layout definition :

    <block type="catalog/navigation" name="left.navigation.block" as="left.navigation.block" template="catalog/navigation/page_left.phtml">
        <action method="unsetData"><key>cache_lifetime</key></action>
    </block>
    
    0 讨论(0)
  • 2021-02-05 08:45

    How to get current product id

    Try below code to get currently loaded product id:

    $product_id = $this->getProduct()->getId();
    

    When you don’t have access to $this, you can use Magento registry:

    $product_id = Mage::registry('current_product')->getId();
    
    0 讨论(0)
提交回复
热议问题