Magento .NET Framework Framework 2.0 根据产品属性集显示html元素?

问题描述 投票:3回答:2

我希望在产品页面上显示一个静态块,如果产品属于某个属性集的话

例如,如果我是一家时装店,我的属性集是 "鞋类",我只希望在属性集符合 "鞋类 "的时候,静态块才会显示在产品页面上。

我找到了一点代码,可以输出属性集的ID,但我想把它变成一个 else if语句。

<?php

$entityTypeId = Mage::getModel('eav/entity')
                ->setType('catalog_product')
                ->getTypeId();
$attributeSetName   = 'Footwear';
$attributeSetId     = Mage::getModel('eav/entity_attribute_set')
                    ->getCollection()
                    ->setEntityTypeFilter($entityTypeId)
                    ->addFieldToFilter('attribute_set_name', $attributeSetName)
                    ->getFirstItem()
                    ->getAttributeSetId();
echo $attributeSetId;

?>

谁有什么好办法?

G

magento attributes magento-1.7 if-statement
2个回答
2
投票

将此方法添加到 Product View Block

(不对核心文件 app/code/core/Mage/Catalog/Block/Product/View.php 当然是)。)

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:

if($this->checkAttributeSet($_product, 'Monitors')):
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('monitor')->toHtml();
elseif($this->checkAttributeSet($_product, 'Footwear')):
    echo $this->getLayout()->createBlock('cms/block')->setBlockId('footwear')->toHtml();
endif; 

1
投票

对于那些删除有用信息的管理员来说,STACKOVERFLOW上的积分>>>>>> 第三次回复这个帖子的更新材料,你知道只是以防有人偶然发现这个帖子。

这是更新后的magento 2.3完成这个任务的方式。

添加代码

模块-目录视图前端模板产品视图。

        <?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
© www.soinside.com 2019 - 2024. All rights reserved.