How to Update Group Price Programmatically Without Product Object Save
-
12-12-2019 - |
Question
To save group price for the products, the following works for me.
$product->setData('group_price', $group_pricing_data);
$product->save();
However, it takes a while for mass updating products. The faster way to save only attributes via saveAttribute() does not seem to work on group price.
$product->setData('group_price', $group_pricing_data);
$product->getResource()->saveAttribute($product, 'group_price');
Is it because group_price is not EAV like the other attributes, and if so, is there any other way to just save only group pricing?
Solution
As far as I know, you can't simply update group price data using saveAttribute
because it has a custom backend model, catalog/product_attribute_backend_groupprice
.
When you load and save a product object, group price's custom resource mode does the work to load and save the data accordingly. If you look at Mage_Catalog_Model_Resource_Product_Attribute_Backend_Groupprice_Abstract
, you will see the methods Magento uses to load and save group price data.
Specifically, savePriceData
shows how to save the data properly. If you want a fast group price update, you can mimic what's done here. Check how $data
looks like, and you have a good place to start.
OTHER TIPS
You can speed up saving group_price attribute like this:
$prices = $product->getData('group_price');
above code return array, next step is to modify this array and last inject as parameter to below:
$product->setGroupPrice($prices)
->getResource()
->getAttribute('group_price')
->getBackend()
->afterSave($product);
5k products 45 ~ 60 sec on docker local environment.
Happy coding, Adam
try to: var_dump($product->setGroupPrice($prices) ->getResource() ->getAttribute('group_price')); and post here