문제

I have created an attribute using the following install script:

<?php

$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'category_pricebreak', array(
'group'         => 'Display Settings',
'input'         => 'select',
'type'          => 'varchar',
'label'         => 'Price Break',
'option'        => array(
        'values' => array(
            0 => '$10',
            1 => '$50',
            2 => '$100',
            3 => '$250',
            4 => '$500',
            5 => '$1000',
        ),
    ),
'backend'        => 'eav/entity_attribute_backend_array',
'visible'       => 1,
'required'      => 0,
'user_defined' => 1,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$installer->endSetup();
?>

Now I would like to add another option for $25. I would also like to do this with an upgrade script as opposed to directly editing the DB. If possible, I'd also like this new $25 option to appear between the $10 and $50 options in the drop down. From what I've found so far it appears the only way to achieve this is by dumping all the current table data into an array, then basically dropping and recreating the table with the new option, then reinserting all the info back into the table. Is that the approach I have to take or is there a function I can use to simply insert a new option into the existing attribute list? I found this question to be somewhat helpful, but I'd like to make sure the new option appears in a specific order. Is this possible?

도움이 되었습니까?

해결책

I continued to pick away at this one, and found a solution that appears to work correctly for me. I ended up using raw SQL to insert the option, then the value. I have included the code for my upgrade script below in the hopes it helps someone else out along the way:

<?php
$installer = $this;
$installer->startSetup();
function getLastInsertId($tableName, $primaryKey)
   {
    //SELECT MAX(id) FROM table
    $db = Mage::getModel('core/resource')->getConnection('core_read');
    $result = $db->raw_fetchRow("SELECT MAX(`{$primaryKey}`) as LastID FROM `{$tableName}`;");
    return $result['LastID'];
   }

$installer->run("INSERT into eav_attribute_option (attribute_id, sort_order) VALUES (1081, 1);");

$insertId = getLastInsertId('eav_attribute_option', 'option_id');

$installer->run("INSERT INTO eav_attribute_option_value (option_id, store_id, value) VALUES (".$insertId.", 0, '$25');");
    $installer->endSetup();

?>

If anyone sees any problems with doing it this way please let me know!

EDIT I found an issue with this but was able to correct it. The attribute_id value was different between my dev and the production servers. To get around this, I got the attribute_id based on the entity_type_id and the attribute_code like so:

$attributeCode = 'category_pricebreak';
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_category', $attributeCode);
$id = $attribute->getId();

then passed it into the query like so:

$installer->run("INSERT into eav_attribute_option (attribute_id, sort_order) VALUES (".$id.", 1);");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top