有谁知道如何从订单中获得目录和购物车价格规则?
我知道我可以通过方法getDiscountPercent()
从订单商品中获得折扣百分比,但我如何获得应用于整个订单的所有规则?
例如,我有一条规则“客户组X可以在商店中获得所有商品20%的折扣”。
现在我想确定用户提交订单时实际应用了哪些规则。我需要这个订单导出界面,我必须提供用户获得的所有折扣。
提前致谢!
答案 0 :(得分:5)
查看sales_flat_order_item
表格。有一个名为applied_rule_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>";
}
}
答案 1 :(得分:4)
/* Example of getting catalog rules applied for specific product */
$productId = 6;
$user = Mage::getSingleton('customer/session')->getCustomer();
$product = Mage::getModel('catalog/product')->load($productId);
$storeId = $product->getStoreId();
$websiteId = Mage::app()->getStore($storeId)->getWebsiteId();
$dateTs = Mage::app()->getLocale()->storeTimeStamp($storeId);
$customerGroupId = $user->getGroupId();
$resource = Mage::getResourceModel('catalogrule/rule');
$rules = $resource->getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId);
答案 2 :(得分:2)
是的,当您使用销售价格计算基准价格时,Magento不会在订单记录中留下任何历史记录,这是一种痛苦。
幸运的是它是开源的,所以你可以根据需要修补它。
我最近编写了一个观察者,它在加载订单记录时触发,以解决这个问题。它将产品线base_price
与产品的当前零售价格进行交叉引用。如果存在不匹配,则会在订单项中添加几个字段,以帮助将此信息公开给正在监听的任何外部脚本(在我们的示例中,是使用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();
}
}
你的模块etc/config.xml
需要告诉magento何时运行你的观察者:
<?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>
确保在app/etc/modules/...
中激活新模块并清除配置缓存。
现在,当您加载订单时,您可以遍历每个商品并检查$item->getFullRetailPrice() --
是否存在,您知道商品是否已售出(好吧,或者自订单以来价格上涨了)被安置)。你仍然不知道为什么,即哪个销售价格规则生效,但对于我们的应用程序,我们并不真正关心,并且将这些信息与订单一起保存将是一个更加困难的模式。
答案 3 :(得分:1)
虽然可能对你有所帮助:
$order = Mage::getModel('sales/order')->load(20569);
echo Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription());
这将打印出已应用于订单的所有折扣规则名称。