How to get updated cart subtotal in checkout_cart_product_add_after event observer in Magento 2?Cart items...

Is it possible to run Internet Explorer on OS X El Capitan?

Why can't I see bouncing of a switch on an oscilloscope?

Character reincarnated...as a snail

Do infinite dimensional systems make sense?

Why does Kotter return in Welcome Back Kotter?

I'm flying to France today and my passport expires in less than 2 months

Can a vampire attack twice with their claws using Multiattack?

Why do I get two different answers for this counting problem?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?

Do I have a twin with permutated remainders?

Codimension of non-flat locus

Has there ever been an airliner design involving reducing generator load by installing solar panels?

What are these boxed doors outside store fronts in New York?

How is it possible to have an ability score that is less than 3?

What is the word for reserving something for yourself before others do?

What is a clear way to write a bar that has an extra beat?

LaTeX: Why are digits allowed in environments, but forbidden in commands?

Alternative to sending password over mail?

Can you really stack all of this on an Opportunity Attack?

How to determine what difficulty is right for the game?

Is it legal for company to use my work email to pretend I still work there?

Is it possible to do 50 km distance without any previous training?

strTok function (thread safe, supports empty tokens, doesn't change string)



How to get updated cart subtotal in checkout_cart_product_add_after event observer in Magento 2?


Cart items not updating properly in Magento 2Dynamically add extra price on subtotal on cartAdd a product to cart without going through the event checkout_cart_product_add_aftermain.CRITICAL: Plugin class doesn't existSet custom price of product when adding to cart code not workingWhy Getting categories and names on product view page Magento 2 fails?Magento 2: Add a product to the cart programmaticallyMagento 2: toolbar url problems in custom module with custom routerMagento 2: After custom cookie is created all pages default to home pageI have created one field using product form field for my price i want save my field value at product creation time from backend magento2Magento 2.2.5: Add, Update and Delete existing products Custom Options






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I want to add a free product to cart with another product. For that I wrote checkout_cart_product_add_after event observer. My problem is, I don't get the updated cart subtotal(after adding first product, it returns subtotal as 0). This is my observer code.



namespace StwBuyXGetYBuyXGetYObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkRegistry;

class AddfreeProduct implements ObserverInterface
{
protected $_productRepository;
protected $_cart;
protected $formKey;
public function __construct(
MagentoCheckoutModelCart $cart,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataFormFormKey $formKey){
$this->cart = $cart;
$this->formKey = $formKey;
$this->logger = $logger;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$totals = $this->cart->getQuote()->getTotals();
$subtotal = $totals['subtotal']->getValue();
$this->logger->debug($subtotal);
}

}


enter image description here



enter image description here



Any help will be greatly appreciated.










share|improve this question
















bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Discusstion: chat.stackexchange.com/rooms/44527/…

    – Khoa TruongDinh
    Aug 26 '16 at 13:10











  • see this related thread, it may useful magento.stackexchange.com/questions/182356/…

    – Bojjaiah
    Sep 18 '17 at 13:46






  • 4





    Possible duplicate of Cart items not updating properly in Magento 2

    – Teja Bhagavan Kollepara
    Oct 20 '17 at 10:09


















0















I want to add a free product to cart with another product. For that I wrote checkout_cart_product_add_after event observer. My problem is, I don't get the updated cart subtotal(after adding first product, it returns subtotal as 0). This is my observer code.



namespace StwBuyXGetYBuyXGetYObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkRegistry;

class AddfreeProduct implements ObserverInterface
{
protected $_productRepository;
protected $_cart;
protected $formKey;
public function __construct(
MagentoCheckoutModelCart $cart,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataFormFormKey $formKey){
$this->cart = $cart;
$this->formKey = $formKey;
$this->logger = $logger;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$totals = $this->cart->getQuote()->getTotals();
$subtotal = $totals['subtotal']->getValue();
$this->logger->debug($subtotal);
}

}


enter image description here



enter image description here



Any help will be greatly appreciated.










share|improve this question
















bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Discusstion: chat.stackexchange.com/rooms/44527/…

    – Khoa TruongDinh
    Aug 26 '16 at 13:10











  • see this related thread, it may useful magento.stackexchange.com/questions/182356/…

    – Bojjaiah
    Sep 18 '17 at 13:46






  • 4





    Possible duplicate of Cart items not updating properly in Magento 2

    – Teja Bhagavan Kollepara
    Oct 20 '17 at 10:09














0












0








0








I want to add a free product to cart with another product. For that I wrote checkout_cart_product_add_after event observer. My problem is, I don't get the updated cart subtotal(after adding first product, it returns subtotal as 0). This is my observer code.



namespace StwBuyXGetYBuyXGetYObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkRegistry;

class AddfreeProduct implements ObserverInterface
{
protected $_productRepository;
protected $_cart;
protected $formKey;
public function __construct(
MagentoCheckoutModelCart $cart,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataFormFormKey $formKey){
$this->cart = $cart;
$this->formKey = $formKey;
$this->logger = $logger;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$totals = $this->cart->getQuote()->getTotals();
$subtotal = $totals['subtotal']->getValue();
$this->logger->debug($subtotal);
}

}


enter image description here



enter image description here



Any help will be greatly appreciated.










share|improve this question
















I want to add a free product to cart with another product. For that I wrote checkout_cart_product_add_after event observer. My problem is, I don't get the updated cart subtotal(after adding first product, it returns subtotal as 0). This is my observer code.



namespace StwBuyXGetYBuyXGetYObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkAppRequestInterface;
use MagentoFrameworkRegistry;

class AddfreeProduct implements ObserverInterface
{
protected $_productRepository;
protected $_cart;
protected $formKey;
public function __construct(
MagentoCheckoutModelCart $cart,
PsrLogLoggerInterface $logger,
MagentoFrameworkDataFormFormKey $formKey){
$this->cart = $cart;
$this->formKey = $formKey;
$this->logger = $logger;
}
public function execute(MagentoFrameworkEventObserver $observer)
{
$totals = $this->cart->getQuote()->getTotals();
$subtotal = $totals['subtotal']->getValue();
$this->logger->debug($subtotal);
}

}


enter image description here



enter image description here



Any help will be greatly appreciated.







magento2 cart event-observer totals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 '16 at 7:16







Keerthana

















asked Aug 26 '16 at 5:56









KeerthanaKeerthana

169318




169318





bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 5 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Discusstion: chat.stackexchange.com/rooms/44527/…

    – Khoa TruongDinh
    Aug 26 '16 at 13:10











  • see this related thread, it may useful magento.stackexchange.com/questions/182356/…

    – Bojjaiah
    Sep 18 '17 at 13:46






  • 4





    Possible duplicate of Cart items not updating properly in Magento 2

    – Teja Bhagavan Kollepara
    Oct 20 '17 at 10:09



















  • Discusstion: chat.stackexchange.com/rooms/44527/…

    – Khoa TruongDinh
    Aug 26 '16 at 13:10











  • see this related thread, it may useful magento.stackexchange.com/questions/182356/…

    – Bojjaiah
    Sep 18 '17 at 13:46






  • 4





    Possible duplicate of Cart items not updating properly in Magento 2

    – Teja Bhagavan Kollepara
    Oct 20 '17 at 10:09

















Discusstion: chat.stackexchange.com/rooms/44527/…

– Khoa TruongDinh
Aug 26 '16 at 13:10





Discusstion: chat.stackexchange.com/rooms/44527/…

– Khoa TruongDinh
Aug 26 '16 at 13:10













see this related thread, it may useful magento.stackexchange.com/questions/182356/…

– Bojjaiah
Sep 18 '17 at 13:46





see this related thread, it may useful magento.stackexchange.com/questions/182356/…

– Bojjaiah
Sep 18 '17 at 13:46




4




4





Possible duplicate of Cart items not updating properly in Magento 2

– Teja Bhagavan Kollepara
Oct 20 '17 at 10:09





Possible duplicate of Cart items not updating properly in Magento 2

– Teja Bhagavan Kollepara
Oct 20 '17 at 10:09










2 Answers
2






active

oldest

votes


















0














We should use MagentoCheckoutModelSession:



/**
* @var MagentoCheckoutModelSession
*/
protected $_checkoutSession;

public function __construct(
MagentoCheckoutModelSession $checkoutSession,
.....
)
{
$this->_checkoutSession = $checkoutSession;

}


Try to get the totals:



//Get totals
$this->_checkoutSession->getQuote()->getTotals();
//Get sub total.
$this->_checkoutSession->getQuote()->getSubtotal();





share|improve this answer


























  • Thanks Khoa, for your answer. But, my bad luck, it is not working :(

    – Keerthana
    Aug 26 '16 at 6:27











  • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

    – Khoa TruongDinh
    Aug 26 '16 at 6:30











  • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

    – Keerthana
    Aug 26 '16 at 6:35











  • You tried to dump out this data?

    – Khoa TruongDinh
    Aug 26 '16 at 6:38











  • Yes, I have used logger to print this data. But it displays 0.

    – Keerthana
    Aug 26 '16 at 6:42



















0














You can try



$this->_cartTotalRepository using MagentoQuoteModelCartCartTotalRepository

$totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
// Subtotal
$subtotal = (float) $totals->getSubtotal();


Let me know if you have any question.






share|improve this answer
























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "479"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f133164%2fhow-to-get-updated-cart-subtotal-in-checkout-cart-product-add-after-event-observ%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    We should use MagentoCheckoutModelSession:



    /**
    * @var MagentoCheckoutModelSession
    */
    protected $_checkoutSession;

    public function __construct(
    MagentoCheckoutModelSession $checkoutSession,
    .....
    )
    {
    $this->_checkoutSession = $checkoutSession;

    }


    Try to get the totals:



    //Get totals
    $this->_checkoutSession->getQuote()->getTotals();
    //Get sub total.
    $this->_checkoutSession->getQuote()->getSubtotal();





    share|improve this answer


























    • Thanks Khoa, for your answer. But, my bad luck, it is not working :(

      – Keerthana
      Aug 26 '16 at 6:27











    • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

      – Khoa TruongDinh
      Aug 26 '16 at 6:30











    • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

      – Keerthana
      Aug 26 '16 at 6:35











    • You tried to dump out this data?

      – Khoa TruongDinh
      Aug 26 '16 at 6:38











    • Yes, I have used logger to print this data. But it displays 0.

      – Keerthana
      Aug 26 '16 at 6:42
















    0














    We should use MagentoCheckoutModelSession:



    /**
    * @var MagentoCheckoutModelSession
    */
    protected $_checkoutSession;

    public function __construct(
    MagentoCheckoutModelSession $checkoutSession,
    .....
    )
    {
    $this->_checkoutSession = $checkoutSession;

    }


    Try to get the totals:



    //Get totals
    $this->_checkoutSession->getQuote()->getTotals();
    //Get sub total.
    $this->_checkoutSession->getQuote()->getSubtotal();





    share|improve this answer


























    • Thanks Khoa, for your answer. But, my bad luck, it is not working :(

      – Keerthana
      Aug 26 '16 at 6:27











    • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

      – Khoa TruongDinh
      Aug 26 '16 at 6:30











    • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

      – Keerthana
      Aug 26 '16 at 6:35











    • You tried to dump out this data?

      – Khoa TruongDinh
      Aug 26 '16 at 6:38











    • Yes, I have used logger to print this data. But it displays 0.

      – Keerthana
      Aug 26 '16 at 6:42














    0












    0








    0







    We should use MagentoCheckoutModelSession:



    /**
    * @var MagentoCheckoutModelSession
    */
    protected $_checkoutSession;

    public function __construct(
    MagentoCheckoutModelSession $checkoutSession,
    .....
    )
    {
    $this->_checkoutSession = $checkoutSession;

    }


    Try to get the totals:



    //Get totals
    $this->_checkoutSession->getQuote()->getTotals();
    //Get sub total.
    $this->_checkoutSession->getQuote()->getSubtotal();





    share|improve this answer















    We should use MagentoCheckoutModelSession:



    /**
    * @var MagentoCheckoutModelSession
    */
    protected $_checkoutSession;

    public function __construct(
    MagentoCheckoutModelSession $checkoutSession,
    .....
    )
    {
    $this->_checkoutSession = $checkoutSession;

    }


    Try to get the totals:



    //Get totals
    $this->_checkoutSession->getQuote()->getTotals();
    //Get sub total.
    $this->_checkoutSession->getQuote()->getSubtotal();






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 26 '16 at 13:37

























    answered Aug 26 '16 at 6:14









    Khoa TruongDinhKhoa TruongDinh

    22.1k64187




    22.1k64187













    • Thanks Khoa, for your answer. But, my bad luck, it is not working :(

      – Keerthana
      Aug 26 '16 at 6:27











    • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

      – Khoa TruongDinh
      Aug 26 '16 at 6:30











    • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

      – Keerthana
      Aug 26 '16 at 6:35











    • You tried to dump out this data?

      – Khoa TruongDinh
      Aug 26 '16 at 6:38











    • Yes, I have used logger to print this data. But it displays 0.

      – Keerthana
      Aug 26 '16 at 6:42



















    • Thanks Khoa, for your answer. But, my bad luck, it is not working :(

      – Keerthana
      Aug 26 '16 at 6:27











    • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

      – Khoa TruongDinh
      Aug 26 '16 at 6:30











    • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

      – Keerthana
      Aug 26 '16 at 6:35











    • You tried to dump out this data?

      – Khoa TruongDinh
      Aug 26 '16 at 6:38











    • Yes, I have used logger to print this data. But it displays 0.

      – Keerthana
      Aug 26 '16 at 6:42

















    Thanks Khoa, for your answer. But, my bad luck, it is not working :(

    – Keerthana
    Aug 26 '16 at 6:27





    Thanks Khoa, for your answer. But, my bad luck, it is not working :(

    – Keerthana
    Aug 26 '16 at 6:27













    You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

    – Khoa TruongDinh
    Aug 26 '16 at 6:30





    You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee?

    – Khoa TruongDinh
    Aug 26 '16 at 6:30













    I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

    – Keerthana
    Aug 26 '16 at 6:35





    I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.

    – Keerthana
    Aug 26 '16 at 6:35













    You tried to dump out this data?

    – Khoa TruongDinh
    Aug 26 '16 at 6:38





    You tried to dump out this data?

    – Khoa TruongDinh
    Aug 26 '16 at 6:38













    Yes, I have used logger to print this data. But it displays 0.

    – Keerthana
    Aug 26 '16 at 6:42





    Yes, I have used logger to print this data. But it displays 0.

    – Keerthana
    Aug 26 '16 at 6:42













    0














    You can try



    $this->_cartTotalRepository using MagentoQuoteModelCartCartTotalRepository

    $totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
    // Subtotal
    $subtotal = (float) $totals->getSubtotal();


    Let me know if you have any question.






    share|improve this answer




























      0














      You can try



      $this->_cartTotalRepository using MagentoQuoteModelCartCartTotalRepository

      $totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
      // Subtotal
      $subtotal = (float) $totals->getSubtotal();


      Let me know if you have any question.






      share|improve this answer


























        0












        0








        0







        You can try



        $this->_cartTotalRepository using MagentoQuoteModelCartCartTotalRepository

        $totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
        // Subtotal
        $subtotal = (float) $totals->getSubtotal();


        Let me know if you have any question.






        share|improve this answer













        You can try



        $this->_cartTotalRepository using MagentoQuoteModelCartCartTotalRepository

        $totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
        // Subtotal
        $subtotal = (float) $totals->getSubtotal();


        Let me know if you have any question.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 28 '18 at 7:27









        Vu Tran KienVu Tran Kien

        3521224




        3521224






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f133164%2fhow-to-get-updated-cart-subtotal-in-checkout-cart-product-add-after-event-observ%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

            How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

            夢乃愛華...