【问题标题】:Magento : Display html element based on product attribute set?Magento:根据产品属性集显示html元素?
【发布时间】:2013-02-27 10:20:27
【问题描述】:

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

例如,如果我是一家时装店,并且我有一个“鞋类”属性集,我只希望静态块在属性集与“鞋类”匹配时显示在产品页面上

我找到了一些输出属性集 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


    【解决方案1】:

    将此方法添加到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; 
    

    【讨论】:

    • 完美 - 非常感谢 :)
    【解决方案2】:

    对于删除关于 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-07
      • 2011-01-15
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-26
      相关资源
      最近更新 更多