加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:40:07 所属栏目:PHP教程 来源:网络整理
导读:我创建了一个非常大的导入脚本,它将产品从CSV导入到magento.我还有一个要解决的问题. 我使用下拉列表来表示属性.不幸的是,我无法为单个产品设置这些属性的值. 我做了什么: 创建属性集[php], 为此set [php]添加了带有值的dropdown属性, 在适当的属性集中添加
我创建了一个非常大的导入脚本,它将产品从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;
}

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

热忱.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读