Magento在管理中为未选择的属性值设置空值。它会影响前端显示。如何处理?

Magento在管理中为未选择的属性值设置空值。它会影响前端显示。如何处理?,magento,magento-1.7,Magento,Magento 1.7,我创建了一个新属性(type:dropdown),它不是必填字段 此时,每个产品都会在前端显示“我的属性:n/a” 在某些产品中保存任何内容后,magento会在catalog_product_entity_int表中为此属性写入一个空值 但在前端,属性现在显示为“my attribute:No”而不是“N/A” 它看起来像一个bug,因为我在编辑新产品时没有触及属性 有没有办法处理它或者在我的phtml中应用一些规则?实际上这不是一个bug。这是一项功能。 当该属性的表catalog\u pr

我创建了一个新属性(type:dropdown),它不是必填字段

此时,每个产品都会在前端显示“我的属性:n/a

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

但在前端,属性现在显示为“my attribute:No”而不是“N/A”

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


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

实际上这不是一个bug。这是一项功能。
当该属性的表
catalog\u product\u entity\u int
中没有记录时,将显示
N/A

添加属性时,任何产品的该属性都没有值,但一旦保存了具有该属性的产品,就会在表中插入空值(如您所述)。所以
无值
空值
不同
所有的神奇都发生在这里
Mage\u Catalog\u Block\u Product\u View\u 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\u Catalog\u Block\u Product\u Compare\u List::getProductAttributeValue()


相同的系统用于在比较产品列表中显示属性值。

我最终创建了两个观察者。。。一个覆盖来自Mage\u Eav\u Model\u Entity\u Attribute\u Frontend\u Default的getValue,另一个覆盖Mage\u Catalog\u Block\u Product\u View\u 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;
    }
}

谢谢你,马吕斯。我将创建一个模块来覆盖它。但我想它应该被视为一个bug,因为“”并不意味着“不”,它意味着我也没有价值,或者我已经为某些东西设置了“”。例如,如果我创建了一个“保修”属性,但没有为现有产品选择一个值,这并不意味着产品没有保修,而是我目前没有此信息。“你不这么认为吗?”里卡多马汀。我认为你如何看待这个问题很重要。如果你用你自己的方式看问题,你是对的,如果你用Magento团队的方式看问题,他们是对的。对此我真的没有意见。
<?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;
    }   
}