Woocommerce区域的最低金额

时间:2020-05-03 15:26:00

标签: wordpress woocommerce

我该如何设置这种情况?

如何为按地区划分的统一费率产品添加最低订购限制?

我们只能免费送货产品。

1 个答案:

答案 0 :(得分:1)

您可以根据区域ID使用以下内容

function zone_based_minimum_amount() {
    // Only run it in Cart or Checkout pages
    if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) {
        // HERE below your targeted zone IDs
        $targeted_zones_ids = array( 1, 2 );

        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping mehod
        $chosen_method = explode(':', reset($chosen_methods) );
        $shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $chosen_method[1] );
        $zone_name = $shipping_zone->get_zone_name(); // Get the zone name
        $zone_id = $shipping_zone->get_id(); // Get the zone ID

        // Total (before taxes and shipping charges)
        $total = WC()->cart->subtotal;

         // HERE Set minimum cart total amount
        $min_total = 100;

        // Add an error notice is cart total is less than the minimum required      
        if( $total <= $min_total && in_array( $zone_id, $targeted_zones_ids ) ) {
            // Display an error message
            wc_add_notice( '<strong>' . sprintf(
                __("A minimum total purchase amount of %s is required to checkout."),
                wc_price($min_total)
            ) . '<strong>', 'error' );

            // Removing the Proceed to checkout button from the Cart page
            remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        }
    }
}
add_action( 'woocommerce_check_cart_items', 'zone_based_minimum_amount', 10, 0 );