简体   繁体   English

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

[英]Allow checkout only when a product of a mandatory category is in cart

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.我已将它作为代码片段添加到我的 Wordpress 安装中,但可惜它不起作用,即使我打开了调试,也没有错误消息。 Here is the code that I have found in Github that may need modification in order for this to work:这是我在 Github 中找到的可能需要修改才能使其工作的代码:

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.第一个功能(N°3)在购物车页面上显示您的消息,当购物车中有东西但不是强制性产品类别时。 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.第二个功能(N°4)在客户尝试结账并且他的购物车没有缺少的强制性产品类别时将客户重定向到产品强制性类别存档页面。

Define before your mandatory category slug in your_mandatory_category_slug() function.your_mandatory_category_slug()函数中的强制类别 slug 之前定义。

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.这在您的活动子主题(或主题)或任何插件文件的 function.php 文件中。

This code is tested and fully functional.此代码经过测试且功能齐全。

Works like a charm for my Wordpress installation with Woocommerce v2.6.12.使用 Woocommerce v2.6.12 对我的 Wordpress 安装很有用。

I would like to add two mandatory product-categories, maybe 3 or 4 in the future.我想添加两个强制性产品类别,将来可能会添加 3 或 4 个。 I tried changing:我尝试改变:

$category = 'slug1';

to

$category = 'slug1', 'slug2';

and

$category = array('slug1','slug2');

Without success.没有成功。 Is there a way to make this code work with multiple slugs?有没有办法使此代码与多个 slug 一起使用?

如果我想重定向到产品网址而不是类别,您将如何做不同的事情?

I have adapted LoicTheAztec's answer to work with 2 categories.我已经改编了 LoicTheAztec 的答案以处理 2 个类别。 It seems to be working.它似乎正在工作。 I did not adapt the text has_mandatory_categorytext function, .我没有改编文字has_mandatory_categorytext函数,. If you can follow the code you can diy, but I was focusing on just functionality.如果您可以按照代码进行操作,则可以自己动手做,但我只关注功能。 There is another answer here but I preferred this answer and the other was not working for me.这里有另一个答案但我更喜欢这个答案,而另一个对我不起作用。

// Function that define the 2 mandatory product categories cat1 & cat2
 function your_mandatory_category_slug(){ $category = 'cat1';    return $category; }
 function your_mandatory_category_slug2(){ $category = 'cat2';    return $category; }
// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category1(){
    $category_needed = your_mandatory_category_slug();
    $has_cat = false;
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
            $has_cat = true;
            break;
        }
    }
    return $has_cat;
 }
// Conditional function that returns true if the mandatory product category is in cart
function has_mandatory_category2(){
    $category_needed = your_mandatory_category_slug2();
    $has_cat = false;
    foreach ( WC()->cart->get_cart() as $item ) {
        if ( has_term($category_needed, 'product_cat', $item['product_id'] ) ) {
            $has_cat = true;
            break;
        }
    }
    return $has_cat;
 }





// 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();
        $category_needed2 = your_mandatory_category_slug2();
        if ( !has_mandatory_category1() )
            wp_redirect( get_term_link( $category_needed, 'product_cat' ) );
        if ( !has_mandatory_category2() )
            wp_redirect( get_term_link( $category_needed2, 'product_cat' ) );
    }
}
add_action('template_redirect', 'mandatory_category_checkout_redirect');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 购物车中每个产品类别只允许一个产品 - Allow only one product per product category in cart 在Woocommerce中仅允许更新一个商品类别的购物车商品数量 - Allow cart item quantity update for only one product category in Woocommerce 在 Woocommerce 中一次只允许购物车中的一个产品类别 - Allow only one product category in cart at once in Woocommerce 在购物车和结帐页面上显示 Woocommerce 产品类别 - Display Woocommerce Product Category on cart and checkout page Magento允许产品添加到购物车(如果添加了其他类别的产品) - Magento allow product add to cart if another category product added WooCommerce 根据产品类别替换购物车/结帐中的“延期交货可用” - WooCommerce replace “Available on backorder” in cart/checkout based on product category Woocommerce从结帐/购物车页面中删除产品类别链接 - Woocommerce remove product category link from checkout/cart page 仅当特定产品在 WooCommerce 购物车中时,才根据特定产品类别应用/确定折扣 - Apply/determine discount based on a particular product category only when a specific product is in WooCommerce cart "允许将 Woocommerce 中特定产品类别的最多 3 个产品添加到购物车" - Allow add to cart 3 products max for a specific product category in Woocommerce 结帐购物车中的可选产品(/结帐/购物车/) - Optional product in a Checkout Cart (/checkout/cart/)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM