[英]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.
我需要在此块中检索当前查看的产品的产品ID。 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.
我知道这是因为getProduct()不是该块的方法。
The latter is always returning null. 后者总是返回null。
If anyone could help me, that would be great. 如果有人可以帮助我,那就太好了。
Thanks in advance. 提前致谢。
EDIT: The custom block layout.xml 编辑:自定义块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
Mage_Catalog_Block_Product_View
you would be easily able to use : $this->getProduct() 您将可以轻松使用: $ this-> getProduct()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.