2011-10-04 17 views
5

誰かが注文からカタログとカートの価格ルールをどのように取得できるか知っていますか?Magento - 注文から価格ルールを取得

getDiscountPercent()という方法で注文アイテムから割引率を得ることができますが、注文全体に適用されたすべてのルールを取得するにはどうすればよいですか?

たとえば、「顧客グループXは店舗内のすべての商品を20%割引」というルールを持っています。

今、ユーザーが注文を送信したときに実際に適用されたルールを特定したいと考えています。私はユーザーが持っているすべての割引を提供しなければならない注文の輸出インターフェイスのためにこれを必要とします。

ありがとうございます!

+2

注文にカタログ価格が適用されることはありません。これらの規則は、価格インデクサによるカタログに適用されます。見積りは、この索引価格を製品価格として受け取ります。 – Zyava

答えて

4

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

    } 
+4

ありがとう、これは動作しますが、getAppliedRuleIds()を使用すると、カートルールID(例:クーポンコードルール)のみが取得されますが、カタログルールは取得されません。 – Alex

+0

'getAppliedRuleIds()'は、ショッピングカートの価格ルールをコンマで区切ったリストを取得します。そのため、名前を取得するには 'salesrule/rule'モデルを読み込む必要があります。同様に: '$ rule = Mage :: getModel( 'salesrule/rule') - > load($ ruleID);' –

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); 
+0

これは質問に答えません。 – Karl

2

うん、それはあなたがそのベース価格を算出した方法に関しては、販売価格設定を使用するときにMagentoの注文レコードに周りのすべての歴史を残していない痛みのようなものです。

幸運なことにオープンソースなので、好きなときにこれを修正することができます。

私は最近、この非常に問題に対処するために注文記録がロードされたときに発生するオブザーバーを書いた。製品の現在の小売価格でbase_priceという行を相互参照します。不一致がある場合は、オーダー項目に2つのフィールドを追加して、この情報をリッスンしている外部スクリプト(この場合、MagentoのSOAP APIを使用して注文をSAPに送信するカスタム注文履行スクリプト)に公開します。

ここで基本的な考え方だ - app/code/local/YourCo/SalePricing

でモジュールを作成し、あなたのオブザーバーを実行する際のMagentoを伝えるために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ニーズをオブザーバークラスを設定します。

<?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/...で新しいモジュールを有効にして、設定キャッシュをクリアしてくださいe。

今、注文をロードする際に、各アイテムをループして$item->getFullRetailPrice() --を確認することができます。アイテムが販売されていることがわかっていれば(注文が行われてから価格が上がったか、 。あなたはまだなぜ、どの販売価格ルールが効力を発揮したのかわからないのですが、私たちのアプリケーションでは本当に気にしませんでしたし、その情報を注文と一緒に保存することはずっと厳しいものでした。

1

何ががあなたを助けるかもしれない:

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

これは順番に適用されているすべてのディスカウントルール名を出力します。

関連する問題