1

I created the discount programatically by sales_quote_collect_totals_after event.

This discount applies only when cart subtotal greater than 500. This discount works good in cart page. After customer login, this discount is removed automatically.I tried this Refer link, but not working.The below which I used in sales_quote_collect_totals_after event

 $discountAmount = 0;
        $quote = $observer->getQuote();

    $quoteitems = $quote->getAllItems();
    foreach ($quoteitems as $item) {
        /*@var $quoteItem Mage_Sales_Model_Quote_Item */
        $product = $item->getProduct();
        $totalprice += $quote->getBaseSubtotal();
        //exit;

    }

    if($$totalprice>500) {
    $discount = 100;
        $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) {

                    $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->getDiscountAmount()){
                    $address->setDiscountAmount(-($address->getDiscountAmount()-$discountAmount));
                    $address->setDiscountDescription($address->getDiscountDescription().', Free Samples');
                    $address->setBaseDiscountAmount(-($address->getBaseDiscountAmount()-$discountAmount));
                }
                else {
                    $address->setDiscountAmount(-($discountAmount));
                    $address->setDiscountDescription('Free Samples');
                    $address->setBaseDiscountAmount(-($discountAmount));
                }
                $address->save();
            }
        }
    }
3
  • Please share your code to get more insight
    – Dexter
    Commented Nov 29, 2014 at 5:18
  • this code works good and discount is applied in cart page. after customer login discount removed automatically Commented Nov 29, 2014 at 5:28
  • Are you solve this issue?
    – Mahi M
    Commented Jul 27, 2019 at 7:09

1 Answer 1

0

In config.xml , use load_customer_quote_before event

<load_customer_quote_before>
    <observers>
        <custom_checkout_observer>
            <type>singleton</type>
            <class>yourNamespace_yourModule_Model_Observer</class>
            <method>loadCustomerQuote</method>
        </custom_checkout_observer>
    </observers>
</load_customer_quote_before>

In observer.php

  <?php
    class YourNamespace_YourModule_Model_Observer extends Mage_Checkout_Model_Session {

        public function loadCustomerQuote() {

            $quote =Mage::getSingleton('checkout/session')->getQuote();
            $quote->getBillingAddress();
            $quote->getShippingAddress();
            $quote->setCustomer(Mage::getSingleton('customer/session')->getCustomer())
                    ->setTotalsCollectedFlag(false)
                    ->collectTotals()
                    ->save();
            return $this;

        }
}

the above code will set the customer to quote before merge.Now the custom discount works good after merge quote.

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.