php – 限制购物车商品来自同一产品类别

前端之家收集整理的这篇文章主要介绍了php – 限制购物车商品来自同一产品类别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我在购物车中添加了特殊产品类别“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,$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( '<p class="woocommerce-error">bla bla bla one category X item in the cart</p>' ),'success' );
        } elseif ( $notice == '2' ) { // message for an 'cat_x' item and other ones => removed other ones 
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla ther is already category X in the cart => Other category items has been removed</p>' ),'error' );
        } elseif ( $notice == '3' ) { // message for other categories items (if needed)
            wc_add_notice( sprintf( '<p class="woocommerce-error">bla bla bla NOT category X in the cart</p>' ),'success' );
        }
    }
}

条件函数has_term()是否也适用于类别数组,我尝试过而不是使用一个类别来设置该代码中的类别数组.但它不起作用.

但是,我的需求已经改变:我不想让顾客有可能选择不同类别的购物车商品.因此购物车必须始终包含来自同一产品类别的商品.

有什么帮助吗?

谢谢.

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

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

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

add_filter( 'woocommerce_add_to_cart_validation','custom_checking_product_added_to_cart',10,3 );
function custom_checking_product_added_to_cart( $passed,$product_id,$quantity) {

    // HERE Type your alert displayed message
    $message = __( 'BLA BLA YOUR MESSAGE.','woocommerce' );

    $product_cats_object = get_the_terms( $product_id,'product_cat' );
    foreach($product_cats_object as $obj_prod_cat){
        $product_cats[] = $obj_prod_cat->slug;
    }

    foreach (WC()->cart->get_cart() as $cart_item ){
        if( ! has_term( $product_cats,$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,$cart_item['product_id'] )) {

通过

if( has_term( $product_cats,$cart_item['product_id'] )) {
原文链接:https://www.f2er.com/php/138502.html

猜你在找的PHP相关文章