【问题标题】:Replace zero cost to "Free" from WooCommerce shipping method full label将 WooCommerce 运输方式完整标签中的零成本替换为“免费”
【发布时间】:2021-02-28 20:53:45
【问题描述】:

要在运费为零时显示运费,我使用以下代码(因为 woocommerce 隐藏了运费为零的运费)

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_add_zero_cost_to_shipping_label', 10, 2 );
   
function custom_add_zero_cost_to_shipping_label( $label, $method ) {
    // if shipping rate is 0, concatenate ": $0.00" to the label
    if ( ! ( $method->cost > 0 ) ) {
        $label .= ': ' . wc_price(0);
    } 
 
    // return original or edited shipping label
    return $label;
}

如何更改此代码以显示 “免费” 而不是显示 $0,00 (零成本)?

是否可以通过调整以下代码来实现?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce shipping-method


    【解决方案1】:

    当相关运输方式的成本为零时,只需使用以下将“免费”附加到运输方式标签(免费送货除外)

    add_filter( 'woocommerce_cart_shipping_method_full_label', 'Add_free_to_shipping_label_for_zero_cost', 10, 2 );
    function Add_free_to_shipping_label_for_zero_cost( $label, $method ) {
        // If shipping method cost is 0 or null, display 'Free' (except for free shipping method)
        if ( ! ( $method->cost > 0 ) && $method->method_id !== 'free_shipping' ) {
            $label .= ': ' . __('Free');
        }
        return $label;
    }
    

    代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2021-01-24
      • 2019-06-21
      • 2018-09-23
      • 1970-01-01
      • 2021-05-14
      相关资源
      最近更新 更多