0

P.S I want to display grand total after store credit is applied because as of current it shows without store credit in it, for example customer applied 200 credit out of 500 purchase so his grand total should be 300$ but instead grand total shows 500$, the correct display is to show 300$ in grand total

I created an observer to change grand total and remove store credit from grand total, my observer is working fine and calculating customer balance and has the quote, however I cannot save the quote, I also tried $quote->collectTotals() but it gives me 502 Nginx error I am not sure how to make the quote understand to change the grand total, although I am setting the grand total I do not see it working on the page: here is the events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_address_collect_totals_before">
    <observer name="customer_get_balance" instance="ImplantDirect\Storecredit\Observer\GetCustomerBalance"/>
</event>
</config>

and here is part of observer execute function:

public function execute(EventObserver $observer)
    {
        /* @var $customer \Magento\Customer\Api\Data\CustomerInterface */
        $quote = $observer->getEvent()->getQuote();
        $customerId = $quote->getCustomer()->getId();
        $websiteId =$this->_storeManager->getStore()->getWebsiteId();
        $balanceModel = $this->_balanceFactory->create()->setCustomerId($customerId)->setWebsiteId($websiteId)->loadByCustomer();
        $amount = $balanceModel->getAmount();
        $grandTotal = $quote->getGrandTotal();
        if($amount>=$grandTotal)
        {
            $quote->setGrandTotal(0);
        }
        else{
            $quote->setGrandTotal($grandTotal-$amount);
        }
    

please explain what can I do to make Quote understand to use the new updated grand total thank you

5
  • uhmm why don't you simply disable the store credit functionality instead?
    – Diana
    Mar 12, 2021 at 7:26
  • I want to decrease store credit from grand total
    – Nickool
    Mar 12, 2021 at 15:48
  • oh ok, now i get it, you simply want to display the grand total without the store credit applied, right?
    – Diana
    Mar 12, 2021 at 15:49
  • @DianaBotean correct
    – Nickool
    Mar 12, 2021 at 15:53
  • actually I want to display it after store credit is applied because as of current it shows without store credit in it, for example customer applied 200 credit out of 500 purchase so his grand total should be 300$ but instead grand total shows 500$, the correct display is to show 300$ in grand total
    – Nickool
    Mar 12, 2021 at 15:55

1 Answer 1

1

You should work with $total instead of $quote.

The reason is you are inside the collectTotals function, it's all about the totals.

For more logic, you can start working with collect/fetch methods. Here is a depth dive article about Totals in Magento https://www.classyllama.com/blog/unravel-magento-collecttotal-intro

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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