更新可配置产品引发关于 UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID 的完整性约束违规错误
Updating configurable product throws Integrity constraint violation error about UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID
我有以下使用 Magento 模型创建或更新可配置产品的脚本。
// Method to create or update configurable product with configurable options
function createOrUpdateConfigurableProduct($product_data, $configurable_products_data)
{
// Init product
$product = Mage::getModel('catalog/product');
// Load product id by sky
$product_id = Mage::getModel('catalog/product')
->getIdBySku($product_data['sku']);
// If product already exists
if ($product_id)
{
// Load product
$product->load($product_id);
}
// Set product data
$product
->setTypeId('configurable')
->setAttributeSetId(4)
->setSku($product_data['sku'])
->setName($product_data['name'])
->setDescription($product_data['description'])
->setShortDescription($product_data['description'])
->setMetaTitle($product_data['name'])
->setMetaDescription($product_data['description'])
->setPrice($product_data['price'])
->setTaxClassId(2)
->setWebsiteIDs(array(1))
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => ($product_data['qty'] >= 1 ? 1 : 0)
));
// Workout all the attribute ids used
$used_attribute_ids = array();
foreach ($configurable_products_data as $product_id => $options) {
foreach ($options as $option) {
if (!in_array($option['attribute_id'], $used_attribute_ids)) {
$used_attribute_ids[] = $option['attribute_id'];
}
}
}
// Set configurable attributes data
$product->getTypeInstance()->setUsedProductAttributeIds($used_attribute_ids);
$configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();
$product->setCanSaveConfigurableAttributes(true);
$product->setConfigurableAttributesData($configurableAttributesData);
// Set configurable products data & save
$product->setConfigurableProductsData($configurable_products_data);
$product->save();
}
其中 $configurable_products_data
看起来像这样:
Array
(
[49] => Array
(
[0] => Array
(
[label] => 27 in.
[attribute_id] => 155
[value_index] => 47
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 32L
[attribute_id] => 156
[value_index] => 50
[is_percent] => 0
[pricing_value] => 210
)
)
[50] => Array
(
[0] => Array
(
[label] => 28 in.
[attribute_id] => 155
[value_index] => 48
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 32L
[attribute_id] => 156
[value_index] => 50
[is_percent] => 0
[pricing_value] => 210
)
)
[51] => Array
(
[0] => Array
(
[label] => 28 in.
[attribute_id] => 155
[value_index] => 48
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 34L
[attribute_id] => 156
[value_index] => 51
[is_percent] => 0
[pricing_value] => 210
)
)
)
当此代码 运行s 时,首先 运行 - 它工作正常并创建可配置产品。但是,在第二个 运行 上,它似乎无法更新现有的可配置产品,它抛出以下神秘错误消息:
Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '52-155' for key 'UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID''
有什么想法吗?
+----------------------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+----------------------+------+-----+---------+----------------+
| product_super_attribute_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| product_id | int(10) unsigned | NO | MUL | 0 | |
| attribute_id | smallint(5) unsigned | NO | | 0 | |
| position | smallint(5) unsigned | NO | | 0 | |
+----------------------------+----------------------+------+-----+---------+----------------+
以上是tablecatalog_product_super_attributetable的说明。这 table 保持可配置产品和简单产品之间的关系。您收到此错误是因为它试图添加已映射的相同简单项目。如果您想在不指定新的简单 sku 的情况下进行更新,可能的解决方法是从此 table 中删除相应可配置产品 table 的条目。在您的代码中,错误来自行 -
$product->setConfigurableAttributesData($configurableAttributesData);
在更新之前,您可以添加一行检查您的可配置产品是否是新的。
if ( $product_is_new ) {
$product->setConfigurableAttributesData($configurableAttributesData);
}
我有以下使用 Magento 模型创建或更新可配置产品的脚本。
// Method to create or update configurable product with configurable options
function createOrUpdateConfigurableProduct($product_data, $configurable_products_data)
{
// Init product
$product = Mage::getModel('catalog/product');
// Load product id by sky
$product_id = Mage::getModel('catalog/product')
->getIdBySku($product_data['sku']);
// If product already exists
if ($product_id)
{
// Load product
$product->load($product_id);
}
// Set product data
$product
->setTypeId('configurable')
->setAttributeSetId(4)
->setSku($product_data['sku'])
->setName($product_data['name'])
->setDescription($product_data['description'])
->setShortDescription($product_data['description'])
->setMetaTitle($product_data['name'])
->setMetaDescription($product_data['description'])
->setPrice($product_data['price'])
->setTaxClassId(2)
->setWebsiteIDs(array(1))
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStockData(array(
'use_config_manage_stock' => 0,
'manage_stock' => 1,
'is_in_stock' => ($product_data['qty'] >= 1 ? 1 : 0)
));
// Workout all the attribute ids used
$used_attribute_ids = array();
foreach ($configurable_products_data as $product_id => $options) {
foreach ($options as $option) {
if (!in_array($option['attribute_id'], $used_attribute_ids)) {
$used_attribute_ids[] = $option['attribute_id'];
}
}
}
// Set configurable attributes data
$product->getTypeInstance()->setUsedProductAttributeIds($used_attribute_ids);
$configurableAttributesData = $product->getTypeInstance()->getConfigurableAttributesAsArray();
$product->setCanSaveConfigurableAttributes(true);
$product->setConfigurableAttributesData($configurableAttributesData);
// Set configurable products data & save
$product->setConfigurableProductsData($configurable_products_data);
$product->save();
}
其中 $configurable_products_data
看起来像这样:
Array
(
[49] => Array
(
[0] => Array
(
[label] => 27 in.
[attribute_id] => 155
[value_index] => 47
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 32L
[attribute_id] => 156
[value_index] => 50
[is_percent] => 0
[pricing_value] => 210
)
)
[50] => Array
(
[0] => Array
(
[label] => 28 in.
[attribute_id] => 155
[value_index] => 48
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 32L
[attribute_id] => 156
[value_index] => 50
[is_percent] => 0
[pricing_value] => 210
)
)
[51] => Array
(
[0] => Array
(
[label] => 28 in.
[attribute_id] => 155
[value_index] => 48
[is_percent] => 0
[pricing_value] => 210
)
[1] => Array
(
[label] => 34L
[attribute_id] => 156
[value_index] => 51
[is_percent] => 0
[pricing_value] => 210
)
)
)
当此代码 运行s 时,首先 运行 - 它工作正常并创建可配置产品。但是,在第二个 运行 上,它似乎无法更新现有的可配置产品,它抛出以下神秘错误消息:
Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '52-155' for key 'UNQ_CATALOG_PRODUCT_SUPER_ATTRIBUTE_PRODUCT_ID_ATTRIBUTE_ID''
有什么想法吗?
+----------------------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------------+----------------------+------+-----+---------+----------------+
| product_super_attribute_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| product_id | int(10) unsigned | NO | MUL | 0 | |
| attribute_id | smallint(5) unsigned | NO | | 0 | |
| position | smallint(5) unsigned | NO | | 0 | |
+----------------------------+----------------------+------+-----+---------+----------------+
以上是tablecatalog_product_super_attributetable的说明。这 table 保持可配置产品和简单产品之间的关系。您收到此错误是因为它试图添加已映射的相同简单项目。如果您想在不指定新的简单 sku 的情况下进行更新,可能的解决方法是从此 table 中删除相应可配置产品 table 的条目。在您的代码中,错误来自行 -
$product->setConfigurableAttributesData($configurableAttributesData);
在更新之前,您可以添加一行检查您的可配置产品是否是新的。
if ( $product_is_new ) {
$product->setConfigurableAttributesData($configurableAttributesData);
}