Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 1.7 - setting customer group price programmatically

Tags:

magento

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();
like image 966
Chris MacLean Avatar asked May 15 '12 14:05

Chris MacLean


2 Answers

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.

like image 147
themedog Avatar answered Oct 02 '22 19:10

themedog


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"]
                                );
                            }
like image 35
Chris MacLean Avatar answered Oct 02 '22 19:10

Chris MacLean