dongqian9567 2018-03-01 03:16
浏览 73

隐藏运费统一费率“当Woocommerce免费送货时

I'm new so if I goof in how I post, please guide me... also am not a programmer... I tried the code given in two other threads but it didn't quite do the trick. I understood by the documentation that I was not to seek clarification on those threads, so I hope it's ok that I'm opening a new one...

Website: www.leannacinquanta.com/shop, Woocommerce plugin latest version. When I inserted the following two code snippets into the Custom CSS area in my premium Wp theme "X Theme" it did not result in any change to my shopping cart. I would deeply appreciate your assistance:

/*WOOCOMMERCE hide"calculate shipping"*/
add_filter('woocommerce_product_needs_shipping', function(){return false;});

/*WOOCOMMERCE remove Flat Rate from view when order is over $50 which qualifies for free shipping */
add_filter('woocommerce_package_rates', 'hide_flat_rate_based_on_cart_total', 10, 3);
function hide_flat_rate_based_on_cart_total( $available_shipping_methods, $package ){
    $price_limit = 50;
    if( WC()->cart->get_total() > $price_limit ){
        foreach($available_shipping_methods as $method_key  =>  $method){
            if ( strpos($method->method_id, 'flat_rate' ) !== false) {
                unset($available_shipping_methods[$method_key]);
            }
        }
    }
    return $available_shipping_methods;
}
  • 写回答

1条回答 默认 最新

  • dongzg2006 2018-03-01 13:15
    关注

    There is 2 things that are making an issue in your code:

    • Your first function removes shipping everywhere (where it should only need to be done in cart page)
    • The 2nd function should need to use cart subtotal (Is not possible to make that on cart total and it's not accessible).

    First you should need to set your free shipping as follow with your desired limit amount:

    enter image description here

    Once done, this code instead:

    // Hide shipping in cart page.
    add_filter( 'woocommerce_product_needs_shipping', 'disable_shipping_in_cart_page', 10, 2 );
    function disable_shipping_in_cart_page( $needs_shipping, $product ){
        if ( is_cart() )
            $needs_shipping = false;
    
        return $needs_shipping;
    }
    
    // Conditionally remove Flat Rate which qualifies for free shipping
    add_filter( 'woocommerce_package_rates', 'hide_flat_rate_when_free_is_available', 10, 3 );
    function hide_flat_rate_when_free_is_available( $rates, $package ) {
        $free = array();
        $free_available = false;
    
        foreach ( $rates as $rate_key => $rate ) {
            if ( 'free_shipping' === $rate->method_id ){
                $free_available = true;
                break;
            }
        }
    
        foreach ( $rates as $rate_key => $rate ) {
            if ( $free_available && 'flat_rate' === $rate->method_id )
                unset($rates[$rate_key]);
        }
    
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    You should need to refresh the shipping caches:
    1) First this code is already saved on your function.php file.
    2) In Shipping settings, enter in a Shipping Zone and disable a Shipping Method and "save". Then re-enable that Shipping Method and "save". You are done.

    Instead using the 1st function you can also remove the shipping calculator from cart page in:
    Woocommerce > Settings > Shipping > shipping options (tab).

    Code is tested and works.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题