Magento - 动态产品属性仅从单个 CMS 块中提取数据
Magento - Dynamic Product Attribute Only Pulling Data from Single CMS Block
我在我的个人产品页面上设置了 2 个额外的标签,它们应该从每个产品的不同 CMS 块中提取信息。但是,当我查看产品页面时,它们都从我创建的 1 个 CMS 块中提取完全相同的信息,而不是从该特定产品的 CMS 块中提取完全相同的信息。
以下是我在尝试进行故障排除时注意到的其他一些事情:
1) 如果我刷新 Magento 缓存然后转到产品页面,则会为该页面显示正确的信息。然后,如果我导航到任何其他产品页面,它总是显示我刷新 Magento 缓存后查看的第一个产品的信息。
2) 这似乎也与产品类别有关。我有 2 个产品类别。类别 1 中的每个产品都将显示相同的 CMS 块的信息。类别2中的每个产品都会显示相同的CMS Block信息,但这些产品的CMS Block信息实际上与类别1中的产品不同,但都是一样的。
以下是我设置所有内容的方式:
1) 为每个具有不同信息的产品创建了单独的 CMS 块。
2) 创建了一个文本区域属性并将其添加到正确的属性集中。对于每个产品,我都为该产品的 CMS 块输入了 ID。我仔细检查过,每个产品都输入了不同的 ID。
3) 在 app/design/frontend/rwd/default/layout/catalog.xml 中,我添加了以下内容以显示 2 个新选项卡:
<!-- Features -->
<block type="catalog/product_view_attributes" name="product.features" as="features" template="catalog/product/view/features.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Features</value></action>
</block>
<!-- END Features -->
<!-- TECH SPECS -->
<block type="catalog/product_view_attributes" name="product.tech_specs" as="techspecs" template="catalog/product/view/tech_specs.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Tech Specs</value></action>
</block>
<!-- END TECH SPECS -->
4) 最后,我创建了 2 个文件
app/design/frontend/rwd/default/template/catalog/product/view/features.phtml
app/design/frontend/rwd/default/template/catalog/product/view/tech_specs.phtml
代码如下:
features.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('features');
if ( is_object($attribute) ) {
$identifier = $_product->getData("features");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
tech_specs.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('tech_specs');
if ( is_object($attribute) ) {
$identifier = $_product->getData("tech_specs");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
有人知道这里发生了什么吗?
想通了。这是因为 Magento 正在缓存 CMS 块。
解法:
复制
app/code/core/Mage/Cms/Block/Block.php
到
app/code/local/Mage/Cms/Block/(我必须添加丢失的文件夹层次结构)
然后编辑Block.php
在受保护的函数_construct()中
改变
$this->setCacheLifetime(false);
至
$this->setCacheLifetime(null);
不再有 CMS 块缓存,动态内容按预期显示!
如果属性是动态的,您可以对其内容进行哈希处理并将其添加到缓存标记中,请参阅 "yes" 或 "no" 属性的示例,但如果它更改缓存重新加载 ;)
在 di.xml 中重写来自 Magento 2.1 供应商的块后,执行此操作:
namespace Dpl\Xxx\Block\Catalog\Product\View;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class Attributes extends \Magento\Catalog\Block\Product\View\Attributes
{
/**
* Retrieve current product model
*
* @return \Magento\Catalog\Model\Product
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
PriceCurrencyInterface $priceCurrency,
array $data = []
) {
parent::__construct($context, $registry, $priceCurrency, $data);
$this->setCacheTag($this->getProduct()->getId().'-'.$this->getProduct()->getXxxopen();
}
}
?>
此致 ;)
我在我的个人产品页面上设置了 2 个额外的标签,它们应该从每个产品的不同 CMS 块中提取信息。但是,当我查看产品页面时,它们都从我创建的 1 个 CMS 块中提取完全相同的信息,而不是从该特定产品的 CMS 块中提取完全相同的信息。
以下是我在尝试进行故障排除时注意到的其他一些事情:
1) 如果我刷新 Magento 缓存然后转到产品页面,则会为该页面显示正确的信息。然后,如果我导航到任何其他产品页面,它总是显示我刷新 Magento 缓存后查看的第一个产品的信息。
2) 这似乎也与产品类别有关。我有 2 个产品类别。类别 1 中的每个产品都将显示相同的 CMS 块的信息。类别2中的每个产品都会显示相同的CMS Block信息,但这些产品的CMS Block信息实际上与类别1中的产品不同,但都是一样的。
以下是我设置所有内容的方式:
1) 为每个具有不同信息的产品创建了单独的 CMS 块。
2) 创建了一个文本区域属性并将其添加到正确的属性集中。对于每个产品,我都为该产品的 CMS 块输入了 ID。我仔细检查过,每个产品都输入了不同的 ID。
3) 在 app/design/frontend/rwd/default/layout/catalog.xml 中,我添加了以下内容以显示 2 个新选项卡:
<!-- Features -->
<block type="catalog/product_view_attributes" name="product.features" as="features" template="catalog/product/view/features.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Features</value></action>
</block>
<!-- END Features -->
<!-- TECH SPECS -->
<block type="catalog/product_view_attributes" name="product.tech_specs" as="techspecs" template="catalog/product/view/tech_specs.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Tech Specs</value></action>
</block>
<!-- END TECH SPECS -->
4) 最后,我创建了 2 个文件
app/design/frontend/rwd/default/template/catalog/product/view/features.phtml app/design/frontend/rwd/default/template/catalog/product/view/tech_specs.phtml
代码如下:
features.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('features');
if ( is_object($attribute) ) {
$identifier = $_product->getData("features");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
tech_specs.phtml
<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('tech_specs');
if ( is_object($attribute) ) {
$identifier = $_product->getData("tech_specs");
}
?>
<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
<div class="std">
<?php echo $_sizeBlock->toHtml() ?>
</div>
<?php endif; ?>
有人知道这里发生了什么吗?
想通了。这是因为 Magento 正在缓存 CMS 块。
解法:
复制 app/code/core/Mage/Cms/Block/Block.php
到 app/code/local/Mage/Cms/Block/(我必须添加丢失的文件夹层次结构)
然后编辑Block.php
在受保护的函数_construct()中
改变 $this->setCacheLifetime(false);
至 $this->setCacheLifetime(null);
不再有 CMS 块缓存,动态内容按预期显示!
如果属性是动态的,您可以对其内容进行哈希处理并将其添加到缓存标记中,请参阅 "yes" 或 "no" 属性的示例,但如果它更改缓存重新加载 ;)
在 di.xml 中重写来自 Magento 2.1 供应商的块后,执行此操作:
namespace Dpl\Xxx\Block\Catalog\Product\View;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class Attributes extends \Magento\Catalog\Block\Product\View\Attributes
{
/**
* Retrieve current product model
*
* @return \Magento\Catalog\Model\Product
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Registry $registry,
PriceCurrencyInterface $priceCurrency,
array $data = []
) {
parent::__construct($context, $registry, $priceCurrency, $data);
$this->setCacheTag($this->getProduct()->getId().'-'.$this->getProduct()->getXxxopen();
}
}
?>
此致 ;)