我想做的是对购物车中的产品进行分类。然后通过一些条件检查类别id是什么,并显示不同的通知。
到目前为止我有这个
/**
* Cart collaterals hook.
*
* @hooked woocommerce_cross_sell_display
* @hooked woocommerce_cart_totals - 10
*/
$categories = array( 39 );
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_term( $categories, \'product_cat\', $cart_item[\'product_id\'] ) ) {
$found = true; // Set to true
break; // Stop the loop
}
}
if( $found ) {
echo \'Category with ID = 39\';
}
else if ($found != $cart_item[\'category_ids\']) {
echo "Category with ID = 39 and other ID(\'s)";
}
else {
"Category with ID != 39";
}
似乎是
$cart_item[\'category_ids\']
不返回类别。
$found
正在工作并显示ID=39的类别。
当我var_dump($cart_items)
我确实看到了这样的不同类别:
["category_ids"]=>
array(1) {
[0]=>
int(39)
}
/// another stuff in the array
["category_ids"]=>
array(2) {
[0]=>
int(32)
[1]=>
int(33)
}
所以,在购物车中,我有来自id为的类别的产品
39
,
32
和
33
.
我也试过了WC()->cart->get_cart()->category_ids
但是这个回报NULL
更新:此
$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids, $cart_item[\'data\']->get_category_ids()
);
}
$cat_id = 39;
if ( in_array( $cat_id, $cat_ids ) ) {
echo \'Only 39 \';
} elseif ( ! empty( $cat_ids ) ) {
echo \'39 + other\';
} else {
echo \' all other without 39\';
}
当前匹配
时间:类别39+其他->Only 39
时间:所有其他无39->39 + other
时间:仅39->Only 39
应该是这样的
时间:类别39+其他->39 + other
时间:所有其他无39->all other without 39
时间:仅39->Only 39
UPDATE
时间:类别39+其他类别的产品(类别ID=32、33等)
var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) } <---- there are 3 products in the cart 2 of them are from other categories.
时间:仅39类
var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) }
时间:无类别39
var_dump(count( $cat_ids )); -> int(2)
var_dump($has_cat); -> bool(false)
var_dump(cat_ids); -> array(2) { [0]=> int(32) [1]=> int(33) } <--- product is added in 2 categories
UPDATE 2
条件1
1) cat 30;
2) cat 39;
$cond = 2 (because there are products in cart from 39 + other category)
条件2
1) cat 39;
$cond = 1 (because in cart is/are product/s only from cat 39)
条件3
1) cat 40;
2) cat 15;
$cond = last one (because there is no product/s from cat 39 in cart)
最合适的回答,由SO网友:Sally CJ 整理而成
使用$cart_item[\'data\']->get_category_ids()
要检索类别ID,请执行以下操作:
$category_ids = $cart_item[\'data\']->get_category_ids(); // array
The
category_ids
您看到的不是
$cart_item
阵列:
var_dump( $cart_item[\'category_ids\'] ); // null and PHP throws a notice
它是产品数据的受保护属性中的一项(
$cart_item[\'data\']
) 是对象还是类实例&mdash;e、 g.课程为
WC_Product_Simple
对于简单的产品。
更新如果要收集所有产品类别ID,可以这样做:
$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids, $cart_item[\'data\']->get_category_ids()
);
}
var_dump( $cat_ids );
更新#2您可以使用此选项检查购物车中的产品是否属于特定类别:
$cat_ids = array( 39 );
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( $cat_ids, \'product_cat\', $cart_item[\'data\'] ) ) {
echo \'Has category 39<br>\';
} else {
echo \'No category 39<br>\';
}
}
更新#3/#4/#5这应按预期工作:
// Collect the category IDs.
$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
$cat_ids = array_merge(
$cat_ids, $cart_item[\'data\']->get_category_ids()
);
}
// And here we check the IDs.
$cat_id = 39;
$count = count( $cat_ids );
if ( 1 === $count && in_array( $cat_id, $cat_ids ) ) {
echo \'Only in category 39\';
} elseif ( $count && in_array( $cat_id, $cat_ids ) ) {
echo \'In category 39 and other categories\';
} else {
echo \'No category 39\';
}
以下是
if
上面的块:
if ( $count && in_array( $cat_id, $cat_ids ) ) {
// Only one category.
if ( $count < 2 ) {
echo \'Only in category 39\';
// Multiple categories.
} else {
echo \'In category 39 and other categories\';
}
} else {
echo \'No category 39\';
}