2013-04-11 84 views
0

我正試圖與Magento集成一個系統,我想要一種方式將優惠券代碼,當前用戶和購物車發送到Magento並檢索相應折扣(如果適用),所以我不必複製優惠券驗證背後的所有邏輯。Magento以編程方式獲得折扣給予優惠券代碼和產品

我真的很感激它。

我設法做到以下幾點。

$customerId = 1; 
    $couponCode = "TESTCOUPON"; 
    $json = "{ 
       \"cart\":[{ 
        \"listProduct\":[{ 
         \"idReferenceProduct\":15, 
         \"quantity\":1 
        }] 
       }] 
      }"; 
    $jsonDecoded = json_decode($json); 
    $products = $jsonDecoded->cart[0]->listProduct; 

    // ********************************************************* 

    $customerObj = Mage::getModel('customer/customer')->load($customerId); 
    $storeId = $customerObj->getStoreId(); 
    $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj); 
    $storeObj = $quoteObj->getStore()->load($storeId); 
    $quoteObj->setStore($storeObj); 

    foreach ($products as $singleProduct) { 

     $productObj = Mage::getModel('catalog/product'); 
     $productObj->load($singleProduct->idReferenceProduct); 
     echo $productObj->getName(); 
     echo $productObj->getPrice(); 

     try{ 
      $quoteItem = $quoteObj->addProduct($productObj); 
      $quoteItem->setPrice($productObj->getPrice()); 
      $quoteItem->setQty($singleProduct->quantity); 
      $quoteItem->setQuote($quoteObj);          
      $quoteObj->addItem($quoteItem); 

     } catch (exception $e) { 
      echo "error creating quote item "; 
     } 

     $singleProduct->quantity); 
    } 

    try{ 
     $quoteObj->setCouponCode($couponCode); 
    } 
    catch(exception $e){ 
     return "error setting coupon"; 
    } 

    $quoteObj->collectTotals(); 

    var_dump($quoteObj->toArray()); 

和輸出:

{ 
["customer_id"] = > "1" 
["customer_prefix"] = > NULL 
["customer_firstname"] = > "xxxxxx" 
["customer_middlename"] = > NULL 
["customer_lastname"] = > "xxxxxxx" 
["customer_suffix"] = > NULL 
["customer_email"] = > "[email protected]" 
["customer_dob"] = > "1981-03-06 00:00:00" 
["customer_taxvat"] = > NULL 
["customer_gender"] = > "1" 
["customer_group_id"] = > "1" 
["customer_tax_class_id"] = > "3" 
["store_id"] = > "1" 
["coupon_code"] = > "TESTCOUPON" 
["subtotal"] = > float(872.06) 
["base_subtotal"] = > float(872.06) 
["subtotal_with_discount"] = > float(830.92) 
["base_subtotal_with_discount"] = > float(830.92) 
["grand_total"] = > float(929.64) 
["base_grand_total"] = > float(929.64) 
["applied_rule_ids"] = > "1" 
["virtual_items_qty"] = > int(0) 
["taxes_for_items"] = > { 
    [""] = > { 
     [0] = > { 
      ["rates"] = > { 
       [0] = > { 
        ["code"] = > "IVA" 
        ["title"] = > "IVA" 
        ["percent"] = > float(12) 
        ["position"] = > "1" 
        ["priority"] = > "1" 
        ["rule_id"] = > "1" 
       } 
      }["percent"] = > float(12) 
      ["id"] = > "IVA" 
     } 
    } 
}["items_count"] = > int(2) 
["items_qty"] = > float(2) 
["trigger_recollect"] = > int(0) 
["can_apply_msrp"] = > bool(false) 
["totals_collected_flag"] = > bool(true) 
} 

的優惠券折扣應該是5%的折扣。

由於某些原因價格不正確。該產品價格爲516.00,輸出中的小計爲872.06。也只有一個項目和輸出狀態2項。難道我做錯了什麼?

+0

你能詳細說明你的要求嗎?其實我沒有明白你的意思。 – MagentoDiary 2013-04-11 18:06:08

+0

總之,我需要創建一個方法,接受優惠券代碼,購物車和客戶,並返回折扣金額 – jonathanwiesel 2013-04-11 18:19:19

回答

0

傻了,我似乎是以錯誤的方式將產品添加到報價中。這是它現在的工作方式:

 $quoteItem = Mage::getModel('sales/quote_item'); 
     $quoteItem->setProduct($productObj); 
     $quoteItem->setPrice($productObj->getPrice()); 
     $quoteItem->setQty($singleProduct->quantity); 
     $quoteItem->setQuote($quoteObj);          
     $quoteObj->addItem($quoteItem); 
+0

我也有這種情況,我想出了一個類似的實現作爲你的。我的腳本還會返回報價的grand_total。但是這種技術存在問題。我的優惠券代碼僅適用於1個「使用每個客戶」。所以,當我運行我的腳本時,優惠券已用完,並且不再可供特定用戶使用。 我的應用程序首先調用上述腳本。向用戶顯示總計,然後調用實際下訂單的另一個腳本。 你有什麼想法如何解決這個問題? – RHLK 2015-09-15 22:40:48

相關問題