微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 为产品设置Magento下拉列表(选择)值

我创建了一个非常大的导入脚本,它将产品从CSV导入到magento.我还有一个解决的问题.

我使用下拉列表来表示属性.不幸的是,我无法为单个产品设置这些属性的值.
我做了什么:

>创建属性集[PHP],
>为此set [PHP]添加了带有值的dropdown属性,
>在适当的属性集中添加新产品,并尝试为我创建的属性设置值.

我尝试了几种方法,这里的one对我来说很好看:

private function setorAddOptionAttribute($product,$arg_attribute,$arg_value) {
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');

    $attribute_code = $attribute_model->getIdByCode('catalog_product',$arg_attribute);

    $attribute = $attribute_model->load($attribute_code);

    $attribute_options_model->setAttribute($attribute);
    $options = $attribute_options_model->getAllOptions(false);

    // determine if this option exists
    $value_exists = false;
    foreach($options as $option) {
        if ($option['label'] == $arg_value) {
            $value_exists = true;
            break;
        }
    }

    // if this option does not exist,add it.
    if (!$value_exists) {
        $attribute->setData('option',array(
            'value' => array(
                'option' => array($arg_value,$arg_value)
            )
        ));
        $attribute->save();
    }


    $product->setData($arg_attribute,$arg_value);
    $product->save();
}

不幸的是它不起作用.有任何想法吗?我正在使用Magento 1.7.0.2

你可以这样做:
$attr_id = $this->attributeValueExists('manufacturer','Samsung');
$product->setData('manufacturer',$attr_id );
$product->save();

public function attributeValueExists($arg_attribute,$arg_value)
{
    $attribute_model        = Mage::getModel('eav/entity_attribute');
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table') ;

    $attribute_code         = $attribute_model->getIdByCode('catalog_product',$arg_attribute);
    $attribute              = $attribute_model->load($attribute_code);

    $attribute_table        = $attribute_options_model->setAttribute($attribute);
    $options                = $attribute_options_model->getAllOptions(false);

    foreach($options as $option)
    {
        if ($option['label'] == $arg_value)
        {
            return $option['value'];
        }
    }

    return false;
}

如果您有任何疑问,请告诉我.

热忱.

原文地址:https://www.jb51.cc/php/133235.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐