php Magento - 获取当前产品
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11506492/
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
Magento - get current product
提问by Shani1351
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'm on a product page.
在这个块中,我有一个产品列表,当我在产品页面上时,我想选择当前产品。
I'm using :
我正在使用 :
$current_product = Mage::registry('current_product');
to get the current product, but this works only for the first time I'm loading the product page. When I select a different product the code above returns the same value (the first product).
获取当前产品,但这仅适用于我第一次加载产品页面。当我选择不同的产品时,上面的代码返回相同的值(第一个产品)。
I'm assuming this happens because I'm using Magento cache. What can I do to get the right value?
我假设发生这种情况是因为我使用的是 Magento 缓存。我该怎么做才能获得正确的价值?
The same thing happens when I use:
当我使用时会发生同样的事情:
$currentCategory = Mage::registry('current_category');
The sidebar block is a navigation template I've added here: ..\app\design\frontend\default\mytheme\template\catalog\navigation\page_left.phtml.
侧边栏块是我在此处添加的导航模板:..\app\design\frontend\default\mytheme\template\catalog\navigation\page_left.phtml。
I'm adding it to the layout with this XML :
我正在使用此 XML 将其添加到布局中:
<block type="catalog/navigation" name="left.navigation.block" as="left.navigation.block" template="catalog/navigation/page_left.phtml"/>
采纳答案by blmage
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 :
如果您不愿意使用自己的块类继承自Mage_Catalog_Block_Navigation,您可以在其中设置自己的缓存信息(例如根据当前产品进行设置),您可以通过在它的布局定义:
<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>
回答by drsndodiya
How to get current product id
如何获取当前产品ID
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();
回答by Adrian
$_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.
这确实有效。它在上面被否决了,但是它对我有用。我知道当您使用 时该产品确实会加载getProduct(),但是,我只获得了该产品的部分信息。获取 ID 并重新加载产品会带回所有信息。
It might be specific to a version of Magento, however it does work though it looks stupid.
它可能特定于某个版本的 Magento,但它确实有效,尽管它看起来很愚蠢。
回答by Sankar Subburaj
You can get the current product ID and product details from the below code.
您可以从以下代码中获取当前产品 ID 和产品详细信息。
$_proId = $this->getProduct()->getId();
$_product= Mage::getModel('catalog/product')->load($_proId);

