简体   繁体   中英

Magento get product ID in custom module block on product detail page

I have a custom module with a block which shows a form inside the product detail page. I need to retrieve the product ID for the currently viewed product inside this block. Everything else is working.

Other answers suggested trying:

$this->getProduct()->getId(); // or
Mage::registry('current_product')->getId();

The former returned an error. Using this one inside the product detail template is working fine. I understand that's because getProduct() is not a method for the block.

The latter is always returning null.

If anyone could help me, that would be great.

Thanks in advance.

EDIT: The custom block layout.xml

<layout version="0.1.0">
    <catalog_product_view>
        <reference name="content">
            <reference name="product.info">
                <block
                    type="s148_interestnotification/form"
                    name="interest_notification"
                    before="-"
                    template="s148_interestnotification/index.phtml"
                ></block>
            </reference>
        </reference>
    </catalog_product_view>
</layout>

试试这个代码:

$collection = Mage::getSingleton('Mage_Reports_Block_Product_Viewed')->getItemsCollection();

if your custom block is extending this class

Mage_Catalog_Block_Product_View

you would be easily able to use : $this->getProduct()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM