Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

php - Minimum cart amount for specific product categories in WooCommerce

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.

I tried to hook into woocommerce_after_calculate_totals to:

  • check cart items for a specific product category
  • display a notice when a specific product category is found and the order is lower than 30 €
  • and eventually redirect to cart page when user tries to checkout with an order lower than 30 €.

Here is my code:

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' ) );

                }
            }
        }
    }
}

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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' );
    }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...