在 WooCommerce 购物车中获取购物车项目的产品 ID [英] Get in WooCommerce cart the product ID of a cart item

查看:64
本文介绍了在 WooCommerce 购物车中获取购物车项目的产品 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$cart_item = $woocommerce->cart->get_cart();

我有上面的代码.

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

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

如何仅获取 product_id?

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

print_r($test);

没用.

解决方案

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

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

如果是可变产品,获取variation ID:

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

或者对于这两种情况(其中 $cart_item['data'] WC_Product Woocommerce 3+ 中的对象):

foreach(WC()->cart->get_cart() as $cart_item ){//与 WC+3 兼容if( version_compare( WC_VERSION, '3.0', '<' ) ){$product_id = $cart_item['data']->id;//3.0版本之前} 别的 {$product_id = $cart_item['data']->get_id();//对于版本 3 或更高版本}}

<小时><块引用>

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

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

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

您可以直接使用购物车中第一件商品的$product_id变量.

<小时>

2) 使用一组产品 ID (购物车中的每件商品一个).

$products_ids_array = array();foreach(WC()->cart->get_cart() 作为 $cart_item ){$products_ids_array[] = $cart_item['product_id'];}

  • 要获取第一个商品 ID:$products_ids_array[0];
  • 获取第二个商品 ID:$products_ids_array[1]; 等等……
<小时>

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

foreach(WC()->cart->get_cart() as $cart_item ){//对于产品类别(术语 ID、术语 slug 或术语名称)if( has_term( array('clothing','music'), 'product_cat', $cart_item['product_id'] ) ) {//做点什么}//对于产品标签(术语 ID、术语 slug 或术语名称)if( has_term( array('clothing','music'), 'product_tag', $cart_item['product_id'] ) ) {//做其他事情}}

<块引用>

我们总是使用 $cart_item['product_id'],因为当购物车项目是产品变体时,我们会得到父变量 product.

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

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

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

How do I get the product_id only?

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

print_r($test);

Didn't work.

解决方案

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

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:

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

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

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

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

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.


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

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

To check product categories or product tags in cart item use WordPress has_term() like:

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

这篇关于在 WooCommerce 购物车中获取购物车项目的产品 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆