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





.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 23 hours 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 23 hours 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 23 hours 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 23 hours 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

            What other Star Trek series did the main TNG cast show up in?

            Berlina muro

            Berlina aerponto