2

I have customized for applying the reward points from a third party Capillary. I want to update the cart total and display by deducting from the reward points after the response from the curl. I have updated the quote in a controller by the code.

protected $cart;
protected $cartHelper;
protected $_customerSession;
protected $_scopeConfig;
protected $_objectManager;
protected $_checkoutSession;
protected $_quoteRepository;
protected $_priceCurrency;

public $fieldId = ["capillary_enable", "capillary_url", "capillary_user", "capillary_password"];
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Checkout\Model\Cart $cart,
    \Magento\Checkout\Helper\Cart $cartHelper,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    \Magento\Framework\Message\ManagerInterface $messageManager
) {
    $this->_customerSession = $customerSession;
    $this->_scopeConfig = $scopeConfig;
    $this->_objectManager = $objectmanager;
    $this->_priceCurrency = $priceCurrency;
    $this->_checkoutSession = $checkoutSession;
    $this->_quoteRepository = $quoteRepository;
    $this->cart = $cart;
    $this->resultPageFactory = $resultPageFactory;
    $this->cartHelper = $cartHelper;
    $this->messageManager = $messageManager;
    return parent::__construct($context);
}

public function updateCartPrice() {

    $quote = $this->cart->getQuote();        
    $quoteid = $quote->getId();
    $discountAmount = 4;
    $label = "Redeemed Points";    
    $total=$quote->getBaseSubtotal();

    $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');     

    foreach ($quote->getAllAddresses() as $address) {
        if ($address->getAddressType() == $canAddItems) {

            $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount() - $discountAmount);
            $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount() - $discountAmount);

            $address->setGrandTotal((float) $address->getGrandTotal() - $discountAmount);
            $address->setBaseGrandTotal((float) $address->getBaseGrandTotal() - $discountAmount);

            if ($address->getDiscountDescription()) {
                $address->setDiscountDescription($address->getDiscountDescription() . ', ' . $label);
                $address->setDiscountAmount(-((float)$address->getDiscountAmount() + $discountAmount));
                $address->setBaseDiscountAmount(-((float)$address->getDiscountAmount() + $discountAmount));
            } else {
                $address->setDiscountDescription($label);

                $address->setDiscountAmount(-($discountAmount));
                $address->setBaseDiscountAmount(-($discountAmount));
            }
            $address->save();
        }
    }
    foreach($quote->getAllItems() as $item){

        $rat=$item->getPriceInclTax()/$total;
        $ratdisc=$discountAmount*$rat;
        $item->setDiscountAmount(($item->getDiscountAmount()+$ratdisc) * $item->getQty());
        $item->setBaseDiscountAmount(($item->getBaseDiscountAmount()+$ratdisc) * $item->getQty())->save();

    }

    $quote->setSubtotalWithDiscount((float) $quote->getSubtotalWithDiscount() - $discountAmount);
    $quote->setBaseSubtotalWithDiscount((float) $quote->getBaseSubtotalWithDiscount() - $discountAmount);
    $quote->setGrandTotal((float) $quote->getGrandTotal() - $discountAmount);
    $quote->setBaseGrandTotal((float) $quote->getBaseGrandTotal() - $discountAmount);


    $quote->setTotalsCollectedFlag(true)->collectTotals()->save();
    $quote->save();

    $this->_checkoutSession->getQuote()->collectTotals()->save();
 }

This is affected in the database.but after refreshing the updated value is coming for the first time and after the api call of

/V1/carts/mine/totals-information

its reverting back to the old details of quote and reflecting in the cart total summary ui. kindly help on this.

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.