本文介绍了WooCommerce:当免费送货时,隐藏其他送货方式(本地取货除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免费送货时,我不想隐藏本地取件.删除本地取件没有任何意义,但是我不知道如何使用官方代码将其取下.

I do not want to hide Local Pickup when free shipping is available. Removing local pickup makes no sense, but I cannot figure out how to not remove it using the official code.

/**
 * Hide shipping rates when free shipping is available.
 * Updated to support WooCommerce 2.6 Shipping Zones.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }
    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

这是我尝试删除flat_rate1的尝试,因为对我来说,这是付费选项.再次,我想保持免费送货和本地取货.

This is my attempt in removing flat_rate1 since that is, for me, the paid option. Again, I want to keep FREE shipping and LOCAL pickup.

add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 );
function hide_shipping_except_local_when_free_is_available($rates) {
    $free = array();
    foreach ($rates as $rate_id => $rate) {
        if ('free_shipping' === $rate->method_id) {
            foreach($rates as $rate_id => $rate) {
            if ('flat_rate1' === $rate->method_id )
                unset($rates[ $rate_id ]);
        }
            break;
        }
    }
    return !empty( $free ) ? $free : $rates;
}

推荐答案

要在免费送货时隐藏所有送货方式(本地取货和免费送货方式除外),请使用以下命令:

To hide all shipping methods except local pickup and free shipping methods when free shipping is available, use the following:

add_filter( 'woocommerce_package_rates', 'hide_shipping_except_local_when_free_is_available', 100 );
function hide_shipping_except_local_when_free_is_available($rates) {
    $free = $local = array();

    foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $free[ $rate_id ] = $rate;
        } elseif ( 'local_pickup' === $rate->method_id ) {
            $local[ $rate_id ] = $rate;
        }
    }
    return ! empty( $free ) ? array_merge( $free, $local ) : $rates;
}

代码进入活动子主题(或活动主题)的functions.php文件中.经过测试,可以正常工作.

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

相关:

这篇关于WooCommerce:当免费送货时,隐藏其他送货方式(本地取货除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 12:52