このコードで:
foreach ( WC()->cart->get_cart() as $cart_item ) {
$quantity = $cart_item['quantity'];
echo $quantity;
}
カートに追加されたすべての商品の数量を取得できますが、特定の商品に必要です。
これを試してみてください:
// Set here your product ID
$targeted_id = 24;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if($cart_item['product_id'] == $targeted_id ){
$qty = $cart_item['quantity'];
break; // stop the loop if product is found
}
}
// Displaying the quantity if targeted product is in cart
if( ! empty( $qty ) )
echo '<p>Quantity for Product ID ' . $targeted_id . ' is: ' . $qty . '</p>;
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
これを試して :
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
?>