0

I'm trying to modify my configurable attributes options that I have set in Magento, but upon save I recieve a PDO error saying that it's a duplicate. I haven't been able to find a way to set these through catalog/product, catalog/product_type_configurable_attribute, or catalog/product_type_configurable but this was my best and most understandable attempt. Can anyone tell me the best way to change this data? Thank you!

// Get a collection of all the configurables
$productConfig = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', 'configurable');
foreach($productConfig as $product) {
    // Get an array of all of the configurable products and their options
    $product_attribute_options = $product->getTypeInstance(true)->getConfigurableOptions($product);
    $product_attribute_options = array_shift($product_attribute_options);
    foreach($product_attribute_options as & $child) {
        // Adjust arrays price in here
    }
    // Get a new fresh product just in case the collection product doesn't have everything. 
    $config_product = Mage::getModel('catalog/product');
    $config_product->load($product->getId());
    // Set and save
    $config_product->setCanSaveConfigurableAttributes(true);
    $config_product->setConfigurableAttributesData($product_attribute_options);
    $config_product->save();
}

And here is the PDO error:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2864-0' for key 'UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID'' in /lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#1 /lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /lib/Zend/Db/Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /lib/Zend/Db/Adapter/Abstract.php(479): Zend_Db_Statement->execute(Array)
#4 /lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `ca...', Array)
#5 /lib/Varien/Db/Adapter/Pdo/Mysql.php(419): Zend_Db_Adapter_Pd in /lib/Zend/Db/Statement/Pdo.php on line 234
1

The problem ended up being the array_shift, I ended up extending the foreach loop and it works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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