I am looking for a solution to check the weight in the cart based on meta. for example, it prints: :
- 5kg in your basket is Cold and Hot
- 2.5kg in your basket is other
I use the following loop to check the meta_key:
$category_check = false; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; if ( $cold = $product->get_meta('cold_product') or $hot= $product->get_meta( 'hot_product' ) ) { $category_check = true; break; } }
Then after checking products with a special meta, I want to calculate the weight.
if (category_check ) { echo 'your basket include:'; // I stock in this stage // Show total weight of product $cold and $hot }
Would you please give me some tips regarding create the second loop?
In fact, I need a new variation like $new_weight
to hold the total weight of products that is both $cold
and $hot
.
Advertisement
Answer
Ok, I found the answer and I put it here to share with others:
$cat_check = false; $w= 0; foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $product = $cart_item['data']; $weight=$product->get_weight() * $cart_item['quantity']; if ( $cold = $product->get_meta('cold_product') or $coldsummer = $product->get_meta( 'cold_product_summer' ) ) { $cat_check = true; $w = $w + $weight; } }
Then I use the following part to print it:
if ($cat_check) { echo '<p>your basket include: ' . $w . ' kg special products.</p>'; }