在 magento 中添加自定义选项时出错

Error adding Custom Option in magento

我有这个错误"Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '6152-0-11-0' for key"

在 magento 中以编程方式添加自定义选项时,我使用此功能:

function createCustomOption($value, $customoptions_price, $sku, $title, $type, $noOption = false)
{
$custom_options = array();
if ($type && $value != "" && $value) {
$values = explode(',', $value);
$skus = explode(',', $sku);
$is_required = 1;
$customoptions_prices = explode(',', $customoptions_price);

    if (count($values)) {
        /**If the custom option has options*/
        if (! $noOption) {

            $is_required = 1;
            $sort_order = 0;

            $custom_options[ ] = array(
                'is_delete' => 0,
                'title' => $title,
                'previous_group' => "",
                'previous_type' => "",
                'type' => $type,
                'is_require' => $is_required,
                'sort_order' => $sort_order ,
                'values' => array()
                );

    for($i = 0; $i < (count($values)) ; $i++)
        {
        switch ($type) {

            case 'drop_down':
            case 'radio':
            case 'checkbox':
            case 'multiple':
            default:

            $custom_options[count($custom_options)-1]['values'][ ] = array(

                'is_delete' => 0, 
                'title' => $values[$i], 
                'option_type_id' => –1, 
                'price_type' => 'fixed', 
                'price' => $customoptions_prices[$i], 
                'sku' => $skus[$i], 
                'sort_order' => $i);
                break;
            }
        }
return $custom_options;
}
/**If the custom option doesn't have options | Case: area and field*/
else {

    $is_required = 1;
    $sort_order = 0;
    $custom_options[ ] = array(
    "is_delete" => 0, 
    "title" => $title, 
    "previous_group" => "text", 
    "price_type" => 'fixed', 
    "price" => "0",
    "type" => $type, 
    "is_required" => $is_required
    );
    return $custom_options;
    }
    }
    }
return false;
}

我从这里调用它:

$product = Mage::getModel('catalog/product')->load(6152);

            unset($optionData);

            $precio = 100;

            $custom_title = "Unidad,Caja de 10 unidades";
            $customoptions_price = "0,".$precio."";
            $prod_sku = "unidadesku,cajasku";
            $customoption_main_title = "Seleccione unidades o cajas";
            $option_type = "drop_down";

            $optionData[ ] = createCustomOption($custom_title, $customoptions_price, $prod_sku, $customoption_main_title, $option_type);

            if(count($product->getOptions()) == 0){

            foreach ($optionData as $options) {

                foreach ($options as $option) {

                    $opt = Mage::getModel('catalog/product_option');
                    $opt->setProduct($product);
                    $opt->addOption($option);
                    $opt->saveOptions();

                    }
                }

            $product->setHasOptions(1)->save();

我解决了这个问题,在我的脚本中添加了这一行:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);