按ID更新制造商选项值

时间:2014-09-09 04:46:33

标签: magento attributes magento-1.7

我需要更新属性选项标签ID。

我的csv包含manufacturer_idmanufacturer_label

请告诉我们如何以编程方式更新。

$productid = 100;
$productModel = Mage::getModel('catalog/product')->load($productid);
$attr = $productModel->getResource()->getAttribute("manufacturer");
if ($attr->usesSource()) {
    $manufacturer_label = $attr->getSource()->setOptionText("some_value")->save();
}

1 个答案:

答案 0 :(得分:0)

以下是更新数据库中制造商名称的代码

require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();

echo '<pre>';
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
$table = $resource->getTableName('catalog/product');
// $productId = 44;
// $newSku = 'new-sku';
// $query = "UPDATE {$table} SET sku = '{$sku}' WHERE entity_id = "
//              . (int)$productId;
// $writeConnection->query($query);
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;

}


// Set path to CSV file
$csvFile = 'manufacturer.csv';
$csv = readCSV($csvFile);
$filtered_array = array_filter($csv);

foreach ($filtered_array as $key => $value) {
    if($value[0]!='manufacturer_id'){
        // print_r($value);
        $new_name = $value[1];
        $option_id = $value[0];
        // UPDATE  `allday19Mat14`.`eav_attribute_option_value` SET  `value` =  'Cipla 2' WHERE  `eav_attribute_option_value`.`option_id` =102;
        $query = "UPDATE `eav_attribute_option_value` SET `value` = '{$new_name}' WHERE `eav_attribute_option_value`.`option_id` = "
              . (int)$option_id;
              // echo '<br/>';
        $writeConnection->query($query);
    }
}
Mage::getModel('index/process')->load(4)->reindexEverything();
exit();