Skip to content

Magento 2 How to update quote totals information from admin

Magento 2.4.5-p1 version using.

We want to give custom discount via our custom module

Using this code –

<?php
namespace NamespceModulenameControllerAdminhtmlQuote;
use MagentoBackendAppActionContext;
use MagentoFrameworkControllerResultFactory;
use MagentoQuoteApiDataShippingAssignmentInterface;
use MagentoQuoteApiDataTotalItemInterface;
use MagentoSalesRuleModelRuleFactory;
use MagentoSalesRuleApiRuleRepositoryInterface;
use MagentoQuoteModelQuoteAddressTotal;
use MagentoFrameworkAppResourceConnection;
use MagentoFrameworkDBTransactionFactory;

class CustomFullDiscount extends MagentoBackendAppAction
{
    private $jsonResultFactory;

    protected $magentoQuoteFactory;

    protected $ruleFactory;

    protected $ruleRepository;

    protected $quoteRepository;

    protected $_resource;

    protected $_dbConnection;

    protected $transactionFactory;


    public function __construct(
        Context $context,
        MagentoFrameworkControllerResultJsonFactory $jsonResultFactory,
        MagentoQuoteModelQuoteFactory $magentoQuoteFactory,
        RuleFactory $ruleFactory,
        RuleRepositoryInterface $ruleRepository,
        MagentoQuoteModelQuoteAddressTotal $total,
        MagentoQuoteApiDataShippingAssignmentInterface $shippingAssignment,
        MagentoQuoteApiCartRepositoryInterface $quoteRepository,
        ResourceConnection $resource,
        TransactionFactory $transactionFactory
    ) {
        parent::__construct($context);
        $this->jsonResultFactory = $jsonResultFactory;
        $this->magentoQuoteFactory = $magentoQuoteFactory;
        $this->ruleFactory = $ruleFactory;
        $this->ruleRepository = $ruleRepository;
        $this->quoteRepository = $quoteRepository;
        $this->_resource        = $resource;
        $this->_dbConnection    = $this->_resource->getConnection();
        $this->transactionFactory = $transactionFactory;
    }

    /**
     * @return MagentoBackendModelViewResultRedirect|void
     */
    public function execute()
    {
        /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        
        try {
            $data = (array)$this->getRequest()->getPost();
            $custom_full_discount = $data['custom_full_discount'];
            $quote_id = $data['quote_id'];
            if ($data) {
                $connection =  $this->_dbConnection;
                $transaction = $this->transactionFactory->create();
                try{
                    $quote = $this->quoteRepository->get($quote_id);
                    $quote->setCustomDiscountAmount($custom_full_discount);
                    $quote->setBaseCustomDiscountAmount($custom_full_discount);
                    $quote->setSubtotal($quote->getSubtotal());
                    $quote->setBaseSubtotal($quote->getBaseSubtotal());
                    $quote->setSubtotalWithDiscount($quote->getSubtotalWithDiscount() - $custom_full_discount);
                    $quote->setBaseSubtotalWithDiscount($quote->getBaseSubtotalWithDiscount() - $custom_full_discount);
                    $quote->setGrandTotal($quote->getGrandTotal() - $custom_full_discount);
                    $quote->setBaseGrandTotal($quote->getBaseGrandTotal() - $custom_full_discount);
                    $this->quoteRepository->save($quote);
                    $transaction->addObject($quote)->save();
                    }
                    catch (Exception $e) {
                        throw $e;
                        $data = ['message'=>$e->getMessage(),'response' => 'Error', 'status' => '0'];
                        $result = $this->jsonResultFactory->create();
                        $result->setData($data);
                        return $result;
                    }
               
                $data = ['message' => 'Updated','response' => 'Success', 'status' => '1'];
                $result = $this->jsonResultFactory->create();
                $result->setData($data);
                return $result;
            }
        }catch (Exception $e) {
                $data = ['message'=>$e->getMessage(),'response' => 'Error', 'status' => '0'];
                $result = $this->jsonResultFactory->create();
                $result->setData($data);
                return $result;
        }
        return $resultRedirect;
    }
}

But the quote is not updating the quote table.

How to Update the quote like subtotal,grand total,add custom discount ?