How to get updated cart subtotal in checkout_cart_product_add_after event observer in Magento 2? The 2019 Stack Overflow Developer Survey Results Are InCart 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

How to change the limits of integration

Idiomatic way to prevent slicing?

What does "sndry explns" mean in one of the Hitchhiker's guide books?

Why isn't airport relocation done gradually?

Spanish for "widget"

Does a dangling wire really electrocute me if I'm standing in water?

Is domain driven design an anti-SQL pattern?

How to manage monthly salary

Confusion about non-derivable continuous functions

What is a mixture ratio of propellant?

Is three citations per paragraph excessive for undergraduate research paper?

Understanding the implication of what "well-defined" means for the operation in quotient group

How to make payment on the internet without leaving a money trail?

Is bread bad for ducks?

Why is Grand Jury testimony secret?

How was Skylab's orbit inclination chosen?

Where to refill my bottle in India?

I see my dog run

Time travel alters history but people keep saying nothing's changed

What is the best strategy for white in this position?

What is this 4-propeller plane?

How can I create a character who can assume the widest possible range of creature sizes?

Pristine Bit Checking

Why is my p-value correlated to difference between means in two sample tests?



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



The 2019 Stack Overflow Developer Survey Results Are InCart 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
























  • 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
























  • 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












  • 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.2k64187




    22.2k64187












    • 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

            Sum ergo cogito? 1 nng

            三茅街道4182Guuntc Dn precexpngmageondP