Php Magento-从订单获取价格规则
有人知道如何从订单中获取目录和购物车价格规则吗 我知道我可以通过方法Php Magento-从订单获取价格规则,php,magento,Php,Magento,有人知道如何从订单中获取目录和购物车价格规则吗 我知道我可以通过方法getDiscountPercent()从订单项目中获取折扣百分比,但是如何获取应用于整个订单的所有规则 例如,我有一条规则“X客户组可以享受商店所有商品20%的折扣” 现在我想确定用户提交订单时实际应用了哪些规则。我需要一个订单导出界面,我必须提供用户得到的所有折扣 提前谢谢 查看sales\u flat\u order\u item表。有一个名为applied\u rule\u ids的字段,它将为您提供应用于该项目的规则的
getDiscountPercent()
从订单项目中获取折扣百分比,但是如何获取应用于整个订单的所有规则
例如,我有一条规则“X客户组可以享受商店所有商品20%的折扣”
现在我想确定用户提交订单时实际应用了哪些规则。我需要一个订单导出界面,我必须提供用户得到的所有折扣
提前谢谢 查看
sales\u flat\u order\u item
表。有一个名为applied\u rule\u ids
的字段,它将为您提供应用于该项目的规则的id。此外,您还可以在此表中了解应用了多少折扣和百分比
范例
//The order I want to check
$order_id = 859;
//Get the list of items for your order
$items = Mage::getModel('sales/order_item')
->getCollection()
->addFilter('order_id',array('eq'=>$order_id));
//loop through each item
foreach($items as $item){
//if the item has not had a rule applied to it skip it
if($item->getAppliedRuleIds() == '')continue;
/*
* I cant remember in the database they might be comma separated or space if multiple rules were applied
* the getAppliedRuleIds() function is the one you want
*/
foreach(explode(",",$item->getAppliedRuleIds()) as $ruleID){
//Load the rule object
$rule = Mage::getModel('catalogrule/rule')->load($ruleID);
// Throw out some information like the rule name what product it was applied to
echo "<p>".$item->getSku()." had rule ".$rule->getName()."(".$item->getAppliedRuleIds().") applied </p>";
}
}
//我要检查的顺序
$order_id=859;
//获取订单的项目列表
$items=Mage::getModel('sales/order_item')
->getCollection()
->addFilter('order_id',array('eq'=>$order_id));
//循环浏览每个项目
foreach($items作为$item){
//如果项目没有应用规则,则跳过它
如果($item->getAppliedRuleIds()='')继续;
/*
*我不记得在数据库中,如果应用了多个规则,它们可能是逗号分隔的或是空格
*getAppliedRuleIds()函数就是您想要的函数
*/
foreach(分解(“,”,$item->getAppliedRuleIds())为$ruleID){
//加载规则对象
$rule=Mage::getModel('catalogrule/rule')->load($ruleID);
//扔掉一些信息,比如规则名称,它应用于什么产品
echo“”$item->getSku()”已应用规则“$rule->getName()”(“$item->getAppliedRuleIds()”)”;
}
}
是的,在使用销售定价时,Magento没有在订单记录中留下任何历史记录,这是一种痛苦,因为它涉及到如何计算基准价格
幸运的是,它是开源的,所以如果您愿意,可以将其修补
我最近写了一个观察者,在加载订单记录时触发,以解决这个问题。它将基准价格
行与产品当前的完整零售价格交叉引用。如果存在不匹配,它会向订单项添加两个字段,以帮助将此信息公开给正在侦听的任何外部脚本(在我们的示例中,是一个使用Magento的SOAP API将订单转发给SAP的自定义订单履行脚本)
以下是基本想法-在app/code/local/YourCo/SalePricing
并在app/code/local/YourCo/SalePricing/Model/Promo/observer.php
<?php
class YourCo_SalePricing_Model_Promo_Observer
{
public function __construct()
{
}
// tag order items that have a sale-price
// with original retail price and total sale discount for this line
public function report_sale_pricing($observer)
{
$event = $observer->getEvent();
$order = $event->getOrder();
$items = $order->getAllItems();
foreach ($items as $item) {
// heads up, you may want to do this for other types as well...
if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
$regular_price = $this->_lookupFullRetail($item,$order->getStoreId());
$sale_price = $item->getBasePrice();
if ($regular_price - $sale_price > 0.005) {
$qty = $item->getQtyOrdered();
$total_sale_discount = ($regular_price * $qty) - ($sale_price * $qty);
$item->setFullRetailPrice((string)$regular_price);
$item->setSaleDiscount((string)$total_sale_discount);
}
}
}
}
private function _lookupFullRetail(&$item,$store_id)
{
$mpid = $item->getProductId();
$p = Mage::getModel('catalog/product')->setStoreId($store_id)->load($mpid);
return $p->getPrice();
}
}
确保在app/etc/modules/…中激活新模块,并清除配置缓存
现在,当您加载订单时,您可以循环查看每个项目并检查
$item->getFullRetailPrice()--
如果它在那里,您就知道该项目正在销售中(好吧,或者是因为该项目或者自下订单以来价格已经上涨)。你仍然不知道为什么,也就是说哪个销售价格规则是有效的,但对于我们的应用程序来说,我们并不真正关心,而要将这些信息与订单一起保存将是一个更加困难的模式。不过,有什么可以帮到你呢:
$order = Mage::getModel('sales/order')->load(20569);
echo Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription());
这将打印出已应用于订单的所有折扣规则名称。您根本无法将目录价格应用于订单。这些规则由价格索引器应用于目录。Quote将此索引价格作为产品价格接收。谢谢,这是可行的,但是使用getAppliedRuleIds()我只获取购物车规则ID(例如优惠券代码规则),而不获取目录规则?
getAppliedRuleIds()
将获取购物车价格规则的逗号分隔列表,因此,为了获得它的名称,您应该加载salesrule/rule
model。类似这样:$rule=Mage::getModel('salesrule/rule')->load($ruleID)代码>这不能回答问题。
<?xml version="1.0"?>
<config>
<global>
<models>
<yourco_salepricing>
<class>YourCo_SalePricing_Model</class>
</yourco_salepricing>
</models>
<events>
<sales_order_load_after>
<observers>
<yourco_salepricing>
<type>singleton</type>
<class>YourCo_SalePricing_Model_Promo_Observer</class>
<method>report_sale_pricing</method>
</yourco_salepricing>
</observers>
</sales_order_load_after>
</events>
</global>
</config>
$order = Mage::getModel('sales/order')->load(20569);
echo Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription());