【问题标题】:Minimum cart amount for specific product categories in WooCommerceWooCommerce 中特定产品类别的最低购物车数量
【发布时间】:2020-09-21 04:55:54
【问题描述】:

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

我尝试将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 以下),那么 Ajax 重新计算总计循环并且页面被阻止。但是,如果我重新加载结帐页面,我会正确地重定向到购物车页面。

【问题讨论】:

    标签: php wordpress woocommerce cart taxonomy-terms


    【解决方案1】:

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

    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 文件中。经过测试并且可以工作。

    【讨论】:

    • 好吧,我发现我错了。如果在结帐页面上使用优惠券并且总数降至 30 以下,则最初不会发生任何事情。如果您随后想下订单,您将被告知这是不可能的。所以有我的困惑。我道歉!
    • @7uc1f3r 没问题,路西法先生 :)……不客气。
    • 谢谢 LoicTheAztec 好多了。我只是有一个小问题:在购物车页面中,如果我申请优惠券,我会收到通知。但是,如果我删除优惠券,我仍然会收到通知。我认为这是因为 woocommerce_check_cart_items 只是在取消优惠券后重新计算购物车总数之前执行。这就是我尝试使用 woocommerce_after_calculate_totals 的原因。但是,如果我用这个钩子尝试你的代码,我会收到两次通知......无论如何,你的代码非常有助于阻止结帐过程,直到满足条件,所以我会设法解决双重通知问题。
    • @psivel Mélangé Franco-Mexicain ++
    • CQFD :) 布列塔尼-墨西哥? En tous cas merci aussi d'avoir reformulé ma 问题。 C'était mon tout Premier post et votre version est bien plus claire。 Ca m'aidera à rendre plus 简明扼要的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2017-12-10
    • 1970-01-01
    • 2019-01-27
    相关资源
    最近更新 更多