【问题标题】:How to check if attribute exist in product attribute set? Magento如何检查产品属性集中是否存在属性? Magento
【发布时间】:2013-01-19 12:09:19
【问题描述】:

如何检查产品属性集中是否存在属性?

我需要知道一个产品的属性集是否有一个属性。

我得到的属性是:

$attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg();

如果产品属性集中存在属性,$attrPricekg 显示:为产品设置值 如果没有为产品设置值,则为 0。

如果产品属性集中不存在该属性,则 $attrPricekg 显示 0。这是我的问题。我需要避免这种情况,我想检查该产品是否不存在该属性。

谢谢。

【问题讨论】:

    标签: magento attributes product catalog


    【解决方案1】:

    现在我将提供一个无论如何都有效的答案!

    $product = Mage::getModel('catalog/product')->load(16);
    
    $eavConfig = Mage::getModel('eav/config');
    /* @var $eavConfig Mage_Eav_Model_Config */
    
    $attributes = $eavConfig->getEntityAttributeCodes(
        Mage_Catalog_Model_Product::ENTITY,
        $product
    );
    
    if (in_array('pricekg',$attributes)) {
        // your logic
    }
    

    【讨论】:

      【解决方案2】:

      要检查产品中是否存在特定属性,即使该属性的值为“null”,它也应该返回 true。

      一种可行的方法是:

      $attr = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product',$code);
      if (null!==$attr->getId())
      

      { //属性存在代码这里 }

      当然也可以写成一行:

      if(null!===Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product','attributecode_to_look_for')->getId()) {
          //'attributecode_to_look_for' exists code here
      }
      

      找到并修改了一下: https://github.com/astorm/Pulsestorm/issues/3

      【讨论】:

      • 这个很一般,整体完美。我需要它来删除一个属性,如果它存在的话。删除现有属性$installer->removeAttribute('catalog_product', 'attributecode_to_look_for'); simple。
      • 感谢您的回答。在为第一个示例的条件添加右括号后,它起作用了!
      【解决方案3】:

      也许这种方式更适合你:

      $attribute = Mage::getModel('catalog/product')->load($productId)->getResource()->getAttribute($attributeCode);
      if ($attribute && $attribute->getId()) { ... }
      

      你也可以试试

      $attributes = $product->getAttributes();
      

      但您可以在属性集合中检查所有内容:

      $entityTypeId = Mage::getModel('eav/entity')
                  ->setType('catalog_product')
                  ->getTypeId();
      $attributeId = 5;
      $attributeSetName   = 'Default';
      $attributeSetId     = Mage::getModel('eav/entity_attribute')
                      ->getCollection()
                      ->addFieldToFilter('entity_type_id', $entityTypeId)
                      ->addFieldToFilter('attribute_set_name', $attributeSetName)
                      ->addFieldToFilter('attribute_id', $attributeId)
                      ->getFirstItem();
      

      可能是源代码需要一些更正,但我想你会理解这个想法。

      在此处查看更多示例 - http://www.blog.magepsycho.com/playing-with-attribute-set-in-magento/

      【讨论】:

      • 第一个解决方案不能正常工作。我需要检查特定的产品。
      • 尝试做$attribute = Mage::getModel('catalog/product')->load(1)->getResource()->getAttribute($attributeCode);
      • 你也可以试试 $attributes = $product->getAttributes();
      • 谢谢。 :) $attributes = $product->getAttributes() 和 foreach 构造帮助了我!!!
      【解决方案4】:

      编辑:这不是正确答案。

      $product->offsetExists('pricekg');
      

      Varien_Object::offsetExists() (link)

      【讨论】:

      • 只有在该属性有值的情况下才会返回 true 吗?或者当 product_entity_attribute_[type] 表中没有记录时,magento 是否会在 $_data 中放入一个值为 NULL 的键?
      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 2018-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多