简体   繁体   English

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

[英]how to get particular product quantity from the cart page in the woocommerce

With this code:使用此代码:

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

I can get the quantity of all the products added in cart but I need it for the particular product.我可以获得添加到购物车中的所有产品的数量,但我需要特定产品的数量。

You can loop through cart items to get the quantity for a specific product id as follows:您可以遍历购物车项目以获取特定产品 ID 的数量,如下所示:

// Set here your product ID (or variation ID)
$targeted_id = 24;

// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) { 
    if( in_array( $targeted_id, array($cart_item['product_id'], $cart_item['variation_id']) ){
        $quantity =  $cart_item['quantity'];
        break; // stop the loop if product is found
    }
}
// Displaying the quantity if targeted product is in cart
if( isset( $quantity ) && $quantity > 0 ) {
    printf( '<p>' . __("Quantity for Product ID %d is: %d") . '</p>, $targeted_id, $quantity );
}

Or you can also use the WC_Cart method get_cart_item_quantities() as follow:或者您也可以使用WC_Cart方法get_cart_item_quantities()如下:

// Set here your product ID (or variation ID)
$targeted_id = 24;

// Get quantities for each item in cart (array of product id / quantity pairs)
$quantities = WC()->cart->get_cart_item_quantities(); 

// Displaying the quantity if targeted product is in cart
if( isset($quantities[$targeted_id]) && $quantities[$targeted_id] > 0 ) {
    printf( '<p>' . __("Quantity for Product ID %d is: %d") . '</p>, $targeted_id, $quantities[$targeted_id] );
}

Note: This last way doesn't allow to target the parent variable product.注意:最后一种方法不允许定位父变量产品。

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>";
    } 
$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']);
}

Try this :试试这个:

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

1) Make a child theme 1)制作儿童主题

2) Create a file named functions.php with this code : 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) Use the shortcode : 3)使用简码:

[product_qty id="xxx"]

(Change xxx with id of the product you want to know the quantity in cart) (用您想知道购物车数量的产品的id更改xxx)

Thanks to LoicTheAztec (Same thread)感谢 LoicTheAztec(同一线程)

WC_Cart::get_cart_item_quantities() – Get cart items quantities
  1. Via $product_id通过$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. Via $product通过$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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WooCommerce $ product-&gt; get_stock_quantity(); 打破购物车页面 - WooCommerce $product->get_stock_quantity(); breaking cart page 如何从WooCommerce中的购物车页面中删除数量 - How to remove quantity from cart page in WooCommerce 如何从购物车页面中删除特定产品属性WooCommerce的数量字段 - How to remove the quantity field from cart page for specific product attribute WooCommerce 当 woocommerce 中的产品数量增加/减少时,如何从商店页面更新购物车? - How to update cart from shop page when product quantity increase/decrease in woocommerce? 根据购物车页面中的属性值和产品数量,在 WooCommerce 中添加到购物车验证 - Add to cart validation in WooCommerce based on attribute value and product quantity from the cart page 在 WooCommerce 产品页面中显示数量变化的产品总数和购物车小计 - Display product total and cart subtotal on quantity change in WooCommerce product page Woocommerce购物车中的“最小和步骤”数量和批发角色的单个产品页面 - Min and Steps quantity in Woocommerce cart and single product page for wholesale role 如果用户在WooCommerce购物车中将数量更改为0,则获取项目的产品ID - Get The Product Id of An Item If User Changes Quantity to 0 in WooCommerce Cart WooCommerce:仅获取定制产品的购物车商品数量 - WooCommerce: Get the cart item quantity of a customized product only 从购物车页面中删除Woocommerce购物车数量选择器 - Remove Woocommerce cart quantity selector from cart page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM