gpt4 book ai didi

php - 为产品设置 Magento 下拉(选择)值

转载 作者:可可西里 更新时间:2023-11-01 13:07:21 24 4
gpt4 key购买 nike

我创建了相当大的导入脚本,用于将产品从 CSV 导入到 magento。我还有一个问题要解决。

我对属性使用下拉菜单。不幸的是,我无法为单个产品的这些属性设置值。我做了什么:

  • 创建属性集[php],
  • 为这个集合 [php] 添加了带有值的下拉属性,
  • 在适当的属性集中添加了新产品,并尝试为我创建的属性设置值。

我尝试了几种方法,这里是 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;
}

如果您有任何问题,请告诉我。

亲切的。

关于php - 为产品设置 Magento 下拉(选择)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19212973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com