首页 文章

Magento在admin中为未选择的属性值设置空值 . 它会影响前端显示 . 怎么处理呢?

提问于
浏览
0

我创建了一个不属于必填字段的新属性(类型:下拉列表) .

此时,每个产品都显示在前端“ my attribute: n/a ” .

在某些产品中保存任何内容后,magento会在catalog_product_entity_int表中为此属性写入一个空值 .

但是在前端,该属性现在显示为“ my attribute: No " instead of " N / A” .

它看起来像一个bug,因为我在编辑新产品时没有触及属性 .

有没有办法处理它或在我的phtml中应用一些规则?

2 回答

  • 1

    实际上这不是一个bug . 这是一个功能 .
    当该属性的表 catalog_product_entity_int 中没有记录时,将显示 N/A .
    添加属性时,任何产品的该属性都没有值,但只要保存具有该属性的产品,就会在表中插入空值(如您所述) . 所以 no valuenull value 不同 .
    所有的魔力都发生在这里 Mage_Catalog_Block_Product_View_Attributes::getAdditionalData() .
    这些是您感兴趣的行:

    if (!$product->hasData($attribute->getAttributeCode())) { // no value in the database
        $value = Mage::helper('catalog')->__('N/A');
    } elseif ((string)$value == '') { // empty value in the database
        $value = Mage::helper('catalog')->__('No');  
    }
    

    如果要更改任何内容,请覆盖此方法 .
    如果您更改了任何内容,可能需要查看 Mage_Catalog_Block_Product_Compare_List::getProductAttributeValue() .
    相同的系统用于在比较产品列表中显示属性值 .

  • 0

    我最终创建了2个观察者......一个覆盖 Mage_Eav_Model_Entity_Attribute_Frontend_Default 的getValue和其他覆盖 Mage_Catalog_Block_Product_View_Attributes 中的getAdditionalData,如下所示:

    <?php
    class Namespace_Module_Model_Entity_Attribute_Frontend_Default extends Mage_Eav_Model_Entity_Attribute_Frontend_Default{
        public function getValue(Varien_Object $object)
        {
            $value = $object->getData($this->getAttribute()->getAttributeCode());
    
            if (in_array($this->getConfigField('input'), array('select','boolean'))) {
                $valueOption = $this->getOption($value);
                if (!$valueOption) {
                    $opt     = Mage::getModel('eav/entity_attribute_source_boolean');
                    $options = $opt->getAllOptions();
                    if ($options && !is_null($value)) { //added !is_null
                        foreach ($options as $option) {
                            if ($option['value'] == $value ) {
                                $valueOption = $option['label'];
                            }
                        }
                    }
                }
                $value = $valueOption;
            } elseif ($this->getConfigField('input') == 'multiselect') {
                $value = $this->getOption($value);
                if (is_array($value)) {
                    $value = implode(', ', $value);
                }
            }
    
            return $value;
        }
    }
    

    <?php
    class Namespace_Module_Block_Product_View_Attributes extends Mage_Catalog_Block_Product_View_Attributes
    {
        public function getAdditionalData(array $excludeAttr = array())
        {
            $data = array();
            $product = $this->getProduct();
            $attributes = $product->getAttributes();
            foreach ($attributes as $attribute) {
                if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
                    $value = $attribute->getFrontend()->getValue($product);
    
                    if (!$product->hasData($attribute->getAttributeCode()) || (string)$value == '') { //modified
                        $value = Mage::helper('catalog')->__('N/A');
                    } elseif ($attribute->getFrontendInput() == 'price' && is_string($value)) {
                        $value = Mage::app()->getStore()->convertPrice($value, true);
                    }
    
                    if (is_string($value) && strlen($value)) {
                        $data[$attribute->getAttributeCode()] = array(
                            'label' => $attribute->getStoreLabel(),
                            'value' => $value,
                            'code'  => $attribute->getAttributeCode()
                        );
                    }
                }
            }
            return $data;
        }   
    }
    

相关问题