2011-02-20 23 views

Trả lời

17

Dưới đây là một đoạn mã mà tôi được sử dụng để thực hiện chính xác tác vụ này. Tạo một mô-đun tùy chỉnh (sử dụng ModuleCreator làm công cụ) và sau đó tạo một mysql4-install-0.1.0.php trong thư mục sql/modulename_setup. Nó nên chứa những điều sau đây (thích nghi với dữ liệu của riêng bạn, tất nhiên!).

$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$installer->startSetup(); 

$aManufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo'); 
$iProductEntityTypeId = Mage::getModel('catalog/product')->getResource()->getTypeId(); 
$aOption = array(); 
$aOption['attribute_id'] = $installer->getAttributeId($iProductEntityTypeId, 'manufacturer'); 

for($iCount=0;$iCount<sizeof($aManufacturers);$iCount++){ 
    $aOption['value']['option'.$iCount][0] = $aManufacturers[$iCount]; 
} 
$installer->addAttributeOption($aOption); 

$installer->endSetup();  

Tài liệu khác trên Magento wiki nếu bạn muốn.

Nếu bạn không muốn làm điều đó trong một mô-đun tùy chỉnh, bạn chỉ có thể tạo một file php mà bắt đầu với:

require_once 'app/Mage.php'; 
umask(0); 
Mage::app('default'); 
+0

làm thế nào để phôi này mà không cần cài đặt? Nó cho một trang web sản xuất. – Tony

+0

nó không hoạt động khi sử dụng như thông báo lỗi của tập lệnh – Tony

+0

? Tôi có thể đảm bảo với bạn rằng nó hoạt động, tôi đã sử dụng chính xác mã đó. Sử dụng trình cài đặt là phương pháp ưu tiên cho các trang web sản xuất. –

8

trả lời của Jonathan là đúng. Nhưng nếu bạn muốn thực hiện nó mà không cần cài đặt tức là trong bất kỳ mã khác, sau đó bạn có thể tìm thấy điều này hữu ích:

thông tin
$arg_attribute = 'manufacturer'; 
$manufacturers = array('Sony','Philips','Samsung','LG','Panasonic','Fujitsu','Daewoo','Grundig','Hitachi','JVC','Pioneer','Teac','Bose','Toshiba','Denon','Onkyo','Sharp','Yamaha','Jamo'); 

$attr_model = Mage::getModel('catalog/resource_eav_attribute'); 
$attr = $attr_model->loadByCode('catalog_product', $arg_attribute); 
$attr_id = $attr->getAttributeId(); 

$option['attribute_id'] = $attr_id; 
foreach ($manufacturers as $key=>$manufacturer) { 
    $option['value'][$key.'_'.$manufacturer][0] = $manufacturer; 
} 

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$setup->addAttributeOption($option); 

có thể tìm thêm here.

1

Quan trọng! (Hy vọng điều này sẽ giúp ai đó, vì tôi đã bị mắc kẹt như 2h với vấn đề này)

Nếu bạn đang sử dụng các ký tự đặc biệt (như ä, ö, ü, ß, ×, ...), hãy đảm bảo mã hóa chúng đúng cách !

array_walk($manufacturers , create_function('&$val', '$val = utf8_encode($val);')); 
+5

Vì tất cả chúng ta đều có PHP/5.3, một phiên bản dễ đọc hơn và nhanh hơn sẽ là '$ manufacturers = array_map ('utf8_encode', $ manufacturers);' –

1

Dưới đây là một cách đơn giản và rất nhanh ....

Xóa một tùy chọn

/* My option value */ 
$value = 'A value'; 
/* Delete an option */ 
$options = array(); 
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID 
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code 
if ($attribute && $attribute->usesSource()) { 
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID from value... 
    if ($option_id) { 
     $options['delete'][$option_id] = true; 
     $attribute->setOption($options)->save(); 
    } 
} 
/* END ! */ 

Thêm hoặc cập nhật một tùy chọn

/* Add/Update an option */ 
$options = array(); 
$entity_type_id = Mage::getModel('eav/entity')->setType('catalog_product')->getTypeId(); // Product Entity Type ID 
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode($entity_type_id, $attribute_code); // Load attribute by code 
if ($attribute && $attribute->usesSource()) { 
    $option_id = $attribute->getSource()->getOptionId($value); // Check Option ID... 
    $options['order'][$option_id] = 10; // Can be removed... Set option order... 
    $options['value'][$option_id] = array(
     0 => $value, // Admin Store - Required ! 
     1 => $value, // Store id 1 - If U want ! Can be removed 
    ); 
    $attribute->setDefault(array($option_id)); /* If you want set option as default value */ 
    $attribute->setOption($options)->save(); /* That's all */ 
} 
/* END ! */ 
2

Tôi đã tạo ra một chức năng để tự động thêm tùy chọn để phân bổ

public function addAttributeOptions($attributeCode, $argValue) 
{ 
    $attribute = Mage::getModel('eav/config') 
     ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode); 


    if ($attribute->usesSource()) { 

     $id = $attribute->getSource()->getOptionId($argValue); 
     if ($id) 
      return $id; 
    } 

    $value = array('value' => array(
      'option' => array(
        ucfirst($argValue), 
        ucfirst($argValue) 
       ) 
     ) 
    ); 

    $attribute->setData('option', $value); 
    $attribute->save(); 

    //return newly created option id 
    $attribute = Mage::getModel('eav/config') 
     ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $attributeCode); 
    if ($attribute->usesSource()) { 
     return $attribute->getSource()->getOptionId($argValue); 
    } 
} 

Bạn có thể thêm một tùy chọn để thuộc tính của bạn bằng cách cung cấp mã số và tùy chọn giá trị

$this->addAttributeOptions('unfiorm_type', 'leotartd') 
0

Trong hướng dẫn của tôi Tôi giải thích làm thế nào để đọc các tùy chọn từ CSV và tạo ra các tùy chọn pro về mặt ngữ pháp.

http://www.pearlbells.co.uk/add-attribute-options-magento-scripts/ vui lòng nhấp vào hướng dẫn cho giải thích thêm

function createAttribute($options) { 
$option = array('attribute_id' => 
Mage::getModel('eav/entity_attribute')->getIdByCode(
    Mage_Catalog_Model_Product::ENTITY, 
    'color' 
    ) 
); 

for ($i = 0; $i < count($options); $i++) { 
    $option['value']['option'.$i][0] = $options[ $i ]; // Store View 
    $option['value']['option'.$i][1] = $options[ $i ]; // Default store view 
    $option['order']['option'.$i] = $i; // Sort Order 

} 

$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); 
$setup->addAttributeOption($option); 
}