在报价和订单中创建订单magento中设置自定义折扣金额

时间:2015-01-09 08:48:53

标签: magento discount

我想在从后端创建订单时向用户提供自定义折扣,并且我使用了来自http://pradeepkumarrcs.blogspot.in/2011/12/applying-custom-discount-amount-in.html的代码,并且它在报价中应用折扣工作正常,但它没有更新订单中的折扣。 它显示了购物车中的折扣金额,但是当我创建订单时,它显示零折扣。我需要做什么额外的事情吗? 我使用的功能是: -

public function setDiscount($quote,$quoteid)
{
   $discountAmount=10;
if($quoteid) {
    if($discountAmount>0) {
        $total=$quote->getBaseSubtotal();
        $quote->setSubtotal(0);
        $quote->setBaseSubtotal(0);

        $quote->setSubtotalWithDiscount(0);
        $quote->setBaseSubtotalWithDiscount(0);

        $quote->setGrandTotal(0);
        $quote->setBaseGrandTotal(0);


        $canAddItems = $quote->isVirtual()? ('billing') : ('shipping');    
        foreach ($quote->getAllAddresses() as $address) {

        $address->setSubtotal(0);
        $address->setBaseSubtotal(0);

        $address->setGrandTotal(0);
        $address->setBaseGrandTotal(0);

        $address->collectTotals();

        $quote->setSubtotal((float) $quote->getSubtotal() + $address->getSubtotal());
        $quote->setBaseSubtotal((float) $quote->getBaseSubtotal() + $address->getBaseSubtotal());

        $quote->setSubtotalWithDiscount(
            (float) $quote->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount()
        );
        $quote->setBaseSubtotalWithDiscount(
            (float) $quote->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount()
        );

        $quote->setGrandTotal((float) $quote->getGrandTotal() + $address->getGrandTotal());
        $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() + $address->getBaseGrandTotal());

        $quote ->save(); 

           $quote->setGrandTotal($quote->getBaseSubtotal()-$discountAmount)
           ->setBaseGrandTotal($quote->getBaseSubtotal()-$discountAmount)
           ->setSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
           ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal()-$discountAmount)
           ->save(); 


            if($address->getAddressType()==$canAddItems) {
            //echo $address->setDiscountAmount; exit;
                $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount()-$discountAmount);
                $address->setGrandTotal((float) $address->getGrandTotal()-$discountAmount);
                $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount()-$discountAmount);
                $address->setBaseGrandTotal((float) $address->getBaseGrandTotal()-$discountAmount);
                if($address->getDiscountDescription()){
                $address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
                $address->setDiscountDescription($address->getDiscountDescription().', Custom Discount');
                $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                }else {
                $address->setDiscountAmount(-($discountAmount));
                $address->setDiscountDescription('Custom Discount');
                $address->setBaseDiscountAmount(-($discountAmount));
                }
                $address->save();
            }//end: if
        } //end: foreach
        //echo $quote->getGrandTotal();

    foreach($quote->getAllItems() as $item){
             //We apply discount amount based on the ratio between the GrandTotal and the RowTotal
             $rat=$item->getPriceInclTax()/$total;
             $ratdisc=$discountAmount*$rat;
             $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
             $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();

           }


        }

    }
 }

提前致谢。

0 个答案:

没有答案