Magento: Get product price given a customer group

21,194

Solution 1

Fished my own wish after hacking on it for a while

$now = Mage::getSingleton('core/date')->timestamp( time() );
$websiteId = Mage::app()->getStore()->getWebsiteId();
$customerGroup = 4;

Mage::getResourceModel('catalogrule/rule')->getRulePrice( $now, $websiteId, $customerGroup, $_productId);

Solution 2

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 :)

Solution 3

Here there is my solution for this problem. I also added my comments about other solutions and some useful tricks.

<?php
//Get the product
$_product = $this->getProduct();
//or load it from the DB by product ID: $_product = Mage::getModel('catalog/product')->load($_id);

$qty = 1; //price for only one item
Mage::log("Current customer GroupID ('0' for Not Logged In): ".Mage::getSingleton('customer/session')->getCustomerGroupId());
Mage::log("Initial Product price GroupID (always = Null): ".$_product->getCustomerGroupId());
Mage::log("Initial Product price: ".$_product->getPriceModel()->getFinalPrice($qty, $_product));

$NonMember_gr=1; //"1" - is default group for logged in customers
$Member_gr=2;
$_product->setCustomerGroupId($NonMember_gr);
$NonMemberPrice = $_product->getPriceModel()->getFinalPrice($qty, $_product);
Mage::log("Product Group changed to: ".$_product->getCustomerGroupId());
Mage::log("Non-member price is: ".$NonMemberPrice);

$_product->setCustomerGroupId($Member_gr);
$MemberPrice = $_product->getPriceModel()->getFinalPrice($qty, $_product);
Mage::log("Product Group changed to: ".$_product->getCustomerGroupId());
Mage::log("Member price is: ".$MemberPrice);

$_product->setCustomerGroupId(Mage::getSingleton('customer/session')->getCustomerGroupId());
Mage::log("Product Group returned to: ".$_product->getCustomerGroupId());
Mage::log("Price returned to: ".$_product->getPriceModel()->getFinalPrice($qty, $_product));
?>

After that you will have in your log file:

2013-12-05T03:01:10+00:00 DEBUG (7): Current customer GroupID ('0' for Not Logged In): 0
2013-12-05T03:01:10+00:00 DEBUG (7): Initial Product price GroupID (always = Null): 
2013-12-05T03:01:10+00:00 DEBUG (7): Initial Product price: 55
2013-12-05T03:01:10+00:00 DEBUG (7): Product Group changed to: 1
2013-12-05T03:01:10+00:00 DEBUG (7): Non-member price is: 40.0000
2013-12-05T03:01:10+00:00 DEBUG (7): Product Group changed to: 2
2013-12-05T03:01:10+00:00 DEBUG (7): Member price is: 25.0000
2013-12-05T03:01:10+00:00 DEBUG (7): Product Group returned to: 0
2013-12-05T03:01:10+00:00 DEBUG (7): Price returned to: 55

If you've got no group price defined it will return just general price

So, this function will return correct price for Customer GID every time you change GID

$price = $_product->getPriceModel()->getFinalPrice($qty, $_product);

Be careful with this function, because it will return the correct price only ONCE, when you initially set up the Product Customer Group ID (null to $gid)

$price = $_product->getFinalPrice();

Also be careful with Dmitry's solution because it returns "raw" product group price. It means that the Group price must be defined for the product and this price wont be affected by catalogue rules discounts, so it could be higher than the actual final price or general price.

$gr_price_arr = $_product->getData('group_price');
foreach ($gr_price_arr as $gr_price){
    echo '<br>CustomerGroup='.$gr_price['cust_group'].' GroupPrice='.$gr_price['price'];
}

A couple of other useful functions to get special prices and duscounts for Catalog Rules and Cart Rules

//get Catalogue Rule by ID
$rule = Mage::getModel('catalogrule/rule')->load($rule_id); 
//get Rule Name and Discount Amount
echo '<br>name: '.$rule->getName().' Amount: '.$rule->getDiscountAmount().'<br>';


//get Cart Rules and Coupon Discounts
$coupon = Mage::getModel('salesrule/rule');
$couponCollection = $coupon->getCollection();
foreach($couponCollection as $c){
    echo 'Code:'.$c->getCode().'--->Discount Amount:'.$c->getDiscountAmount().'<br />'; }

And sometimes it might be useful to check whether customer is logged in

Mage::getSingleton('customer/session')->isLoggedIn()

I hope that will help somebody.

Solution 4

Another solution. If group price will be smaller than main price, function getGroupPrice return main price.

//get product
$product = Mage::getModel('catalog/product')->load($id);

//get group by code
$group = Mage::getModel('customer/group')->load($code, 'customer_group_code');

//get group price
$product->setCustomerGroupId($group->getId());
$price = $product->getGroupPrice();

//get main price
$product->setCustomerGroupId(null);
$price = $product->getPrice();

Solution 5

Heres how you do it. Tested, and working.

<?php
$now = Mage::getSingleton('core/date')->timestamp( time() );
$websiteId = 1; // Choose your store # here
$customerGroup = 4;
$productId = 9369;
$rules = Mage::getResourceModel('catalogrule/rule');
echo $rules->getRulePrice($now, $websiteId, $customerGroup, $productId));
?>
Share:
21,194
AKnox
Author by

AKnox

Updated on July 09, 2022

Comments

  • AKnox
    AKnox almost 2 years

    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
  • AKnox
    AKnox over 11 years
    cool, not a bad solution. loading the product twice is a little yucky but I'll take it!
  • CCBlackburn
    CCBlackburn over 11 years
    Yeah, wish there was a different way - and knowing Magento there is - but I could not find it
  • AKnox
    AKnox over 11 years
    mmm i'm actually trying this out now and can't get the right price after duplicating the product. I'm going to double check that I'm not making a mistake though
  • AKnox
    AKnox over 11 years
    Can't get this to work. even logged in as the customer group and performing this didn't get the right price through. I'm trying to use this in catalog/product/price.phtml What file were you looking at that led you down this track?
  • AKnox
    AKnox over 11 years
    getting closer, but I can't figure out how the date ties in, the db entries seem sporadic. my promotional rule isn't tied to a date. Mage::getResourceModel('catalogrule/rule')->getRulePrice("20‌​12-09-7", $_websiteId, $groupId, $_id);
  • CCBlackburn
    CCBlackburn over 11 years
    I was doing this at the catalog/product/view.phtml level. I've just tried it in the catalog/product/price.phtml and it seems to work fine. I'm not using any date calls, only the setCustomerGroupId one
  • CCBlackburn
    CCBlackburn over 11 years
    Just had another thought - are you sure your rule is set up correctly? The one I was using (from the sample data) had a zero percent discount...which didn't help
  • pspahn
    pspahn over 11 years
    shouldn't Mage::('catalogrule/rule') instead be Mage::getModel('catalogrule/rule') ?
  • CarComp
    CarComp over 11 years
    No, it should be Mage::getResourceModel. getModel is not the right one.
  • clime
    clime almost 11 years
    Thank you, this helped me a lot. Btw. if you want to get fresh price for each call, you can use this: $product->getPriceModel()->getFinalPrice($qty, $product);
  • Egor
    Egor over 7 years
    I know this is very old answer, but i think in you solution you do not take into account any promotional rules.
  • Corgalore
    Corgalore over 7 years
    Wow. This actually works. I never knew the catalogrule module had that method for getting a price. Thank you!
  • Murtuza Zabuawala
    Murtuza Zabuawala over 7 years
    Perfect Answer Appreciate your answer