根据产品属性集显示html元素? [英] Display html element based on product attribute set?
问题描述
如果产品属于某个属性集,我希望在产品页面上显示一个静态块.
I wish to display a static block on the product page if the product belongs to a certain Attribute Set.
如果页面的属性集为"Rc",则该想法是在产品页面上显示块,否则不显示块.我有一个自定义主题,已经制作了一个块并显示在所有产品页面上.我只需要在属性页为"Rc"的产品页面上显示该块.我不知道文件夹的结构和/或以下代码是否适用于magento 2.3.我要在哪里来回复制模板文件...基本上是整个九码,如何实现设置和代码.
The idea is to have a block show on product pages if the page has an attribute set of "Rc" else do not show block. I have a custom theme I made and already have a block made and displayed on ALL product pages. I would only need the block showing on product pages with an attribute set of "Rc". I am unaware of the folder structure and/or if the following code is applicable to magento 2.3. Where do i Copy template file to and from... basically the whole nine yards of how to implement the setting and code.
我找到的代码如下(附我的评论):
The code I found is as follows (with my comments):
将此方法添加到产品视图块"从我正在阅读的视图块中,不再是它的现在称为catalog_product_view.xml,其文件夹结构是
"Add this method to the Product View Block" From what i'm reading the view block is no more its now called the catalog_product_view.xml of which the folder structure is
app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml
app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_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;
}
}
然后在app/design/frontend/package/theme/template/catalog/product/view.phtml:中"是不再使用的view/phtml文件,这是正确的文件夹结构.
"Then in app/design/frontend/package/theme/template/catalog/product/view.phtml:" is the view/phtml file no longer used and what is this the correct folder structure.
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)
What i have set up is (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在所有产品中正常工作并显示.
product-rc.phtml is working and showing in all products.
(测试块)文本字符串在phtml文件中.
(test block) text string is in the phtml file.
推荐答案
我明白了.下面的代码对我有用.
I got it. The below code worked for me.
将代码添加到
module-catalog/view/frontend/templates/product/view
module-catalog/view/frontend/templates/product/view
<?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
这篇关于根据产品属性集显示html元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!