WooCommerce Minimum Order Amount

How to set minimum order amount per distance to customer street?
The WooCommerce Distance Rate Shipping plugin is required updates the distances and the minimum amounts (if local pickup is available replace the ‘woocommerce_no_shipping_available_html’ filter with ‘woocommerce_checkout_process’):

add_action( 'woocommerce_check_cart_items', 'wc_required_min_cart_subtotal_amount' );
add_filter( 'woocommerce_distance_rate_shipping_rule_cost_distance_shipping', 'wc_distance_rate_conditions', 10, 4 );
add_filter( 'woocommerce_package_rates', 'wc_hide_distance_rate' );
add_filter( 'woocommerce_no_shipping_available_html', 'wc_set_no_shipping_message' );
add_action( 'woocommerce_cart_has_errors', 'wc_set_checkout_message' );

function wc_required_min_cart_subtotal_amount() {
	$min_order = 200;
	if ( WC()->cart->get_subtotal() < $min_order ) {
		wc_add_notice( '<strong>' . 'The minimum order is ' . wc_price( $min_order ) . ', feel free to select more items.' . '<strong>' . ' <a class="button wc-backward" href="' . wc_get_page_permalink( 'shop' ) . '">Return</a>', 'error' );
	}
}

function wc_distance_rate_conditions( $rule_cost, $rule, $distance, $package ) {
	$min_order = '';
	$rule_cost = '';
	if ( 5 >= $distance ) {
		$min_order = 200;
		$rule_cost = 15;
	} elseif ( 10 >= $distance ) {
		$min_order = 300;
		$rule_cost = 20;
	}
	WC()->session->set( 'order_below_min', empty( $min_order ) || $package['contents_cost'] < $min_order ? true : false );
	WC()->session->set( 'min_order', $min_order );
	return $rule_cost;
}

function wc_hide_distance_rate( $rates ) {
	if ( WC()->session->get( 'order_below_min' ) ) {
		foreach ( $rates as $rate_id => $rate ) {
			if ( 'distance_rate' === $rate->method_id ) {
				unset( $rates[ $rate_id ] );
			}
		}
	}
	return $rates;
}

function wc_set_no_shipping_message( $message ) {
	$min_order = WC()->session->get( 'min_order' );
	return $min_order ? 'The minimum order for ' . WC()->customer->get_billing_address_1() . ' street is ' . wc_price( $min_order ) . ' <a class="button wc-backward" href="' . wc_get_page_permalink( 'shop' ) . '">return</a>' : $message;
}

function wc_set_checkout_message() {
	echo '<p>The minimum order is 200 USD. Your order: ' . WC()->cart->get_cart_subtotal() . '</p>' . ' <a class="button wc-backward" href="' . wc_get_page_permalink( 'shop' ) . '">Return</a>';
}

Leave a comment

Your email address will not be published. Required fields are marked *