Home > Blockchain >  Add a minimum order price in WooCommerce, but still allow the customer to checkout when the conditio
Add a minimum order price in WooCommerce, but still allow the customer to checkout when the conditio

Time:04-21

I would like to add a minimum order price for WooCommerce. I have found instructions on how to restrict ordering, unless the user adds more products to cart so that the minimum sum (for example 15€) is met.

But I would like to have it so, that the cart total sum is automatically increased to 15€ ( shipping) if the products in cart total less than 15€.

Here is what I have so far:

add_action( 'woocommerce_before_cart' , 'total_order_amount' );
add_action( 'woocommerce_review_order_before_order_total' , 'total_order_amount' );
//include the minimum total in the total price in Cart and Checkout pages 
function total_order_amount( ) { 
    // Set this variable to specify a minimum order value
    $minimum = get_option( 'cart_minimum_total' );
    if ( WC()->cart->subtotal < $minimum ) {
        if( is_cart() || is_checkout() ) {
            $new_price = (WC()->cart->total - WC()->cart->subtotal)   $minimum;
            WC()->cart->total = $new_price;
        } 
    }
}
 
add_action( 'woocommerce_review_order_after_order_total', 'show_minimum_total');
add_action( 'woocommerce_cart_totals_after_order_total', 'show_minimum_total');
//Show the minimum total included in the final price
function show_minimum_total() {
    if ( WC()->cart->subtotal < get_option( 'cart_minimum_total') ) {
        echo '<tr >';
        echo '<th>'.esc_html__( 'Includes minimum price', 'myplugin' ).'</th>';
        echo '<td data-title="'.esc_attr__( 'Includes minimum price', 'myplugin' ).'">'.get_option( 'cart_minimum_total' ).' €</td>';
        echo '</tr>';
    }
}

Plus then there is the admin part where the admin can set the cart_minimum_total to 15€, but that is working fine.

My problem is, that I don't know how to include the sum in the cart/purchase in a way that it would also be included in the order when the user clicks "send order".

I assume that the minimum sum should be a similar "item/product" as the shipping is.

CodePudding user response:

Why not simply add a fee? in this way, the customer will see the amount needs to be added to meet the minimum requirement. The fee is also displayed on a separate line in all order overviews (front and backend),in the e-mail notifications and is automatically included in the order total

So you get:

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    // Set this variable to specify a minimum order value
    //$minimum = get_option( 'cart_minimum_total' );
    $minimum = 15;

    // Get subtotal
    $subtotal = $cart->subtotal;

    if ( $subtotal < $minimum ) {
        // Calculate
        $fee = $minimum - $subtotal;

        // Applying
        $cart->add_fee( __( 'Includes minimum price', 'woocommerce' ), $fee, true, '' );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
  • Related