2013-04-11 87 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

相关问题