1

I cant save the super attribute because i get error

$pricesByAttributeValues = array();
$baseCode = Mage::app()->getBaseCurrencyCode();
$allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies(); 
$rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCode, array_values($allowedCurrencies));
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('name', 'id', 'price','special_price','desde','moneda_base'))
                        ->addAttributeToFilter('type_id', array('eq' => 'configurable'))
                        ->addAttributeToFilter('sku', array('like' => 'Miila-CV'));

foreach ($collectionConfigurable as $_configurableproduct) {
    $_configurableproduct->getId();
    $_configurableproduct->getName();
    $moneda_base = Mage::getModel('catalog/product')->load($_configurableproduct->getId())->getAttributeText('moneda_base');    
    //Get precio moneda base
    foreach ($rates as $rate=>$value){
        if (strcmp ($rate , $moneda_base ) == 0)
            {
                $moneda_base = $value;
            }
    }
    //Precio y Ofertas
    $precio = number_format($_configurableproduct->getPrice() / $moneda_base , 2);
    $descuento = number_format($_configurableproduct->getSpecialPrice() / $moneda_base , 2);
    $desde = number_format($_configurableproduct->getDesde() / $moneda_base , 2);
    $_configurableproduct->setPrice($precio);
    $_configurableproduct->setSpecialPrice($descuento);
    $_configurableproduct->setDesde($desde);
    //Configurables
    $attributes = $_configurableproduct->getTypeInstance(true)->getConfigurableAttributes($_configurableproduct);
    $i = 0;
    foreach ($attributes as $attribute){
    $prices = $attribute->getPrices();
        foreach ($prices as $price){
            $pricesByAttributeValues[$i]['product_super_attribute_id'] = $price['product_super_attribute_id'];
            $pricesByAttributeValues[$i]['value_index'] = $price['value_index'];    
            $pricesByAttributeValues[$i]['label'] = $price['label'];    
            $pricesByAttributeValues[$i]['default_label'] = $price['default_label'];    
            $pricesByAttributeValues[$i]['store_label'] = $price['store_label'];
            $pricesByAttributeValues[$i]['is_percent'] = $price['is_percent'];
            $pricesByAttributeValues[$i]['pricing_value'] = (float)$price['pricing_value'] / 2;
            $pricesByAttributeValues[$i]['use_default_value'] = $price['use_default_value'];
            $pricesByAttributeValues[$i]['value_id'] = $price['value_id'];
            $i++;   
        }
    }
   try{

       $_configurableproduct->setCanSaveConfigurableAttributes(true);   
       $_configurableproduct->setConfigurableAttributesData($pricesByAttributeValues);
       $_configurableproduct->save();
   } catch (Exception $e) {
        echo 'Excepción capturada: ',  $e->getMessage(), "\n";
    }
}

1 Answer 1

0

I think what the exception is saying, is, that you try to add the same super-attribute twice to the product; so, when trying to insert into the database, the unique key throws an error.

For more help, please add some more comments to your code, and please to this in English. You could also clean it up a little; only by first glance, these lines:

$_configurableproduct->getId();
$_configurableproduct->getName();

...don't do anything at all, they're just useless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.