4

What I know.

What I have seen on my searches till now is that we can add custom discount to the quote and split the discount among the quote Items based on ratio calculated.

What I want.

But I want to give specific discount on some line Items(quote items) , say a Gift Item included with a product. As it's a gift Item, I want to give the line item 100% discount, such that my row total will be zero and subtotal have the grand total will have the subtotal - discount amount.

The same line item added individually will be a regular product with no discount.

The Problem

I'm not able to give discount amount to the quote item with out making issue existing calculation.

How can I achieve such a scenario in Magento 1.x programatically? Any Ideas? I have tried using the sales_quote_collect_totals_after and sales_quote_address_discount_item observers with no luck.

  • i think you can refer blog.goods-pro.com/1733/… to start – Gopal Patel Dec 8 '16 at 12:41
  • @GopalPatel Thanks for the link, but in my case its a custom observer that adds the gift products to cart from a list of available gifts mentioned in a list, some times the user can choose alternative gifts also. So I can't use the choose "Buy X Get Y" option :( – shebin512 Dec 8 '16 at 14:27
  • Anyone help me, how can i give each product discount(Discount % display ) while creating manual order. – Gem Jul 28 '18 at 8:08
2

Find the Extension for free product.

You have to do changes for your requirement.

Make changes in C4B_Freeproduct_Model_Observer function salesruleValidatorProcess

You have to retrieve sku of that product which you want to add into cart as free product (Way you are retrieving in your curent observer) . This function runs for the all products in the cart.

    public function salesruleValidatorProcess(Varien_Event_Observer $observer)
    {

        /* @var $quote Mage_Sales_Model_Quote */
        $quote = $observer->getEvent()->getQuote();

        /* @var $item Mage_Sales_Model_Quote_Item */
        $item = $observer->getEvent()->getItem();

        /* @var $rule Mage_SalesRule_Model_Rule */
        $rule = $observer->getEvent()->getRule();


        /*if ($rule->getSimpleAction() != C4B_Freeproduct_Model_Consts::ADD_GIFT_ACTION
            || $item->getIsFreeProduct()
            || $rule->getIsApplied())
        {
            return;
        }*/

        if ($rule->getSimpleAction() != C4B_Freeproduct_Model_Consts::ADD_GIFT_ACTION
            || $item->getIsFreeProduct())
        {
            return;
        }

          //$item->geQty();

        try {
            //$qty = (int)$rule->getDiscountAmount();
            $qty = $item->geQty();
            //$skus = static::_getSkuList($rule->getGiftSku());
            $skus = static::_getSkuList( /*< Get your sku here for item having Id $item->getId() > */);

            foreach ($skus as $sku) {
                /** @var Mage_Sales_Model_Quote_Item $freeItem */
                $freeItem = static::_getFreeQuoteItem($rule->getId(), $sku, $item->getStoreId(), $qty);
                $quote->addItem($freeItem);
                $freeItem->setApplyingRule($rule);
            }

            //$rule->setData('is_applied', true);
        } catch (RuntimeException $e) {
            Mage::logException($e);
        }
    }

Create one cartrule from admin and select add gift. Add any dummy SKU as we are not using it anywhere.

  • Thanks for the suggestion. I have checked C4B_Freeproduct module's observers used, and I added a new function that listens on the event "salesrule_validator_process" and set the discounts there and it works. I might need to tweak the code and templates to get the end results in the required format. – shebin512 Dec 9 '16 at 6:55
  • @shebin512 if you find this helpful then mark it as accepted to close this question. – Gopal Patel Dec 9 '16 at 7:06
1

If the user adds the gift item manually and you want to "make it free", you can do this directly with a cart rule, no code needed:

In the "conditions" tab you select the products that trigger the rule, such as: enter image description here Trigger when I have at least 1 product with sku = dummy_8

Now in the "actions" tab: enter image description here Apply 100 "percent of product price discount" only to the product with the SKU you choose, in my case dummy_1. You should also set the maximum amount of products applied, so the customer gets just ONE item for free, doesn't matter how many items they add to the cart.

That should work! You can promote the action using banners and etc, it's quite faster without the need to code.

  • Thanks for the help. In my case the user is not adding the gift items manually. When a product is added to cart an observer / custom function will add gift items from a list of skus. for some gifts the user can choose alternatives also. I used the custom price zero initially, and the calculations where correct, but I'm not able to track the gift Items value in reports. – shebin512 Dec 8 '16 at 14:30
1

I think I got the solution. Thanks @Gopal Patel.

I need to listen to event "salesrule_validator_process"

In your Custom Module's etc/config.xml.

<global>
   .
        .
        .
   <events>
      <salesrule_validator_process>
         <observers>
            <{{ custom_module }}>
               <type>model</type>
               <class>{{custom_module}}/observer</class>
               <method>giftItemDiscount</method>
            </{{ custom_module }}>
         </observers>
      </salesrule_validator_process>
      .
         .
         .
   </events>
   .
        .
        .
</global>

Next add the function giftItemDiscount () in your module's observer.

public function giftItemDiscount(Varien_Event_Observer $observer) {
    $quote = $observer->getEvent()->getQuote();
    $item = $observer->getEvent()->getItem();
    $rule = $observer->getEvent()->getRule();
    /*$qty  = $item->getqty();*/
    $price = $item->getPrice() * $item->getqty();
    if ($item->getIsFreeGiftItem()) {
        $item->setDiscountPercent(100);
        $item->setDiscountAmount($price);
        $item->setBaseDiscountAmount($price);
    } 
}
  • There is issue when you are using the paypal payflow payment method. PayPal gateway rejected the request. Field format error: 10413-The totals of the cart item amounts do not match order amounts. – Vinay Sikarwar Aug 3 '17 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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