通过API修改可配置产品的价格调整。
-
16-10-2019 - |
题
我正在尝试修改我在Magento中设置的可配置属性选项,但是保存后,我收到PDO错误,说这是重复的。我找不到通过目录/产品,目录/product_type_configurable_attribute或目录/product_type_configurable设置这些方法的方法,但这是我最好和最可理解的尝试。谁能告诉我更改此数据的最佳方法?谢谢!
// 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();
}
这是PDO错误:
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
解决方案
问题最终是Array_shift,我最终扩展了foreach循环,现在可以正常工作。