Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Get product price given a customer group

On magento 1.7 I created a promotional price rule of 20% discount for "special members" customer group.

I'd like to display both prices. I thought there would be something like

$_product->getPriceByCustomerGroup( $groupId );

Goal

(not logged in):

  • Regular price: $10.99
  • Member price: $5.99

(member logged in):

  • Regular price: $10.99
  • Member price: $5.99
like image 770
AKnox Avatar asked Dec 01 '22 23:12

AKnox


1 Answers

Ok, bit of a muck around, but I think I have it.

You can grab the price for the specific group id (3 in the case below) by calling setCustomerGroupId for the product. The only caveat is that once you call the setCustomerGroupId function you cannot then set the customer group id to a different group and receive the price for that group - it sets the price once and then it won't overwrite it.

In the example below I have a product that has a normal price of $399.99, for all groups except group id 3. For group id 3, I have a Catalog price rule set for a 20% discount.

If I run the code below I get:

product A: 399.99
product B (group id 3): 319.9900
product B (group id 0): 319.9900

Note how the second time I set the customer group it doesn't change the price

$_productA = $this->getProduct();

$_productB = Mage::getModel('catalog/product')->load($_productA->getId());  
$_productB->setCustomerGroupId(3);

echo 'product A: '.$_productA->getFinalPrice().'<br/>';
echo 'product B (group id 3): '.$_productB->getFinalPrice().'<br/>';

$_productB->setCustomerGroupId(0);

echo 'product B (group id 0): '.$_productB->getFinalPrice().'<br/>';

2nd time lucky :)

like image 50
CCBlackburn Avatar answered Dec 04 '22 04:12

CCBlackburn