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

use*_*779 7 attributes product catalog magento

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

我需要知道产品是否具有其属性集的属性.

我得到的属性:

$attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg();
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢.

ben*_*rks 28

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

$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
}
Run Code Online (Sandbox Code Playgroud)


tom*_*omg 6

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

一种有效的方法是:

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

{//属性存在代码}

它当然也可以写成一行:

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

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


ben*_*rks -4

编辑:这不是正确的答案。

$product->offsetExists('pricekg');
Run Code Online (Sandbox Code Playgroud)

请参阅Varien_Object::offsetExists()(链接)