有了這個代碼:如何從購物車頁面特定產品的數量在woocommerce
foreach (WC()->cart->get_cart() as $cart_item) {
$quantity = $cart_item['quantity'];
echo $quantity;
}
我能得到的所有購物車內添加的產品的數量,但我需要它的特定產品。
有了這個代碼:如何從購物車頁面特定產品的數量在woocommerce
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>;
非常感謝它的工作...... :) :) –
試試這個:
<?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>";
}
?>
通過這個我會得到所有的XYZ產品 我只需要Y產品數量... –
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>";
}
通過這我會得到所有我需要的XYZ產品只有Y產品數量... –
只是檢查if語句'如果($ cart_item [ '的product_id'] ===您的ID「 ){' –