我正試圖與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項。難道我做錯了什麼?
你能詳細說明你的要求嗎?其實我沒有明白你的意思。 – MagentoDiary 2013-04-11 18:06:08
總之,我需要創建一個方法,接受優惠券代碼,購物車和客戶,並返回折扣金額 – jonathanwiesel 2013-04-11 18:19:19