Magento更新下拉列表产品属性值

以下代码可让您更新下拉产品属性值。

假设我们已经创建了我们的下拉产品属性test_attribute,并且具有“是”,“否”和空(空)值。并且我们想用“是”值更新此属性。我们需要首先获取attrubite的选项值。在这里,option ['value']作为选项ID保存在eav_attribute_option表中。

<?php 
 
public function attributeOptionIdExists($attributeCode, $attrbuteValue) 
{ 
    $attributeModel = Mage::getModel('eav/entity_attribute'); 
    $attributeSourceTable = Mage::getModel('eav/entity_attribute_source_table'); 
 
    $attributeId = $attributeModel->getIdByCode('catalog_product', $attribu teCode); 
    $attribute = $attributeModel->load($attributeId); 
 
    $attributeTable = $attributeSourceTable->setAttribute($attribute); 
    $options = $attributeSourceTable->getAllOptions(false); 
 
    foreach($options as $option) { 
      if ($option['label'] == $attrbuteValue) { 
          return $option['value']; 
      } 
    } 
    return false; 
} 
 
    $optionId = $this->attributeOptionIdExists('test_attribute', "Yes");
?>

一旦获得属性的选项ID,就可以更新该属性。

使用以下方法,我们还可以更新每个商店视图的任何属性。此方法在Magento中定义,开箱即用,一次可以处理1000-2000个属性更新。第三个参数是如上所述  返回的$ optionId。

<?php 
     function massUpdateProductAttribute($storeViewId, $productId, $attributeCode, $attributeValue)
    {
        // mass update per store view
        $action = Mage::getModel('catalog/resource_product_action');
        /*$storeViewId = array($storeViewId);*/
        $action->updateAttributes(array($productId), array(
            $attributeCode => $attributeValue
        ), $storeViewId);
    }
?>

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论