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

查看:62
本文介绍了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天全站免登陆