Display html element based on product attribute set?

こ雲淡風輕ζ 提交于 2020-06-01 05:07:32

问题


I wish to display a static block on the product page if the product belongs to a certain Attribute Set.

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):

"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

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;
    }
}

"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; 

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 is working and showing in all products.

(test block) text string is in the phtml file.


回答1:


I got it. The below code worked for me.

Add code to

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


来源:https://stackoverflow.com/questions/62049171/display-html-element-based-on-product-attribute-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!