0

I have created a module to edit the products. Everything is working fine except the percentage discount rule. When a customer changed the item quantity from 5 to 1, the discount amount was not changed in the order.

Guys could you help me in this.

1 Answer 1

1

You gotta resort to the Rewrite:

  1. /app/code/local/YourCompany/YourModule/etc/config.xml

    <global>
        <models>
            <salesrule>
                <rewrite>                    
                    <quote_discount>YourCompany_YourModule_Model_SalesRule_Quote_Discount</quote_discount>
                </rewrite>
             </salesrule>
         </models>
    </global>
    
  2. /app/code/local/YourCompany/YourModule/Model/SalesRule/Quote/Discount.php

    class YourCompany_YourModule_Model_SalesRule_Quote_Discount extends Mage_SalesRule_Model_Quote_Discount
    {
       /**
        * @param Mage_Sales_Model_Quote_Item_Abstract $item
        * @return Mage_SalesRule_Model_Quote_Discount
        */
       protected function _aggregateItemDiscount($item) {
           $this->transferOldItemDiscount($item); //transfer old discount
           return parent::_aggregateItemDiscount($item);
       }
    
       /**
        * @param $item
        * @return $this
        */
       protected function transferOldItemDiscount($item) {
           if (!Mage::helper('mageworx_ordersedit')->isEnabled()) {
               return $this;
           }
           $orderId = Mage::getSingleton('adminhtml/session_quote')->getOrderId();
           if (!$orderId) {
               return $this;
           }
    
           /** @var Mage_Sales_Model_Order $order */
           $order = Mage::getModel('sales/order')->load($orderId);
           if (!$order->getId()) {
               return $this;
           }
    
           $data = Mage::app()->getRequest()->getPost('order');
           if (isset($data['coupon']['code']) && empty($data['coupon']['code'])) {
               Mage::getSingleton('adminhtml/session_quote')->setCouponCodeIsDeleted(true);
           }
    
           /** @var Mage_Sales_Model_Quote $quote */
           $quote = $item->getQuote();
           // if new coupon or deleted - discount do not touch
           if (Mage::getSingleton('adminhtml/session_quote')->getCouponCodeIsDeleted() || ($quote->getCouponCode() && $quote->getCouponCode()!=$order->getCouponCode())) {
               return $this;
           }
    
           $quote->setAppliedRuleIds($order->getAppliedRuleIds());
    
           $orderItems = $order->getAllItems();
           /** @var Mage_Sales_Model_Order_Item $orderItem */
           foreach ($orderItems as $orderItem) {
               if ($orderItem->getProductId()==$item->getProductId()) {
                   if ($orderItem->getAppliedRuleIds() && $orderItem->getBaseDiscountAmount()>0) {
                       $baseDiscount = ($orderItem->getBaseDiscountAmount() / $orderItem->getQtyOrdered()) * $item->getQty();
                       $discount = ($orderItem->getDiscountAmount() / $orderItem->getQtyOrdered()) * $item->getQty();
                       $item->setBaseDiscountAmount($baseDiscount)->setDiscountAmount($discount)->setAppliedRuleIds($orderItem->getAppliedRuleIds());
                   } else {
                       $item->setBaseDiscountAmount(0)->setDiscountAmount(0)->setAppliedRuleIds(null);
                   }
                   return $this;
               }
           }
       }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.