php 在 WooCommerce 购物车中获取购物车项目的产品 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41104409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:19:21  来源:igfitidea点击:

Get in WooCommerce cart the product ID of a cart item

phpwordpresswoocommercecartproduct

提问by Kevin.a

$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.

没用。

回答by LoicTheAztec

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

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

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:

如果它是可变产品,要获得variation ID

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

Or for both cases (where$cart_item['data']is theWC_ProductObject in Woocommerce 3+):

或者对于这两种情况Woocommerce 3+ 中$cart_item['data']WC_Product对象在哪里)

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_idvariable 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];
  • To get the 2nd item product ID: $products_ids_array[1];etc…
  • 要获取第一个项目产品 ID: $products_ids_array[0];
  • 要获取第二项产品 ID:$products_ids_array[1];等等...


To check product categoriesor product tagsin cart item use WordPress has_term()like:

要检查购物车项目中的产品类别产品标签,请使用 WordPress,has_term()例如:

foreach( WC()->cart->get_cart() as $cart_item ){
    // For product categories (term IDs, term slugs or term names)
    if( has_term( array('clothing','music'), 'product_cat', $cart_item['product_id'] ) ) {
        // DO SOMETHING
    }

    // For product Tags (term IDs, term slugs or term names)
    if( has_term( array('clothing','music'), 'product_tag', $cart_item['product_id'] ) ) {
        // DO SOMETHING ELSE
    }
}

We always use $cart_item['product_id']as we get the parent variable product when a cart item is a product variation.

Product variations don't handle any custom taxonomy as product categories and product tags

$cart_item['product_id']当购物车项目是产品变体时,我们总是使用as 我们获取父变量产品。

产品变体不处理任何自定义分类作为产品类别和产品标签