0

I have created a custom column 'discounted_total' in quote_address table for discount calculation.

use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
use Magento\SalesRule\Model\CouponFactory;
use Magento\SalesRule\Model\RuleFactory;
use Magento\SalesRule\Api\RuleRepositoryInterfaceFactory;

class DiscountedTotal extends AbstractTotal
{

    protected $couponFactory;
    protected $ruleFactory;
    protected $ruleRepositoryInterface;

    public function __construct(CouponFactory $couponFactory, RuleFactory $ruleFactory, RuleRepositoryInterfaceFactory $ruleRepositoryInterface)
    {
        $this->setCode('discounted_total');
        $this->couponFactory = $couponFactory;
        $this->ruleFactory = $ruleFactory;
        $this->ruleRepositoryInterface = $ruleRepositoryInterface;
    }

    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();
        $items = $shippingAssignment->getItems();
        if (!count($items)) {
            return $this;
        }

        $appliedCouponDiscountAmount = 0;

        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
        $logger->info('ctot');
        $logger->info($quote->getCouponCode());
        $logger->info($total->getDiscountAmount());



        if ($quote->getCouponCode()) {  $logger->info($quote->getCouponCode());
            $ruleId = $this->couponFactory->create()->loadByCode($quote->getCouponCode())->getRuleId();
            if ($ruleId) {
                $rule = $this->ruleFactory->create()->load($ruleId);
                $conditions = json_decode($rule['conditions_serialized'], true);
                if (isset($conditions['conditions'])) {
                    foreach ($conditions['conditions'] as $condition) {
                        if ($condition['attribute'] == 'base_' . $this->getCode()) {
                            $itemDiscountAmount = $address->getRuleDiscountAmount();
                            if($itemDiscountAmount && count($itemDiscountAmount) && isset($itemDiscountAmount[$ruleId])) {
                                $appliedCouponDiscountAmount = round($itemDiscountAmount[$ruleId]);
                                $logger->info($appliedCouponDiscountAmount);
                            }
                        }
                    }
                }
            }
        }

        $discountTotal = $total->getSubtotal() + $total->getDiscountAmount() + $appliedCouponDiscountAmount;
        $baseDiscountTotal = $total->getBaseSubtotal() + $total->getBaseDiscountAmount() + $appliedCouponDiscountAmount;

        $address->setDiscountedTotal($discountTotal);
        $address->setBaseDiscountedTotal($baseDiscountTotal);

        $quote->setDiscountedTotal($discountTotal);
        $quote->setBaseDiscountedTotal($baseDiscountTotal);

        $total->setDiscountedTotal($discountTotal);
        $total->setBaseDiscountedTotal($baseDiscountTotal);

        return $this;
    }
}

I have added discounted total in cart rule condition section and created cart rule like subtotal. It's working fine.

enter image description here

I can get the expected value from this column and I can apply coupon code.

The problem is, when I'm logged in as a customer, my applied coupon code was removed automatically but it shouldn't.

How to solve this.

Thanks.

Your Answer

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

Browse other questions tagged or ask your own question.