2017-06-19 60 views
4

有了這個代碼:如何從購物車頁面特定產品的數量在woocommerce

foreach (WC()->cart->get_cart() as $cart_item) { 
    $quantity = $cart_item['quantity']; 
    echo $quantity; 
} 

我能得到的所有購物車內添加的產品的數量,但我需要它的特定產品。

+0

只是檢查if語句'如果($ cart_item [ '的product_id'] ===您的ID「 ){' –

回答

3

你應該試試這個:

// 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>; 
+1

非常感謝它的工作...... :) :) –

0

試試這個:

<?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>"; 
     } 
?> 
+0

通過這個我會得到所有的XYZ產品 我只需要Y產品數量... –

1
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>"; 
    } 
+0

通過這我會得到所有我需要的XYZ產品只有Y產品數量... –

相關問題