Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 如何检查产品属性集中是否存在属性?马根托_Magento_Attributes_Product_Catalog - Fatal编程技术网

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

Magento 如何检查产品属性集中是否存在属性?马根托,magento,attributes,product,catalog,Magento,Attributes,Product,Catalog,如何检查产品属性集中是否存在属性 我需要知道一个产品的属性集合是否有属性 我通过以下方式获得属性: $attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg(); 如果属性存在于产品属性集中,$attrPricekg display:为产品设置值 如果没有为产品设置值,则为0 如果产品属性集中不存在该属性,$attrPricekg显示0。这是我的问题。。我需要避

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

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

我通过以下方式获得属性:

$attrPricekg = Mage::getModel('catalog/product')->load($_product->getId())->getPricekg();
如果属性存在于产品属性集中,$attrPricekg display:为产品设置值 如果没有为产品设置值,则为0

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


谢谢。

也许这样对你更合适:

$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();
可能是源代码需要一些更正,但我想你会理解的想法

请参见此处的更多示例-

编辑:这不是正确答案。


请参阅。

现在,我将提供一个不管怎样都有效的答案

$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
}

要检查产品中是否存在特定属性,即使该属性的值为“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
}
找到它并对其进行了一些修改:

第一个解决方案无法正常工作。我需要检查特定产品。尝试执行$attribute=Mage::getModel('catalog/product')->load(1)->getResource()->getAttribute($attributeCode);您也可以尝试$attributes=$product->getAttributes();谢谢。:)$attributes=$product->getAttributes()和foreach构造帮助了我!!!只有当该属性有值时,才会返回true吗?或者,当product_entity_attribute_[type]表中没有记录时,magento是否在$_数据中输入了一个具有空值的键?这是一个通用的、总体上完美的方法。只有当属性存在时,我才需要它来删除它。删除现有属性
$installer->removeAttribute('catalog_product','attributecode_to_look_for')简单。谢谢你的回答。在第一个示例的条件中添加一个结束括号后,它就工作了!
if(null!===Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product','attributecode_to_look_for')->getId()) {
    //'attributecode_to_look_for' exists code here
}