0

I am trying to set customer group specific price for product like this:

// Parse group prices
$groupPrices = array();
foreach ($productInfo->Prices as $groupPrice) {
    $groupPrices[] = array(
        'website_id' => 0,
        'cust_group' => intval($groupPrice->GroupId),
        'price' => floatval($groupPrice->GroupPrice)
    );
}

// Load product id by sku
$productId = (int)Mage::getModel('catalog/product')->getIdBySku($productInfo->Sku);
if (!$productId)
    throw new Exception('Product does not exists by SKU: '. $productInfo->Sku);

// Load product
$product = Mage::getModel('catalog/product')
    ->load($productId);

// Clear any previous group prices
$product->setGroupPrice(null);
$product->save();

// Save new group prices
$product->setGroupPrice($groupPrices);
$product->save();

When this code runs, I am getting the following error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '86-1-0-10.0000-0' for key 'E8AB433B9ACB00343ABB312AD2FAB087', query was: INSERT INTO catalog_product_entity_tier_price (entity_id, all_groups, customer_group_id, qty, value, website_id) VALUES (?, ?, ?, ?, ?, ?)

Any ideas what might be wrong?

1

I have found the problem.

If a product already has "Tier Prices" defined, updating customer group prices somehow causes that error I reported above.

So, this is how I have solved it and it's working:

// Parse group prices
$groupPrices = array();
foreach ($productInfo->Prices as $groupPrice) {
    $groupPrices[] = array(
        'website_id' => 0,
        'cust_group' => intval($groupPrice->GroupId),
        'price' => floatval($groupPrice->GroupPrice)
    );
}

// Load product id by sku
$productId = (int)Mage::getModel('catalog/product')->getIdBySku($productInfo->Sku);
if (!$productId)
    throw new Exception('Product does not exists by SKU: '. $productInfo->Sku);

// Load product
$product = Mage::getModel('catalog/product')
    ->load($productId);

// Clear any previous tier prices
$oldTierPrices = Mage::getResourceModel('catalog/product_attribute_backend_tierprice')
    ->loadPriceData($productId, null);
$product->setTierPrice(null);

// Clear any previous group prices
$product->setGroupPrice(null);
$product->save();

// Save new group prices
$product->setGroupPrice($groupPrices);
$product->setTierPrice($oldTierPrices);
$product->save();
0

The composite key E8AB433B9ACB00343ABB312AD2FAB087 consists of entity_id,all_groups,customer_group_id,qty,and website_id, in that order.

When you're saving $product with ID 86, there's already a tier price record with that composite key. This probably means that your "clearing previous group prices" is not working or you're creating $groupPrices incorrectly. I would first check $product->setGroupPrice(null); $product->save(); is actually working. Also, I don't think you need to save() twice.

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.