以编程方式创建订单时如何使用折扣代码设置折扣金额?

How to set discount amount with discount code when create order programmatically?

我已经实用地创建了一个订单,并在我检查销售下的订单时强行开具发票以完成订单->订单它显示订单已完成但折扣代码始终为零 amount.How 以折扣价显示给定折扣在产品上。

下面是我的代码工作正常,但需要使用折扣代码添加折扣价格。

$customer = Mage::getModel('customer/customer');
    $customer->setStore($store);
    $quote = Mage::getModel('sales/quote');
    $quote->setStore($store);

     $quote->setCustomerEmail($_REQUEST['customerEmail']);
         $addressData = array(
        'firstname' => ' ',
        'lastname' => ' ',
        'street' => ' ',
        'city' => 'NULL',
        'postcode' => 'NULL',
        'telephone' => '123456',
        'country_id' => 'US',
        'region_id' => 12,  );
        $billingAddress = $quote->getBillingAddress()->addData($addressData);
        $shippingAddress = $quote->getShippingAddress()->addData($addressData);



    $productIds=array();
    $products=$_REQUEST['productIds'];
    $products=str_replace(array( '[', ']' ), '',$products);
    $productIds=explode(',',$products);
    $quant=$_REQUEST['quantity'];
    $quant=str_replace(array( '[', ']' ), '',$quant);
    $quantities=explode(',',$quant);
    $salesReport=array_combine($productIds,$quantities);

    foreach($salesReport as $ids=>$qty)
    {
        $product1 = Mage::getModel('catalog/product')->load($ids); /* HTC Touch Diamond */
        $buyInfo1 = array('qty' => $qty);
        $quoteItem=$quote->addProduct($product1, new Varien_Object($buyInfo1));
     }
    $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
        ->setShippingMethod('freeshipping_freeshipping')
        ->setPaymentMethod('ebs');

    $quote->getPayment()->importData(array('method' => 'ebs'));
    $quote->setDiscountAmount('5');
    $quote->setCouponCode("SALE");
    $quote->collectTotals()->save();
    $service = Mage::getModel('sales/service_quote', $quote);
    $service->submitAll();
    $order = $service->getOrder();

问题不是没有应用优惠券代码,而是在结帐时应用了优惠券代码。

基本上,优惠券代码是在 "Cart" 中设置的,而不是在 "Checkout" 中设置的。

要使您的脚本运行正常,您必须进行更多步骤。 1 / 将产品添加到客户的购物车。 2 /应用优惠券代码。 3/ 从之前添加的优惠券代码加载的购物车创建订单。

希望对您有所帮助。

在购物车上应用优惠券代码:

Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode('YOUR COUPON CODE HERE')
->collectTotals()
->save();