如何从woocommerce的购物车页面获取特定的产品数量

时间:2017-06-19 13:00:56

标签: php wordpress woocommerce cart product

使用此代码:

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

我可以获得购物车中添加的所有产品的数量,但我需要特定产品。

6 个答案:

答案 0 :(得分:5)

你应该试试这个:

// 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 :(得分: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>";
    } 

答案 2 :(得分:1)

$cart = WC()->cart->get_cart();
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
if( WC()->cart->find_product_in_cart( $product_cart_id )) {
    echo($cart[$product_cart_id]['quantity']);
}

答案 3 :(得分: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>";
        } 
?>

答案 4 :(得分:0)

1)制作儿童主题

2)使用以下代码创建一个名为functions.php的文件:

<?php
if( !function_exists('in_cart_product_quantity') ) {
    function in_cart_product_quantity( $atts ) {

        $atts = shortcode_atts(array('id' => ''), $atts, 'product_qty'); // shortcode attributes
        if( empty($atts['id'])) return; // check id not null

        if ( WC()->cart ) { // check cart exists
            $qty = 0; // init qty
            foreach (WC()->cart->get_cart() as $cart_item) {
                if($cart_item['product_id'] == $atts['id']) {
                    $qty =  $cart_item['quantity'];
                    break; // stop the loop if product is found
                }
            }
            return $qty;
        }
        return;
    }
    add_shortcode( 'product_qty', 'in_cart_product_quantity' );
}

3)使用简码:

[product_qty id="xxx"]

(用您想知道购物车中数量的产品ID更改xxx)

感谢LoicTheAztec(同一线程)

答案 5 :(得分:0)

WC_Cart::get_cart_item_quantities() – Get cart items quantities
  1. 通过$product_id
// Your product ID
$product_id = 30;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart - All PHP versions
$product_qty_in_cart = isset( $cart_item_quantities[ $product_id ] ) ? $cart_item_quantities[ $product_id ] : null;

// Product quantity in cart - Same as the previous one with PHP 7
$product_qty_in_cart = $cart_item_quantities[ $product_id ] ?? null;

// Result
echo $product_qty_in_cart;
  1. 通过$product
// Product
global $product;

// Get cart items quantities
$cart_item_quantities = WC()->cart->get_cart_item_quantities();

// Product quantity in cart 
$product_qty_in_cart = $cart_item_quantities[ $product->get_stock_managed_by_id() ];

// Result
echo $product_qty_in_cart;