Magento - get price rules from an order

Does anyone know how to get catalog price and cart price rules from an order?

I know that I can get a percentage of the discount on an order item using the method getDiscountPercent(), but how can I get all the rules that were applied to the entire order?

For example, I have the rule "Customer group X receives 20% of all items in the store."

Now I want to determine what rules were actually applied when the order was sent by the user. I need this for the interface for exporting orders, where I must provide all the discounts that the user received.

Thanks in advance!

+5
source share
4 answers

sales_flat_order_item. applied_rule_ids, , . , .

//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>";
        }

    }
+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);
+4

, - , Magento , , .

, , , .

, , . base_price . , , script, ( script, SAP Magento SOAP API).

- 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() --, , , (, , ). , , .. , , , , .

+2

:

$order = Mage::getModel('sales/order')->load(20569);
echo Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription());

, .

+1

All Articles