1

I am adding custom discount to my cart but it seems it is adding 2 times in the cart.

Here is my model file code.

$discount = 10;
$baseDiscount = 10;

$total->addTotalAmount('plandiscount', -$discount);
$total->addBaseTotalAmount('plandiscount', -$baseDiscount);
$total->setPlanDiscount(-$discount);
$total->setBasePlanDiscount(-$baseDiscount);
$quote->setGrandTotal($total->getGrandTotal() - $discount);
$quote->setBaseGrandTotal($total->getBaseGrandTotal() - $baseDiscount);

$quote->setPlanDiscount(-$discount);
$quote->setBasePlanDiscount(-$baseDiscount);

It is really strange that if the value is positive, it is working fine but in case of negative, it is adding 2 times.

7
  • Why you added setPlanDiscount and setBasePlanDiscount twice? that might be causing the issue, please check your code again. Apr 15, 2019 at 6:40
  • No that is not the trouble. The line $total->addTotalAmount('plandiscount', -$discount); is adding the discount. If i comment everything except that line, then also discount will work.
    – Yash Shah
    Apr 15, 2019 at 6:41
  • I'm talking about this line : $quote->setPlanDiscount(-$discount); Apr 15, 2019 at 6:42
  • i understand but that line is not creating trouble, i just checked removing as u said.
    – Yash Shah
    Apr 15, 2019 at 6:43
  • Please check my ans, hope it will help you to fix your issue. Apr 15, 2019 at 6:49

1 Answer 1

1

Please try this way to apply custom discount

<?php
    namespace Vendor\Mymodule\Model\Quote;
    class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
    {
     public function __construct(
     \Magento\Framework\Event\ManagerInterface $eventManager,
     \Magento\Store\Model\StoreManagerInterface $storeManager,
     \Magento\SalesRule\Model\Validator $validator,
     \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
     ) 
     {
     $this->setCode('testdiscount');
     $this->eventManager = $eventManager;
     $this->calculator = $validator;
     $this->storeManager = $storeManager;
     $this->priceCurrency = $priceCurrency;
     }

     public function collect(
     \Magento\Quote\Model\Quote $quote,
     \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
     \Magento\Quote\Model\Quote\Address\Total $total
     )
     {
     parent::collect($quote, $shippingAssignment, $total);
     $address = $shippingAssignment->getShipping()->getAddress();
     $label = 'My Custom Discount';
     $TotalAmount=$total->getSubtotal();
     $TotalAmount=$TotalAmount/10; //Set 10% discount

     $discountAmount ="-".$TotalAmount; 
     $appliedCartDiscount = 0;

    if($total->getDiscountDescription())
     {
     $appliedCartDiscount = $total->getDiscountAmount();
     $discountAmount = $total->getDiscountAmount()+$discountAmount;
     $label = $total->getDiscountDescription().', '.$label;
     } 

     $total->setDiscountDescription($label);
     $total->setDiscountAmount($discountAmount);
     $total->setBaseDiscountAmount($discountAmount);
     $total->setSubtotalWithDiscount($total->getSubtotal() + $discountAmount);
     $total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() + $discountAmount);

     if(isset($appliedCartDiscount))
     {
     $total->addTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
     $total->addBaseTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
     } 
     else 
     {
     $total->addTotalAmount($this->getCode(), $discountAmount);
     $total->addBaseTotalAmount($this->getCode(), $discountAmount);
     }
     return $this;
     }

     public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
     {
     $result = null;
     $amount = $total->getDiscountAmount();

     if ($amount != 0)
     { 
     $description = $total->getDiscountDescription();
     $result = [
     'code' => $this->getCode(),
     'title' => strlen($description) ? __('Discount (%1)', $description) : __('Discount'),
     'value' => $amount
     ];
     }
     return $result;
     }
    }
8
  • Not working, in any case $appliedCartDiscount returning 0.
    – Yash Shah
    Apr 15, 2019 at 6:52
  • I have added my code which I have used in one of my project and its working fine for me, so please try this way and let me know if any issue. Apr 15, 2019 at 7:02
  • i applied your code and is working with default magento discount, but if i take it as seprate discount column, its not working.
    – Yash Shah
    Apr 15, 2019 at 7:13
  • as per your question you need to add custom discount code and now you asking for extra discount option right ? Apr 15, 2019 at 8:08
  • yes, i checked and found that it is working fine in community edition. It is not working for EE only.
    – Yash Shah
    Apr 15, 2019 at 8:56

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.