Getting minimum order amount for 'Free Shipping' method in checkout page

前端 未结 5 1765
醉话见心
醉话见心 2021-01-06 02:42

I have did tried to use the code from this answer:
How to get minimum order amount for free shipping in woocommerce

But it return a NULL

5条回答
  •  日久生厌
    2021-01-06 03:01

    These 2 filters allow you to get the minimum order amount or even to change its value:

    • woocommerce_shipping_free_shipping_instance_option
    • woocommerce_shipping_free_shipping_option

    You could use it like this for example:

    add_filter( 'woocommerce_shipping_free_shipping_instance_option', 'get_free_shipping_min_amount', 10, 3 );
    add_filter( 'woocommerce_shipping_free_shipping_option', 'get_free_shipping_min_amount', 10, 3 );
    function get_free_shipping_min_amount( $option, $key, $method ){
        if (
            'min_amount' !== $key ||
            ! is_numeric( $option )     
        ) {
            return $option;
        }
        // Minimum amount
        $min_amount = $option;
        return $option;
    }
    

    You can get more info from the shipping method using the $method param like instance_id or something like that if you wish.

    You can also replace 'free_shipping' from the hook to get info from other shipping methods. The hook works like this: 'woocommerce_shipping_' . $shipping_method_id . '_option'.

    You can take a look at it on the documentation:

    • https://docs.woocommerce.com/wc-apidocs/source-class-WC_Shipping_Method.html#469
    • https://docs.woocommerce.com/wc-apidocs/source-class-WC_Shipping_Method.html#495

提交回复
热议问题