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

eoxn13cs  于 2023-08-03  发布在  WordPress
关注(0)|答案(7)|浏览(149)

使用此代码:

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

字符串
我可以得到的数量的所有产品添加到购物车,但我需要它的特定产品。

6bc51xsx

6bc51xsx1#

您可以循环浏览购物车项目以获取特定产品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 );
}

字符串
或者你也可以使用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] );
}

**注意:**最后一种方式不允许以父变量product为目标。

ccrfmcuu

ccrfmcuu2#

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

字符串

knpiaxh1

knpiaxh13#

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

字符串

ewm0tg9j

ewm0tg9j4#

试试这个:

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

字符串

rdlzhqv9

rdlzhqv95#

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"]


(将xxx更改为您想要了解购物车中数量的产品的ID)
感谢LoicTheAztec(同一主题)

juud5qan

juud5qan6#

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;

tuwxkamq

tuwxkamq7#

您可以在以下函数中轻松发送产品ID,如果购物车中不存在产品,则返回0,如果购物车中存在产品,则返回数量。

if ( ! function_exists( 'wc_wg_get_cart_qty_product_id' ) ) :
    function wc_wg_get_cart_qty_product_id( $product_id ) {
        if ( empty( $product_id ) ) {
            return 0;
        }
        if ( ! class_exists( 'WooCommerce' ) ) {
            return 0;
        }
        $quantities = WC()->cart->get_cart_item_quantities(); 

        if ( isset( $quantities[$product_id] ) && $quantities[ $product_id ] > 0 ) {
            return $quantities[ $product_id ];
        } else {
            return 0;
        }
    }
endif;

字符串
要调用该函数,只需将产品ID传入函数,如果产品在购物车中,它将返回购物车中的产品数量。

$quantityinCart = wc_wg_get_cart_qty_product_id( $loop->post->ID );

相关问题