magento - 以编程方式添加属性选项无法立即使用

标签 magento caching attributes

我正在通过脚本创建 Magento 属性选项,但我需要能够获取新 ID 并立即在同一脚本中使用它。

目前它并没有提取 ID - 如果我终止脚本并重新启动它,它会选择创建的选项并返回 ID,但不会作为同一脚本的一部分。

这是我正在使用的代码:

   $attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
   if ($attr->usesSource()) {
           $vattr_id = $attr->getSource()->getOptionId($value);
   }else{
           echo "No Source";
           $vattr_id = false;
   }


if($vattr_id){
        return $vattr_id;
}else{

        $attr_model = Mage::getModel('catalog/resource_eav_attribute');
        $attr = $attr_model->loadByCode('catalog_product', $key);
        $attr_id = $attr->getAttributeId();

        $option['attribute_id'] = $attr_id;
        $option['value'][$value][0] = $value;
        $option['value'][$value][1] = $value;

        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($option);
        $attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
        if ($attr->usesSource()) {
               $vattr_id = $attr->getSource()->getOptionId($value);
                echo "AttrID: $vattr_id";
        }

}

运行这个(使用所需的Mage::app()等),创建选项,您可以在Magento后端看到它,但是$vattr_idNULL。如果我重新加载脚本,它就会在第一个 block 中找到它应该的属性选项。

我猜这与 Magento 缓存模型的方式有关,但不确定我需要在哪里清除这些模型?

最佳答案

function getAttributeOptionId($attributeName, $attributeValue) {

    /* @var $attribute Mage_Eav_Model_Entity_Attribute */
    $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);

    // checking attribute code
    if ($attribute->getId()) {
        $source = $attribute->getSource();
        $options = $source->getAllOptions();

        // looking for existing id
        foreach ($options as $optionValue) {
            if ($attributeValue == $optionValue["label"]) {
                return $optionValue["value"];
            }
        }

        // making new option
        $addOptionData = array(
            "attribute_id" => $attribute->getId(),
            "value" => array(array($attributeValue))
        );

        $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
        $setup->addAttributeOption($addOptionData);

        // getting new id
        $attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);
        $source = $attribute->getSource();
        $options = $source->getAllOptions();

        foreach ($options as $optionValue) {
            if ($attributeValue == $optionValue["label"]) {
                return $optionValue["value"];
            }
        }
    }

    return null;
}

echo getAttributeOptionId("brand", "Intel");

关于magento - 以编程方式添加属性选项无法立即使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26733642/

相关文章:

php - magento ajax 不工作

magento - 规范标签 magento

javascript - Nestjs如何控制缓存?

sqlite - 使用 NSPersistentContainer 从 Core Data SQLite 存储回收空间?

c# - MVC3 范围属性 - 不允许值为零

deployment - Magento 部署的最佳实践

php - 当我访问我的 magento 网站时收到 php 错误

android - 缓存 SHOUTcast API 结果

javascript - 如何在 jQuery 属性选择器中将逻辑 OR 与逻辑 AND 结合起来?

python - 枚举Python类属性(不是函数)