WooCommerce 中特定产品类别的最小购物车数量 [英] Minimum cart amount for specific product categories in WooCommerce

查看:31
本文介绍了WooCommerce 中特定产品类别的最小购物车数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,我将 OUTLET 类别用于促销产品,我想为购买任何Outlet"产品的客户设置最低小计(30 欧元).

In WooCommerce I use an OUTLET category with on sale products and I would like to set a minimum subtotal (30 €) for customers purchasing any "Outlet" product.

我试图连接到 woocommerce_after_calculate_totals 以:

  • 检查特定产品类别的购物车商品
  • 在找到特定产品类别且订单低于 30 欧元时显示通知
  • 并最终在用户尝试以低于 30 欧元的订单结账时重定向到购物车页面.

这是我的代码:

add_action( 'woocommerce_after_calculate_totals', 'check_order_outlet_items', 10, 0 );

function check_order_outlet_items() {

    global $woocommerce;

    if (is_cart() || is_checkout()) {

        // Check if cart contains items in Outlet cat.

        $items = $woocommerce->cart->get_cart();

        foreach($items as $item => $values) {

            $product_id = $values['product_id'];

            $terms = get_the_terms( $product_id, 'product_cat' );

            foreach ($terms as $term) {
                if ($term->name == "OUTLET") {
                    $outlet_found = 1;
                    break;
                }
            }
            if ($outlet_found) {break;}

        }

        if ($outlet_found) {

            // Calculate order amount including discount

            $cart_subtotal = $woocommerce->cart->subtotal;
            $discount_excl_tax_total = $woocommerce->cart->get_cart_discount_total();
            $discount_tax_total = $woocommerce->cart->get_cart_discount_tax_total();
            $discount_total = $discount_excl_tax_total + $discount_tax_total;
            $order_net_amount = $cart_subtotal - $discount_total;

            // Check if condition met

            if ($order_net_amount < 30) {

                if (is_checkout()) {

                    wp_redirect(WC()->cart->get_cart_url());
                    exit();

                } else {

                    wc_add_notice( __( 'You must order at least 30 €', 'error' ) );

                }
            }
        }
    }
}

此代码在购物车页面中完美运行(如果购物车的数量小于 30,即使添加优惠券后购物车数量低于 30,也会显示通知)并在用户想要结账时重定向到购物车.

This code works perfectly in the cart page (displaying notice if cart's amount < 30 even if carts amount goes below 30 after adding a coupon) and redirecting to cart if users wants to go to checkout.

但是如果我去结帐页面的金额 >= 30 然后添加优惠券(将购物车金额降低到 30 以下),则 Ajax 重新计算总计循环并且页面被阻止.但是,如果我重新加载结帐页面,我就会正确地重定向到购物车页面.

But If I go to checkout page with an amount >= 30 and then add a coupon (to lower cart amount below 30), then the Ajax recalculating totals loops and the page is blocked. But then if I reload the checkout page I'm correctly redirected to cart page.

推荐答案

这种情况下使用的正确钩子是woocommerce_check_cart_items 这样:

The right hook to be used in this case is woocommerce_check_cart_items this way:

add_action( 'woocommerce_check_cart_items', 'check_cart_outlet_items' );
function check_cart_outlet_items() {
    $categories = array('OUTLET'); // Defined targeted product categories
    $threshold  = 30; // Defined threshold amount

    $cart       = WC()->cart;
    $cart_items = $cart->get_cart();
    $subtotal   = $cart->subtotal;
    $subtotal  -= $cart->get_cart_discount_total() + $cart->get_cart_discount_tax_total();
    $found      = false;

    foreach( $cart_items as $cart_item_key => $cart_item ) {
        // Check for specific product categories
        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $found = true; // A category is found
            break; // Stop the loop
        }
    }

    if ( $found && $subtotal < $threshold ) {
        // Display an error notice (and avoid checkout)
        wc_add_notice( sprintf( __( "You must order at least %s" ), wc_price($threshold) ), 'error' );
    }
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

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

这篇关于WooCommerce 中特定产品类别的最小购物车数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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