Answer the question
In order to leave comments, you need to log in
How to get the number of items from the cart in Woocommerce by id?
Hello.
Tell me how in Woocommerce you can get the number of products in the cart, on the store page, knowing the id of each product.
Have a function
woocommerce_quantity_input( $args = array( 'input_value' => 0 ) );
$получить_количество_товаров_в_корзине( $id_товара );
Answer the question
In order to leave comments, you need to log in
Mustafo, thanks for your reply. Here's a function I wrote in case it's useful to someone.
function erd_get_cnt_products( $product_id ) {
$cart_content = WC()->cart->cart_contents;
$cnt_products = 0;
if ( $cart_content ) {
foreach ( $cart_content as $cart_item ) {
if ( $cart_item['product_id'] == $product_id ) {
$cnt_products += $cart_item['quantity'];
}
}
}
return $cnt_products;
}
woocommerce_quantity_input( $args = array( 'input_value' => erd_get_cnt_products( $product->id ) ) );
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question