Magento - get price rules from order

后端 未结 4 1196
感动是毒
感动是毒 2020-12-15 01:41

does anyone know how one can get the catalog- and cart price rules from an order?

I know that I can get the discount percentage from an order item via the method

4条回答
  •  隐瞒了意图╮
    2020-12-15 02:31

    Yeah, it's kind of a pain that Magento doesn't leave any history around in the order record when you use sale pricing with regard to how that base price was calculated.

    Fortunately it's open source, so you can patch this in if you like.

    I recently wrote an observer that fires when the order record is loaded, to address this very issue. It cross-references the line base_price with the product's current full-retail price. If there is a mismatch, it adds a couple fields to the order item to help expose this info to any external script that's listening (in our case, a custom order fulfillment script that relays orders to SAP using Magento's SOAP API).

    Here's the basic idea - make a module in app/code/local/YourCo/SalePricing

    and set up an observer class in app/code/local/YourCo/SalePricing/Model/Promo/Observer.php

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

    Your module's etc/config.xml needs to tell magento when to run your observer:

    
    
        
            
              
                YourCo_SalePricing_Model
              
            
            
              
                
                  
                    singleton
                    YourCo_SalePricing_Model_Promo_Observer
                    report_sale_pricing
                  
                
              
            
        
    
    

    Make sure to activate your new module in app/etc/modules/... and clear the config cache.

    Now, when you load the order, you can loop over each item and check for $item->getFullRetailPrice() -- if it's there, you know the item was on sale (well, either that or the price has gone up since the order was placed). You still don't know why, ie which sale price rule was in force, but for our application we didn't really care, and getting that info to be saved with the order would have been a much tougher mod.

提交回复
热议问题