Magento 2: Get qty of added product in cart












0














I want to get total quantity of product added in cart by product ID.
I use MagentoCheckoutModelCart class to get all items in cart.



$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
if ($item->getProductId() == '101') {
return $item->getQty(); //Get product qty
}
}


This will get product qty added in cart. But if I have 30 products in cart it will loop 30 times.



Is there any way to get qty of single product without looping all products from cart?



Any help appreciated!










share|improve this question
















bumped to the homepage by Community 2 days ago


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















  • If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
    – Pavan Kumar
    Jul 6 '17 at 7:15






  • 1




    @PavanKumar Can you provide code.
    – Prince Patel
    Jul 6 '17 at 7:31
















0














I want to get total quantity of product added in cart by product ID.
I use MagentoCheckoutModelCart class to get all items in cart.



$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
if ($item->getProductId() == '101') {
return $item->getQty(); //Get product qty
}
}


This will get product qty added in cart. But if I have 30 products in cart it will loop 30 times.



Is there any way to get qty of single product without looping all products from cart?



Any help appreciated!










share|improve this question
















bumped to the homepage by Community 2 days ago


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















  • If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
    – Pavan Kumar
    Jul 6 '17 at 7:15






  • 1




    @PavanKumar Can you provide code.
    – Prince Patel
    Jul 6 '17 at 7:31














0












0








0







I want to get total quantity of product added in cart by product ID.
I use MagentoCheckoutModelCart class to get all items in cart.



$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
if ($item->getProductId() == '101') {
return $item->getQty(); //Get product qty
}
}


This will get product qty added in cart. But if I have 30 products in cart it will loop 30 times.



Is there any way to get qty of single product without looping all products from cart?



Any help appreciated!










share|improve this question















I want to get total quantity of product added in cart by product ID.
I use MagentoCheckoutModelCart class to get all items in cart.



$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
if ($item->getProductId() == '101') {
return $item->getQty(); //Get product qty
}
}


This will get product qty added in cart. But if I have 30 products in cart it will loop 30 times.



Is there any way to get qty of single product without looping all products from cart?



Any help appreciated!







magento2 product cart qty






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 6 '17 at 9:58







Prince Patel

















asked Jul 6 '17 at 7:08









Prince PatelPrince Patel

13.2k54675




13.2k54675





bumped to the homepage by Community 2 days 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 2 days ago


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














  • If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
    – Pavan Kumar
    Jul 6 '17 at 7:15






  • 1




    @PavanKumar Can you provide code.
    – Prince Patel
    Jul 6 '17 at 7:31


















  • If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
    – Pavan Kumar
    Jul 6 '17 at 7:15






  • 1




    @PavanKumar Can you provide code.
    – Prince Patel
    Jul 6 '17 at 7:31
















If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
– Pavan Kumar
Jul 6 '17 at 7:15




If u can get the total collection of cart items, then u can sort it by using required Product ID. From that you can get the required quantity..
– Pavan Kumar
Jul 6 '17 at 7:15




1




1




@PavanKumar Can you provide code.
– Prince Patel
Jul 6 '17 at 7:31




@PavanKumar Can you provide code.
– Prince Patel
Jul 6 '17 at 7:31










2 Answers
2






active

oldest

votes


















0














You can get item qty from bellow code it will check for the product in current item collection and return qty if product is available...



$items = $cart->getQuote()->getAllItems();
$productId="your_product_id";
foreach ( $items as $item) {
if ($item->getProductId() == $productId) {
return $item->getQty();
}
}





share|improve this answer





















  • I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
    – Prince Patel
    Jul 6 '17 at 7:29










  • it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
    – Emipro Technologies Pvt. Ltd.
    Jul 6 '17 at 7:32










  • I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
    – Prince Patel
    Jul 6 '17 at 8:02





















0














You can directly get the total qty of quote without using foreach by using this method



$quote->getItemsQty();



If you want to check in order object then you can get it by,



$order->getTotalQtyOrdered()



Where $quote is MagentoQuoteModelQuote object and



$order is MagentoSalesModelOrder object.






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%2f182303%2fmagento-2-get-qty-of-added-product-in-cart%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














    You can get item qty from bellow code it will check for the product in current item collection and return qty if product is available...



    $items = $cart->getQuote()->getAllItems();
    $productId="your_product_id";
    foreach ( $items as $item) {
    if ($item->getProductId() == $productId) {
    return $item->getQty();
    }
    }





    share|improve this answer





















    • I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
      – Prince Patel
      Jul 6 '17 at 7:29










    • it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
      – Emipro Technologies Pvt. Ltd.
      Jul 6 '17 at 7:32










    • I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
      – Prince Patel
      Jul 6 '17 at 8:02


















    0














    You can get item qty from bellow code it will check for the product in current item collection and return qty if product is available...



    $items = $cart->getQuote()->getAllItems();
    $productId="your_product_id";
    foreach ( $items as $item) {
    if ($item->getProductId() == $productId) {
    return $item->getQty();
    }
    }





    share|improve this answer





















    • I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
      – Prince Patel
      Jul 6 '17 at 7:29










    • it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
      – Emipro Technologies Pvt. Ltd.
      Jul 6 '17 at 7:32










    • I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
      – Prince Patel
      Jul 6 '17 at 8:02
















    0












    0








    0






    You can get item qty from bellow code it will check for the product in current item collection and return qty if product is available...



    $items = $cart->getQuote()->getAllItems();
    $productId="your_product_id";
    foreach ( $items as $item) {
    if ($item->getProductId() == $productId) {
    return $item->getQty();
    }
    }





    share|improve this answer












    You can get item qty from bellow code it will check for the product in current item collection and return qty if product is available...



    $items = $cart->getQuote()->getAllItems();
    $productId="your_product_id";
    foreach ( $items as $item) {
    if ($item->getProductId() == $productId) {
    return $item->getQty();
    }
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 6 '17 at 7:25









    Emipro Technologies Pvt. Ltd.Emipro Technologies Pvt. Ltd.

    2,4541823




    2,4541823












    • I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
      – Prince Patel
      Jul 6 '17 at 7:29










    • it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
      – Emipro Technologies Pvt. Ltd.
      Jul 6 '17 at 7:32










    • I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
      – Prince Patel
      Jul 6 '17 at 8:02




















    • I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
      – Prince Patel
      Jul 6 '17 at 7:29










    • it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
      – Emipro Technologies Pvt. Ltd.
      Jul 6 '17 at 7:32










    • I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
      – Prince Patel
      Jul 6 '17 at 8:02


















    I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
    – Prince Patel
    Jul 6 '17 at 7:29




    I know how to get all products from cart. I already declare in question. I want to get product qty by product ID.
    – Prince Patel
    Jul 6 '17 at 7:29












    it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
    – Emipro Technologies Pvt. Ltd.
    Jul 6 '17 at 7:32




    it will return you product Qty.. check the code I have added condition for check productID and if product ID match with the Item collection Product ID it will return only product Qty not full Item collection
    – Emipro Technologies Pvt. Ltd.
    Jul 6 '17 at 7:32












    I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
    – Prince Patel
    Jul 6 '17 at 8:02






    I mean $cart->getQuote()->getAllItems() this function get all items from cart and we need foreach to load each item. but I don't won't foreach to load all items. For ex. I have 50 product in cart then it will loop 50 times.
    – Prince Patel
    Jul 6 '17 at 8:02















    0














    You can directly get the total qty of quote without using foreach by using this method



    $quote->getItemsQty();



    If you want to check in order object then you can get it by,



    $order->getTotalQtyOrdered()



    Where $quote is MagentoQuoteModelQuote object and



    $order is MagentoSalesModelOrder object.






    share|improve this answer




























      0














      You can directly get the total qty of quote without using foreach by using this method



      $quote->getItemsQty();



      If you want to check in order object then you can get it by,



      $order->getTotalQtyOrdered()



      Where $quote is MagentoQuoteModelQuote object and



      $order is MagentoSalesModelOrder object.






      share|improve this answer


























        0












        0








        0






        You can directly get the total qty of quote without using foreach by using this method



        $quote->getItemsQty();



        If you want to check in order object then you can get it by,



        $order->getTotalQtyOrdered()



        Where $quote is MagentoQuoteModelQuote object and



        $order is MagentoSalesModelOrder object.






        share|improve this answer














        You can directly get the total qty of quote without using foreach by using this method



        $quote->getItemsQty();



        If you want to check in order object then you can get it by,



        $order->getTotalQtyOrdered()



        Where $quote is MagentoQuoteModelQuote object and



        $order is MagentoSalesModelOrder object.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 6 '18 at 10:09









        Sourabh Kumar Sharma

        657316




        657316










        answered Jul 6 '17 at 9:46









        Keyur ShahKeyur Shah

        12.9k23764




        12.9k23764






























            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%2f182303%2fmagento-2-get-qty-of-added-product-in-cart%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

            1300-talet

            1300-talet

            Has there ever been an instance of an active nuclear power plant within or near a war zone?