我希望在产品页面上显示一个静态块,如果产品属于某个属性集。
我的想法是,如果产品页面的属性集为 "Rc",则在产品页面上显示一个块,否则不显示块。我有一个自定义的主题,我已经做了一个块,并显示在所有的产品页面。我只需要在属性设置为 "Rc "的产品页面上显示区块。我不知道的文件夹结构,或者如果下面的代码是适用于Magento 2.3。我在哪里复制模板文件到和从......基本上整个九码如何实现的设置和代码。
我找到的代码如下(有我的注释)。
"将此方法添加到产品视图块 "从我读到的视图块是没有更多的,它现在被称为目录_product_view.xml的文件夹结构是什么?
appdesignfrontendVendorthemeMagento_Cataloglayoutcatalog_product_view.xml。
public function checkAttributeSet($product = null, $attributeSetName = null)
{
if(is_null($product) || is_null($attributeSetName))
return false;
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
if($attributeSetModel->getAttributeSetName() == $attributeSetName) {
return true;
} else {
return false;
}
}
"那么在appdesignfrontendpackagethemetemplatecatalogproductview.phtml:"viewphtml文件是否不再使用,这是什么正确的文件夹结构。
if($this->checkAttributeSet($_product, 'Rc')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('Rc')->toHtml();
elseif($this->checkAttributeSet($_product, 'ORC')):
echo $this->getLayout()->createBlock('cms/block')->setBlockId('ORC')->toHtml();
endif;
我所设置的是(default.xml)
<referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View" name="product-rc" template="Magento_Theme::product-rc.phtml" after="product.info.price">
</block>
</referenceBlock> -->
product-rc.phtml工作了,并且在所有产品中显示。
(测试块)文本字符串在phtml文件中。
我明白了。下面的代码对我来说是可行的。
添加代码
模块-目录视图前端模板产品视图。
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$attributeSet = $objectManager->create('Magento\Eav\Api\AttributeSetRepositoryInterface');
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());
$attributeSetRepository = $attributeSet->get($product->getAttributeSetId());
$attribute_set_name = $attributeSetRepository->getAttributeSetName();
//$attribute_set_name_arr[] = $attribute_set_name;
//echo '<pre>'; print_r($attribute_set_name);
if( !empty($attribute_set_name) && $attribute_set_name == 'Rc' ) {
// echo $this->getLayout()
->createBlock('Magento\Cms\Block\Block')
->setBlockId('rcitems')
->toHtml();
}
?>
setBlockId = The Identifier of the block in admin.
Rc = is the attribute set
no need to add to default.xml