简体   繁体   English

Magento在产品详细信息页面上的自定义模块块中获取产品ID

[英]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.

相关问题 magento产品详细信息页面的自定义布局 - magento custom layout for product detail page 在Magento的产品比较页面上获取产品ID? - Get product ID on product compare page in Magento? 如何在magento中获取当前产品的类别名称(在产品详细信息页面上) - how to get category name of current product (on product detail page) in magento magento 2.4.1 的单个产品详细信息页面上没有显示带有简单消息的定制模块? - Custom built module with simple message is not displaying on single product detail page in magento 2.4.1? magento将自定义选项卡添加到前端产品详细信息页面不起作用 - magento add custom tab to frontend product detail page not working Magento模块自定义模块在产品详细信息页面中有效,但在列表和网格页面中无效? - Magento module custom block working in product details page but not working in list and grid page? 在magento中获取产品ID - Get product id in magento Magento-如何使用模块在产品详细信息页面中添加新的自定义模块 - Magento- How can i add a new custom block in product details page using module Magento 2:如何在状态可见 =&gt; 是的自定义模块块文件中获取产品属性集合? - Magento 2 : How to get product attributes collection in custom module block file whose status is visible =>yes? 在“前端产品”页面中获取创建自定义模块 - Get create Custom Block in FrontEnd Product page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM