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.
<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
what is
$crate_products
and the peace of code below?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, ⦠:
—- (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.
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)