Get the total number of items for a specific product inside the cart and display it within the loop

I’m trying to figure how I can reflect or display the total number of items the user has added on a single item within the loop. This is to indicate the total number of a particular item the user has added inside the cart

I managed to display the text with the help of the documentation but it seems to fetch all the quantities of all the items inside the cart.

Read More
<div class="container">

<?php
    $args = array(
        'post_type' => 'product',
    );

    $crate_products = new WP_Query ( $args );
    if ( $crate_products->have_posts() ) : while ( $crate_products->have_posts() ) :
    $crate_products->the_post();

?>

    <div id="post-<?php the_ID() ?>" class="three columns product-post">
    <?php
        global $woocommerce;
        $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) {
            $_product = $values['product_id']->post;
            echo "<b>" . '</b>  <br> Quantity: '.$values['quantity'].'<br>';
        }
    ?>

    <?php  wc_get_template_part('content', 'product'); ?>

    </div>

    <?php wp_reset_postdata(); ?>

    <?php endwhile; else: ?>

<?php endif; ?>

<?php wp_reset_query(); ?>

Update

Here is the concept I want to produce. When the user adds a particular item with a specific amount inside the cart. It will then show an icon with the total number of amount for that particular item. I want it to be displayed inside the loop.

Here is an image. The number on the top right corner indicates it has 3 potatoes inside the cart

enter image description here

Related posts

2 comments

  1. what is $crate_products and the peace of code below?

    $args = array( 'post_type' => 'product' );
    $crate_products = new WP_Query ( $args );
    if ( $crate_products->have_posts() ) : while ( $crate_products->have_posts() ) : $crate_products->the_post();
    

    With this part of your code is like you want to display all items related to $crate_products. But this has nothing to do with cart…

    Here is the classical cart loop (nearest the same as in your code). I can display all items that are in the cart, with any kind of details or specifications, the quantity, … :

    <table>
    <?php
        $items = WC()->cart->get_cart();
    
        // Here begins the cart loop
        foreach($items as $item => $values) { 
            $_product = $values['data']->post;
            $product_id = $values['product_id']; // product Id
            $product_title = $_product->post_title // product title
            $product_price = get_post_meta($product_id , '_price', true); // product price
            $product_qty = $values['quantity']; // product quantity
            $productDetails = wc_get_product( $product_id ); // product details array
            // Here in this foreach loop you display each product of the cart
    ?>
        <tr>
            <td><?php echo $product_title; ?></td>
            <td><?php echo $product_qty; ?></td>
            <td><?php echo $product_price; ?></td>
        </tr>
    <?php            
        } 
    ?>
    </table>
    

    —- (E d i t) —-

    If you can give the html structure with the common details for one product in the cart.
    After that for your special product, you can put some conditional to display things different. Even for the non special items, you can enable a conditional that disable some display details based on empty elements.

  2. Here’s the simple answer!

    I’ve split into variables in case you want to break it up.

    Put this in the product loop template (content-product.php)

      global $woocommerce;
      global $product;
    
      $curr_prod_id = $product->id;
    
      $items = $woocommerce->cart->get_cart();
    
      foreach($items as $item => $values) {
          if ($values['product_id'] == $curr_prod_id) {
              echo '<div class="cart-quantity">';
              echo 'Quantity: '.$values['quantity'];
              echo '</div>';
          };
      }
    

Comments are closed.