Magento: Get product price given a customer group

女生的网名这么多〃 提交于 2019-12-02 19:50:26

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

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

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.

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();
CarComp

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));
?>
Dmitry R.

If someine is still searching for solution on 1.7.0.2 CE version, such code worked for me(in situation, when I'm using the product object):

$group_id = 4;
$grp_price = $_product->getData('group_price');
echo $grp_price[$group_id]['price'];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!