在WooCommerce购物车中获取购物车商品的商品ID

时间:2021-01-04 20:19:36
$cart_item = $woocommerce->cart->get_cart(); 

I have the above code.

我有上面的代码。

if I run print_r on cart_item I get a multi dimensional array:

如果我在cart_item上运行print_r,我会得到一个多维数组:

Array(    [a6292668b36ef412fa3c4102d1311a62] => Array        (            [product_id] => 6803

How do I get the product_id only?

我如何才获得product_id?

I tried $test = $cart_item['data'];

我试过$ test = $ cart_item ['data'];

print_r($test);

Didn't work.

没工作。

1 个解决方案

#1


19  

To get the product ID of each cart item in the foreach loop (for a simple product):

要获取foreach循环中每个购物车项目的产品ID(对于简单产品):

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

If it's a variable product, to get the variation ID:

如果它是变量产品,要获取变体ID:

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

Or for both cases:

或两种情况:

foreach( WC()->cart->get_cart() as $cart_item ){
    // compatibility with WC +3
    if( version_compare( WC_VERSION, '3.0', '<' ) ){
        $product_id = $cart_item['data']->id; // Before version 3.0
    } else {
        $product_id = $cart_item['data']->get_id(); // For version 3 or more
    }
}

Update: Using Product ID outside the loop

更新:在循环外使用产品ID

1) Breaking the loop (Just to get the first item ID (product ID) of cart):

1)打破循环(只是为了得到购物车的第一个商品ID(产品ID)):

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

You can use directly $product_id variable of the first item in cart.

您可以直接使用购物车中第一项的$ product_id变量。


2 Using an array of product IDs (one for each item in cart).

2使用产品ID数组(购物车中的每个项目一个)。

$products_ids_array = array();

foreach( WC()->cart->get_cart() as $cart_item ){
    $products_ids_array[] = $cart_item['product_id'];
}
  • To get the 1st item product ID: $products_ids_array[0];
  • 要获得第1项产品ID:$ products_ids_array [0];
  • To get the 2nd item product ID: $products_ids_array[1]; etc…
  • 获取第2项产品ID:$ products_ids_array [1];等等…

#1


19  

To get the product ID of each cart item in the foreach loop (for a simple product):

要获取foreach循环中每个购物车项目的产品ID(对于简单产品):

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

If it's a variable product, to get the variation ID:

如果它是变量产品,要获取变体ID:

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

Or for both cases:

或两种情况:

foreach( WC()->cart->get_cart() as $cart_item ){
    // compatibility with WC +3
    if( version_compare( WC_VERSION, '3.0', '<' ) ){
        $product_id = $cart_item['data']->id; // Before version 3.0
    } else {
        $product_id = $cart_item['data']->get_id(); // For version 3 or more
    }
}

Update: Using Product ID outside the loop

更新:在循环外使用产品ID

1) Breaking the loop (Just to get the first item ID (product ID) of cart):

1)打破循环(只是为了得到购物车的第一个商品ID(产品ID)):

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

You can use directly $product_id variable of the first item in cart.

您可以直接使用购物车中第一项的$ product_id变量。


2 Using an array of product IDs (one for each item in cart).

2使用产品ID数组(购物车中的每个项目一个)。

$products_ids_array = array();

foreach( WC()->cart->get_cart() as $cart_item ){
    $products_ids_array[] = $cart_item['product_id'];
}
  • To get the 1st item product ID: $products_ids_array[0];
  • 要获得第1项产品ID:$ products_ids_array [0];
  • To get the 2nd item product ID: $products_ids_array[1]; etc…
  • 获取第2项产品ID:$ products_ids_array [1];等等…