php购物车商品不重复,php-将购物车中的商品限制为来自WooCommerce中的同一商品类别...

当购物车中添加了带有特殊产品类别“ cat_x”的商品并显示一些不同的自定义通知时,我正在使用以下代码从购物车中删除其他WooCommerce产品类别商品.该代码来自this thread,效果很好:

add_action( 'woocommerce_check_cart_items', 'checking_cart_items' );

function checking_cart_items() {

$special = false;

$catx = 'cat_x';

$number_of_items = sizeof( WC()->cart->get_cart() );

if ( $number_of_items > 0 ) {

// Loop through all cart products

foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

$item = $values['data'];

$item_id = $item->id;

// detecting if 'cat_x' item is in cart

if ( has_term( $catx, 'product_cat', $item_id ) ) {

if (!$special)

$special = true;

}

}

// Re-loop through all cart products

foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {

$item = $values['data'];

$item_id = $item->id;

if ( $special ) // there is a 'cat_x' item in cart

{

if ( $number_of_items == 1 ) { // only one 'cat_x' item in cart

if ( empty( $notice ) )

$notice = '1';

}

if ( $number_of_items >= 2 ) { // 'cat_x' item + other categories items in cart

// removing other categories items from cart

if ( !has_term( $catx, 'product_cat', $item_id ) ) {

WC()->cart->remove_cart_item( $cart_item_key ); // removing item from cart

if ( empty( $notice ) || $notice == '1' )

$notice = '2';

}

}

} else { // Only other categories items

if ( empty( $notice ) )

$notice = '3';

}

}

// Firing notices

if ( $notice == '1' ) { // message for an 'cat_x' item only (alone)

wc_add_notice( sprintf( '

bla bla bla one category X item in the cart

' ), 'success' );

} elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones

wc_add_notice( sprintf( '

bla bla bla ther is already category X in the cart => Other category items has been removed

' ), 'error' );

} elseif ( $notice == '3' ) { // message for other categories items (if needed)

wc_add_notice( sprintf( '

bla bla bla NOT category X in the cart

' ), 'success' );

}

}

}

有条件函数has_term()也可以用于类别数组,我尝试代替一个类别在该代码中设置类别数组.但这不起作用.

但是,我的需求发生了变化:我不想让客户有可能从不同类别中选择购物车项目.因此,购物车必须始终具有相同产品类别的商品.

有什么帮助吗?

谢谢.

解决方法:

制作挂接到woocommerce_add_to_cart_validation过滤器挂钩中的自定义函数将以一种更简单的方式完成此工作,而无需设置类别数组.

因此,您的代码将更快,更紧凑.此外,您可以显示自定义通知以警告客户.

如果购物车中有其他类别的商品,则此代码将避免添加到购物车中:

add_filter( 'woocommerce_add_to_cart_validation', 'add_to_cart_validation_callback', 10, 3 );

function add_to_cart_validation_callback( $passed, $product_id, $quantity) {

// HERE set your alert text message

$message = __( 'MY ALERT MESSAGE.', 'woocommerce' );

if( ! WC()->cart->is_empty() ) {

// Get the product category terms for the current product

$terms_slugs = wp_get_post_terms( $product_id, 'product_cat' array('fields' => 'slugs'));

// Loop through cart items

foreach (WC()->cart->get_cart() as $cart_item ){

if( ! has_term( $terms_slugs, 'product_cat', $cart_item['product_id'] )) {

$passed = false;

wc_add_notice( $message, 'error' );

break;

}

}

}

return $passed;

}

代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中.

此代码已经过测试,可以正常工作

Restricting cart items to be only from different product categories:

在函数中替换条件:

if( ! has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {

通过

if( has_term( $product_cats, 'product_cat', $cart_item['product_id'] )) {

标签:cart,woocommerce,wordpress,categories,php

来源: https://codeday.me/bug/20191111/2021584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值