仅当购物车中包含强制性类别的产品时才允许结

时间:2023-03-20
本文介绍了仅当购物车中包含强制性类别的产品时才允许结帐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果客户的购物车中没有特定的产品类别,我想阻止他们进行结账.我还想通过错误消息告诉他们他们需要添加某个产品.我找到了一些代码,但无法正常工作.我已将它作为代码片段添加到我的 Wordpress 安装中,但可惜它不起作用,即使我打开了调试,也没有错误消息.这是我在 Github 中找到的可能需要修改才能使其工作的代码:

function sv_wc_prevent_checkout_for_category() {//设置我们禁止结账的类别的 slug$category = '兄弟姐妹';//获取产品类别$product_cat = get_term_by('slug', $category, 'product_cat');//如果该术语不存在,则进行完整性检查以防止致命如果(is_wp_error($product_cat)){返回;}$category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">'.$product_cat-> 名称.'</a>';//检查这个类别是否是购物车中唯一的东西如果(sv_wc_is_category_alone_in_cart($category)){//呈现通知以解释结帐被阻止的原因wc_add_notice( sprintf( '您好!看起来您的购物车只包含 %1$s 类别的产品 &ndash; 您必须购买其他类别的产品才能结帐.', $category_name ), '错误' );}}add_action('woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category');/*** 检查购物车是否只包含给定类别中的产品** @param string $category 产品类别的slug* @return bool - 如果购物车只包含给定的类别,则为真*/函数 sv_wc_is_category_alone_in_cart( $category ) {//检查我们类别的每个购物车项目foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {//如果产品不在我们的类别中,则退出,因为我们知道该类别并不孤单如果(!has_term($category,'product_cat',$ca​​rt_item['data']->id)){返回假;}}//如果我们在这里,购物车中的所有商品都在我们的类别中返回真;}

因此,如果兄弟姐妹"类别是购物车中的唯一商品,我希望停止结帐(带有错误消息).我有一个标准"类别,必须在客户结账之前放在购物篮中.希望这是有道理的.

解决方案

这里有一个可以解决问题的解决方案.特别是有两个主要功能(最后一个):

  1. 第一个功能(N°3)在购物车页面上显示您的消息,当购物车中有东西但不是强制性产品类别时.还会在必需的产品存档页面上显示消息(当客户从结帐中被重定向时很有用,见下文).
  2. 第二个功能(N°4)在客户尝试结账并且他的购物车没有缺少的强制性产品类别时将客户重定向到产品强制性类别存档页面.

<块引用>

your_mandatory_category_slug() 函数中的强制类别 slug 之前定义.

这是代码:

//定义必填产品类别的函数函数 your_mandatory_category_slug(){//在此处定义所需产品类别的 SLUG$category = '衣服';返回 $category;}//如果必需的产品类别在购物车中,则返回 true 的条件函数函数 has_mandatory_category(){$category_needed = your_mandatory_category_slug();$has_cat = false;//迭代购物车中的每个项目并检测...foreach ( WC()->cart->get_cart() 作为 $item ) {//检测需要的产品类别是否在购物车项目中if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {$has_cat = true;休息;}}返回 $has_cat;}//如果购物车中没有必需的产品类别,则显示消息的功能函数 required_category_display_message() {$category_needed = your_mandatory_category_slug();//检查购物车是否为空(用于购物车和产品类别档案)if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) ) ){$category_obj = get_term_by('slug', $category_needed, 'product_cat');如果(is_wp_error($category_obj))返回;//当产品类别不在购物车项目中时显示消息如果(!has_mandatory_category()){$category_name = $category_obj->name;$category_url = get_term_link( $category_needed, 'product_cat' );//呈现通知以解释结帐被阻止的原因wc_add_notice( sprintf( __( '<strong>提醒:</strong>您必须将%1$s"类别的产品添加到您的购物车中才能结帐.请返回<a href="%2$s"> 到%1$s"产品页面</a>', 'your_theme_domain'), $category_name, $category_url ), 'error' );}}}add_action('woocommerce_before_main_content', 'mandatory_category_display_message', 30);//对于产品强制性类别档案页面add_action('woocommerce_check_cart_items', 'mandatory_category_display_message');//用于猫页面//从结帐重定向到强制性产品类别存档页面的功能函数 required_category_checkout_redirect() {//如果结帐页面上的购物车不为空if(!WC()->cart->is_empty() &&is_checkout()){$category_needed = your_mandatory_category_slug();//如果缺少产品类别 =>重定向到产品类别页面如果(!has_mandatory_category())wp_redirect( get_term_link( $category_needed, 'product_cat' ) );}}add_action('template_redirect', 'mandatory_category_checkout_redirect');

这会出现在活动子主题(或主题)的 function.php 文件中,也可能出现在任何插件文件中.

此代码经过测试且功能齐全.

I'd like to stop any customer advancing to the checkout if they do not have a particular product category in their basket. I would also like to tell them with an error message that they need to add a certain product. I've found some code but cannot it to work. I've added it as a code snippet into my Wordpress install but alas it does not function and there are no error messages even though I have debugging switched on. Here is the code that I have found in Github that may need modification in order for this to work:

function sv_wc_prevent_checkout_for_category() {

    // set the slug of the category for which we disallow checkout
    $category = 'sibling';

    // 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;
    }

    $category_name = '<a href="' . get_term_link( $category, 'product_cat' ) . '">' . $product_cat->name . '</a>';

    // check if this category is the only thing in the cart
    if ( sv_wc_is_category_alone_in_cart( $category ) ) {

        // render a notice to explain why checkout is blocked
        wc_add_notice( sprintf( 'Hi there! Looks like your cart only contains products from the %1$s category &ndash; you must purchase a product from another category to check out.', $category_name ), 'error' );
    }
}
add_action( 'woocommerce_check_cart_items', 'sv_wc_prevent_checkout_for_category' );

/**
 * Checks if a cart contains exclusively products in a given category
 * 
 * @param string $category the slug of the product category
 * @return bool - true if the cart only contains the given 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;
}

So I'm looking to stop checkout (with error message) if the 'sibling' category is the only item in the cart. I have a 'standard' category which must be in the basket before the customer makes it to the checkout. Hope this makes sense.

解决方案

Here you have a solution that will make the trick. There is especially 2 main functions (the last ones):

  1. The first function (N°3) display your message on cart page, when there is something in cart but not the mandatory product category. Displays also the message on the mandatory product archive pages (useful when customer get redirected from checkout, see below).
  2. The second function (N°4) redirect customer to the product mandatory category archive pages when it tries to checkout and his cart has not that missing mandatory product category.

Define before your mandatory category slug in your_mandatory_category_slug() function.

This is the code:

// Function that define the mandatory product category
 function your_mandatory_category_slug(){

     // DEFINE HERE the SLUG of the needed product category
    $category = 'clothing';
    return $category;
 }


// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category(){
    $category_needed = your_mandatory_category_slug();
    $has_cat = false;

    // Iterrating each item in cart and detecting…
    foreach ( WC()->cart->get_cart() as $item ) {

        // Detects if the needed product category is in cart items
        if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
            $has_cat = true;
            break;
        }
    }
    return $has_cat;
 }


// Function that display a message if there is not in cart a mandatory product category
function mandatory_category_display_message() {
        $category_needed = your_mandatory_category_slug();

    // check that cart is not empty (for cart and product category archives)
    if( !WC()->cart->is_empty() && ( is_cart() || is_product_category( $category_needed ) ) ){
        $category_obj = get_term_by( 'slug', $category_needed, 'product_cat' );
        if ( is_wp_error( $category_obj ) ) return;

        // Display message when product category is not in cart items
        if ( !has_mandatory_category() ) {
            $category_name = $category_obj->name;
            $category_url = get_term_link( $category_needed, 'product_cat' );

            // render a notice to explain why checkout is blocked
            wc_add_notice( sprintf( __( '<strong>Reminder:</strong> You have to add in your cart, a product from "%1$s" category, to be allowed to check out. Please return <a href="%2$s"> here to "%1$s" product page</a>', 'your_theme_domain'), $category_name, $category_url ), 'error' );
        }
    }
}
add_action( 'woocommerce_before_main_content', 'mandatory_category_display_message', 30 ); // for product mandatory category archives pages
add_action( 'woocommerce_check_cart_items', 'mandatory_category_display_message' ); // for cat page


// Function that redirect from checkout to mandatory product category archives pages
function mandatory_category_checkout_redirect() {

    // If cart is not empty on checkout page
    if( !WC()->cart->is_empty() && is_checkout() ){
        $category_needed = your_mandatory_category_slug();

        // If missing product category => redirect to the products category page
        if ( !has_mandatory_category() )
            wp_redirect( get_term_link( $category_needed, 'product_cat' ) );
    }
}
add_action('template_redirect', 'mandatory_category_checkout_redirect');

This goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and fully functional.

这篇关于仅当购物车中包含强制性类别的产品时才允许结帐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:向产品属性添加新术语并将其设置在 Woocommerce 的 下一篇:自定义文本“总计"在 WooCommerce 结帐页面中

相关文章

最新文章