允许在Woocommerce中结帐非混合定义产品类别 [英] Allow proceed to checkout for non mixed defined product categories in Woocommerce

查看:47
本文介绍了允许在Woocommerce中结帐非混合定义产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,当购物车中的商品来自混合产品类别时,我试图避免继续结帐…以下代码通常可以按我的预期工作:

In woocommerce I am trying to avoid proceed to checkout when cart items are from mixed product categories… The following code is mostly working as I intended:

function sv_wc_prevent_checkout_for_category() {

    // set the slug of the category that cannot be mixed from other categories
    $category = 'test';

    // get the product category
    $product_cat = get_term_by( 'slug', $category, 'product_cat' );

    // sanity check to prevent fatals if the term doesn't exist
    if ( is_wp_error( $product_cat ) ) {
        return;
    }

    if ( sv_wc_is_category_alone_in_cart( $category ) ) {

        //Proceed to checkout
    }
    else { // otherwise post a notice to explain why checkout is blocked 
    wc_add_notice( sprintf( 'hi there! looks like your cart contains products from PREORDER and ONHAND categories, in order to proceed go to cart and dont mixed it.', $category_name ), 'error' ); }   

}

add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );

function sv_wc_is_category_alone_in_cart( $category ) {

    // check each cart item for our category
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

        // if a product is not in our category, bail out since we know the category is not alone
        if ( ! has_term( $category, 'product_cat', $cart_item['data']->id ) ) {
        return false;
        }
    }

    // if we're here, all items in the cart are in our category
    return true;
}

但是我有一个问题:当购物车仅包含其他产品类别的商品时,不允许进行结帐以显示错误消息.

But I have a problem: When cart contain items only from other product categories it doesn't allow proceed to checkout displaying the error message.

要恢复:

  1. 如果购物车包含"TEST"产品类别:允许进行结帐"(有效)
  2. 如果购物车商品具有混合的产品类别(测试"和其他类别):不允许进行结帐"并显示错误消息(有效)
  3. 如果购物车仅包含其他产品类别(不包含"TEST"产品类别):不允许进行结帐"并显示错误消息(不工作)

如何使其适用于其他产品类别,允许进行结帐"?

How can I make it work for other product categories, allowing "proceed to checkout"?

感谢您的帮助.

推荐答案

尝试以下代码,这些代码不允许将测试"定义的产品类别与其他产品类别混合,显示错误通知并避免结帐:

Try the following code that will not allow to mix your "test" defined product category with other product categories, displaying an error notice and avoiding checkout:

add_action( 'woocommerce_check_cart_items', 'prevent_checkout_product_category_based' );
function prevent_checkout_product_category_based() {

    // HERE set the product category slug
    $category = 'test';

    $found = $other = false; // Initializing variables

    // Loop through cart items
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // checking for the specific product category
        $term_slugs = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array('fields' => 'slugs') );
        if( in_array($category, $term_slugs) ) {
            $found = true; // Targeted product category found
        }
        elseif( ! in_array($category, $term_slugs) && sizeof($term_slugs) > 0 ){
            $other = true; // Other product categories found
        }
    }
    // If the targeted product category is mixed with other product categories
    if ( $found && $other ) {
        // Display an error notice and avoid checkout
        wc_add_notice( __( "The cart contains products from PREORDER and ONHAND categories and can't be mixed together, to allow checkout." ), 'error' );
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于允许在Woocommerce中结帐非混合定义产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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