这里有我对这个问题的解决方案。
我还添加了有关其他解决方案和一些有用技巧的 cmets。
<?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));
?>
之后,您的日志文件中将包含:
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
如果您没有定义团体价格,它将只返回一般价格
因此,每次您更改 GID 时,此函数都会返回正确的客户 GID 价格
$price = $_product->getPriceModel()->getFinalPrice($qty, $_product);
小心使用此函数,因为它只会在您最初设置产品客户组 ID(null 到 $gid)时返回一次正确的价格
$price = $_product->getFinalPrice();
还要小心 Dmitry 的解决方案,因为它返回“原始”产品组价格。这意味着必须为产品定义组价格,并且该价格不受目录规则折扣的影响,因此它可能高于实际最终价格或一般价格。
$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'];
}
其他几个有用的功能可以获取目录规则和购物车规则的特价和折扣
//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 />'; }
有时检查客户是否登录可能很有用
Mage::getSingleton('customer/session')->isLoggedIn()
我希望这会对某人有所帮助。