0
votes

I have successfully read Group Price and Tier Price arrays from a product.

But I couldn't set Group Price and Tier Price attributes using the arrays for another product.

This is how a read Group Price and Tier Price array.

$groupPrices = $simpleProduct->getData('group_price');
if (is_null($groupPrices)) {
    $attribute = $simpleProduct->getResource()->getAttribute('group_price');
    if ($attribute) {
        $attribute->getBackend()->afterLoad($simpleProduct);
        $groupPrices = $simpleProduct->getData('group_price');
    }
}

I have tried methods below to set them to another products

$product->setGroupPrice($groupPrices);

$product->setData('tier_price', $tierPrices);

$product->setData('tier_price', $tierPrices)->getResource()->saveAttribute($product, 'tier_price');

None of them worked. Help please!

1

1 Answers

0
votes

After looking into tier price array, I found out, there is a price_id key, which obviously should not be used and copied to another product when you copy tier pricing data from one product to another, so you can just slice the array, remove this key-value pair.

(
    [0] => Array
        (
            [price_id] => 1285  // REMOVE THIS KEY-VALUE PAIR
            [website_id] => 0
            [all_groups] => 0
            [cust_group] => 1
            [price] => 7.0600
            [price_qty] => 2.0000
            [website_price] => 7.0600
        )

    [1] => Array
        (
            [price_id] => 1286  // REMOVE THIS KEY-VALUE PAIR
            [website_id] => 0
            [all_groups] => 0
            [cust_group] => 1
            [price] => 6.9100
            [price_qty] => 5.0000
            [website_price] => 6.9100
        )

)