Magento 将当前产品 ID 传递给模块 [英] Magento pass current product ID to module

查看:27
本文介绍了Magento 将当前产品 ID 传递给模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义模块,可以在我的产品页面上显示数据.我的模块需要获取当前的产品 ID.我尝试使用:

I have a custom module that displays data on my product pages. My module needs to get the current product id. I tried using:

Mage::registry('current_product');

这在第一次加载时有效,但当我刷新时,current_product 不再有全页缓存的数据.

This works on the first load but when I refresh, current_product no longer has data with full page caching on.

有什么想法吗?

推荐答案

当整页缓存处理请求时,目录产品操作控制器不会被分派(这对保持速度很有意义).因此,永远不会设置注册表变量.我假设您是在其他完全缓存的页面上动态生成块.我的建议是尽量避免昂贵的加载,因为这会破坏整页缓存的速度改进.如果可能,您确实希望缓存该块,即使它是针对每个客户和每个产品的单独缓存条目.

The catalog product action controller isn't dispatched, when the full page cache handles the request (which makes sense to keep things fast). So, the registry variables are never set. I assume you are generating the block dynamically on the otherwise fully cached page. My recommendation is to try to avoid expensive loads, since that would undermine the speed improvements by the full page cache. You really want to cache the block if at all possible, even if it's a separate cache entry for each customer and each product.

也就是说,这是如何做到的:

That said, here is how to do that:

在容器中,实现_getIdentifier()方法:

protected function _getIdentifier()
{
    return $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '');
}

同时扩展 _getCacheId() 方法以包含方法 _getIdentifier() 的返回值和一个新的占位符属性:product_id

Also expand the _getCacheId() method to include the return value of the method _getIdentifier() and a new placeholder attribute: product_id

protected function _getCacheId()
{
    return 'HOMEPAGE_PRODUCTS' . md5($this->_placeholder->getAttribute('cache_id') . ',' . $this->_placeholder->getAttribute('product_id')) . '_' . $this->_getIdentifier();
}

接下来,在块类中,扩展方法getCacheKeyInfo().cache_key_info 数组中带有字符串索引的所有条目都在占位符上设置为属性.这就是我们如何将产品 ID 传递给占位符.

Next, in the block class, extend the method getCacheKeyInfo(). All entries in the cache_key_info array with a string index are set on the placeholder as attributes. That is how we can pass the product id to the placeholder.

public function getCacheKeyInfo()
{
    $info = parent::getCacheKeyInfo();
    if (Mage::registry('current_product'))
    {
        $info['product_id'] = Mage::registry('current_product')->getId();
    }
    return $info;
}

然后通过在容器类中覆盖它并返回false来启用方法_saveCache().那么现在,因为容器从_getCacheId()返回了一个有效的id,并且_saveCache()是继承自父类,所以可以缓存块,并应用于Enterprise_PageCache_Model_Container_Abstract::applyWithoutApp().

Then enable the method _saveCache() by not overriding it in your container class and returning false. So now, because the container returns a valid id from _getCacheId(), and _saveCache() is inherited from the parent class, the the block can be cached, and applied to the content in an efficient manner in Enterprise_PageCache_Model_Container_Abstract::applyWithoutApp().

您可以通过让容器从 Enterprise_PageCache_Model_Container_Customer 而不是 Enterprise_PageCache_Model_Container_Abstract 扩展来设置缓存条目的生命周期.

You can set the lifetime of the cache entry by having your container extend from Enterprise_PageCache_Model_Container_Customer instead of Enterprise_PageCache_Model_Container_Abstract.

如果您仍然需要将 product_id 传递给块(即使它现在已缓存),您可以在容器的 _renderBlock() 方法中执行此操作:

If you still need to pass the product_id to the block (even though it's cached now), you can do so in the _renderBlock() method of your container:

protected function _renderBlock()
{
    $blockClass = $this->_placeholder->getAttribute('block');
    $template = $this->_placeholder->getAttribute('template');

    $block = new $blockClass;
    $block->setTemplate($template)
        ->setProductId($this->_placeholder->getAttribute('product_id'));
    return $block->toHtml();
}

这篇关于Magento 将当前产品 ID 传递给模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆