Magento : Display html element based on product attribute set?

标签 magento attributes magento-1.7 if-statement

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

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

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

最佳答案

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

关于Magento : Display html element based on product attribute set?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15109556/

相关文章:

php - Magento 数据库从 1.6 迁移到 1.9 错误

python - 为什么自定义类型接受 Python 中的临时属性(而内置函数不接受)?

search - Magento 搜索总是返回相同的产品

magento - 编辑一个项目后,报价项目选项丢失

ajax - 从 Magento Admin Extension 输出 ajax 数据的最佳方式

Magento - 获取报价项目的子项

c# - 枚举的自定义属性是否危险?

magento - 如何在magento的管理员中以网格形式更改后退按钮的网址?

html - Magento 中的订单成功页面需要为 https ://and not www

android - 如何在代码中创建滑动抽屉?