Reputation: 129
I'm trying to update the customer group prices in Magento using Mage (as I cannot find a way to do so in the SOAP V2 API). I've found a StackOverflow example, however it doesn't work for me. The code I'm using is as follows:
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$product = Mage::getModel('catalog/product')->load($productID);
$groupPricingData = array(array('price_id'=>1,'website_id'=>0,
'cust_group'=>3, 'price'=>666));
$product->setData('group_price',$groupPricingData);
$product->save();
echo "true";
?>
I get the following error if I use a product that has any customer prices already set. If I try a product with no existing prices, it doesnt error, but the customer group price is not created.
SCREAM: Error suppression ignored for
( ! ) Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with
message 'SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry '24-0-3-0' for key 'CC12C83765B562314470A24F2BDD0F36'' in
C:\wamp\www\magento\app\code\core\Mage\Core\Model\Config.php on line 1348
( ! ) Mage_Eav_Model_Entity_Attribute_Exception: SQLSTATE[23000]:
Integrity constraint violation: 1062 Duplicate entry '24-0-3-0' for
key 'CC12C83765B562314470A24F2BDD0F36' in
C:\wamp\www\magento\app\code\core\Mage\Core\Model\Config.php on line 1348
Upvotes: 2
Views: 3136
Reputation: 2299
I have been scouring the web for an answer to how to properly "update" group price on Magento Product records and have been unsuccessful. Pouring over Magento's source code hasn't gotten me anywhere either. I have however, discovered two solutions on how to clear out the existing group price, so that you do not receive this error.
1 - Direct SQL query to remove the existing group price data
$coreResource = Mage::getSingleton('core/resource');
$conn = $coreResource->getConnection('core_write');
$table_name = $coreResource->getTableName('catalog_product_entity_group_price');
$conn->delete($table_name, array('entity_id = '.$product->getId()));
2- Set the group price for the product to be empty, and save the product.
$product->setData('group_price', array())->save();
Performing either of these (no need to do both) PRIOR to setting your group price data and saving the product, will avoid the Integrity constraint violation, since they effectively remove the existing group price for the product. Approach 1 feels sketchy because we usually do not make direct sql queries and instead rely on model classes to have methods already built in place for us to encapsulate this logic, but in my test environment (Magento 1.9) it works. Approach 2 may seem safer, however it is more resource intensive since it is saving the entire product record, when all we really need to do is update/delete the existing group price. Plus it requires a redundant save since we're "saving to remove" the existing group price.
I recommend re-indexing Product Prices afterwards, as group price data is also stored in 'catalog_product_index_group_price'.
Upvotes: 1
Reputation: 11
Try to use this extension for magmi group prices:
https://github.com/tim-bezhashvyly/magmi-grouped-price-plugin
Upvotes: 1