In magento's latest version (1.7) there is an option to set customer group prices, however I can't find any documentation on how to do this programmatically. I tried the following code, but it didn't work. Anyone know how to set customer group price?
$_product->setCustomerGroupId($_price->getCustomerGroupId());
$_product->setGroupPrice($price);
$_product->save();
If you are actually using the magento object (not the api), these are the behaviors I have found. Hopefully the application makes sense, regardless of how you are acquiring the data.
// get my product
$product = Mage::getModel('catalog/product')->load(x);
// the group data is expecting an array of arrays that look like..
// array ('website_id'=>y, 'cust_group'=>z, 'price'=>n)
$groupPricingData = array (
// for website 2, customer group 2
array ('website_id'=>2, 'cust_group'=>2, 'price'=>10),
// for all websites, not logged in
array ('website_id'=>0, 'cust_group'=>0, 'price'=>15)
);
$product->setData('group_price',$groupPricingData);
$product->save();
In this example, it would replace all previous group pricing for the product, so like wise.
$product->setData('group_price',array());
$product->save();
will remove group pricing.
Another behavior I noticed related to setting the store id on the product you are updating will cause the group price to append. This way if you are uploading a bunch of group pricing for a specific store, you don't have to worry about loosing the group pricing set against another store.
// add a pricing to store 4
$product = Mage::getModel('catalog/product')->setStoreId(4)->load(1234);
$product->setData('group_price',array (
array (
"website_id" => 3,
"cust_group" => 4,
"price" => 99
)));
$product->save();
// add a pricing to store 1
$product = Mage::getModel('catalog/product')->setStoreId(1)->load(1234);
$product->setData('group_price',array (
array (
"website_id" => 1,
"cust_group" => 2,
"price" => 105
)));
$product->save();
// remove group pricing from store 2
$product = Mage::getModel('catalog/product')->setStoreId(2)->load(1234);
$product->setData('group_price',array ());
$product->save();
This is convenient if you are dealing with batches of group pricing on a single store, but don't what to affect other stores group pricing. I don't know if this is the simplest mechanism, but it has worked so far for me.
Well I finally figured it out, for those who are looking for the solution: you need to make an array of data including website_id, cust_group, price and if needed delete. This is available in the new magento release (v1.7)
$group_prices = array(); if(isset($price_data['delete'])) {
$group_prices[] = array(
"website_id" => Mage::getModel('core/store')->load($price_data['store_id'])->getWebsiteId(),
"cust_group" => $price_data['customer_group_id'],
"all_groups" => false,
"delete" => true
);
} else {
$group_prices[] = array(
"website_id" => Mage::getModel('core/store')->load($price_data['store_id'])->getWebsiteId(),
"cust_group" => $price_data['customer_group_id'],
"all_groups" => false,
"price" => $price_data["price"]
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With