WooCommerce免费送货标签的自定义功能

6
当我把以下内容放进我的functions.php中时,整个网站都会崩溃。这个函数的目的是改变。
elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( '**Free**', 'woocommerce' ) . ')';

转换为...

elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( '**To Be Calculated**', 'woocommerce' ) . ')';

当我在原始的woocommerce/includes/wc-cart-functions.php中更改一个单词时,它可以完美地工作。我不希望它在更新时被覆盖。
/**
* Get a shipping methods full label including price
* @param  object $method
* @return string
*/
function wc_cart_totals_shipping_method_label( $method ) {
$label = $method->label;

if ( $method->cost > 0 ) {
    if ( WC()->cart->tax_display_cart == 'excl' ) {
        $label .= ': ' . wc_price( $method->cost );
        if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
            $label .= ' <small>' . WC()->countries->ex_tax_or_vat() . '</small>';
        }
    } else {
        $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
        if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
            $label .= ' <small>' . WC()->countries->inc_tax_or_vat() . '</small>';
        }
    }
} elseif ( $method->id !== 'free_shipping' ) {
    $label .= ' (' . __( 'To Be Calculated', 'woocommerce' ) . ')';
}

return apply_filters( 'woocommerce_cart_shipping_method_full_label', $label, $method );
}
2个回答

9
如果您正在使用最新的 WooCommerce,则以下筛选器将对您有帮助。
add_filter( 'woocommerce_cart_shipping_method_full_label', 'remove_local_pickup_free_label', 10, 2 );
function remove_local_pickup_free_label($full_label, $method){
    $full_label = str_replace("(Free)","(TBD)",$full_label);
return $full_label;
}

这个代码在主题的function.php中可以正常工作,但是在我的插件中却无法正常工作。有什么想法吗? - Anurag Khandelwal

3
你需要通过过滤器重新构建该函数。使用 str_replace 在翻译安装中无法正常工作,除非替换内容检查每种语言。
function ua_woocommerce_cart_shipping_method_full_label( $label, $method ) {
$label = $method->label;

if ( $method->cost > 0 ) {
    if ( WC()->cart->tax_display_cart == 'excl' ) {
        $label .= ': ' . wc_price( $method->cost );
        if ( $method->get_shipping_tax() > 0 && WC()->cart->prices_include_tax ) {
            $label .= ' <small class="tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
        }
    } else {
        $label .= ': ' . wc_price( $method->cost + $method->get_shipping_tax() );
        if ( $method->get_shipping_tax() > 0 && ! WC()->cart->prices_include_tax ) {
            $label .= ' <small class="tax_label">' . WC()->countries->inc_tax_or_vat() . '</small>';
        }
    }
} 

return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'ua_woocommerce_cart_shipping_method_full_label', 10, 2 );

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接