php 在 magento 中获取产品 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5256346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:46:09  来源:igfitidea点击:

Get product id in magento

phpmagento

提问by blakcaps

In magento,i want to add quick look feature like this http://www.timberlandonline.co.uk/on/demandware.store/Sites-TBLGB-Site/default/Link-Category?cgid=men_footwear_boots.I have added a hidden input & a div in list.phtml.If i click the div of any product javascript returns product id of first product in that category page.But it should return product id of the selected div.

在 magento 中,我想添加这样的快速查看功能http://www.timberlandonline.co.uk/on/demandware.store/Sites-TBLGB-Site/default/Link-Category?cgid=men_footwear_boots。我添加了一个隐藏输入和 list.phtml 中的 div。如果我单击任何产品的 div,javascript 会返回该类别页面中第一个产品的产品 ID。但它应该返回所选 div 的产品 ID。

回答by Knowledge Craving

You need to look into this page (<path_to_your_template_folder>/template/catalog/product/list.phtml) carefully. You will find the following lines of code in different places of this page only:-

您需要仔细查看此页面 ( <path_to_your_template_folder>/template/catalog/product/list.phtml)。您只会在此页面的不同位置找到以下代码行:-

$_productCollection = $this->getLoadedProductCollection();

foreach ($_productCollection as $_product):
    $reqProductId = $_product->getId();
endforeach;

If you carefully match the above code & the code in the above-mentioned page, you will know that you need to use the variable "$reqProductId" properly in your required "INPUT" element of type "hidden". So you will require it to do your part in the main "foreach" loop.

如果您仔细匹配上述代码和上述页面中的代码,您就会知道您需要$reqProductId在所需的“ INPUT”类型的“ hidden”元素中正确使用变量“ ”。因此,您将要求它在主“ foreach” 循环中发挥您的作用。

Hope it helps.

希望能帮助到你。

回答by Amaresh Tiwari

Try below code to get currently loaded product id:

尝试以下代码以获取当前加载的产品 ID:

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

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

当您无权访问 时$this,您可以使用 Magento 注册表:

$product_id = Mage::registry('current_product')->getId();

Also for product type i think

也适用于我认为的产品类型

$product = Mage::getModel('catalog/product')->load($product_id); 

$productType = $product->getTypeID();