WooCommerce custom function for Free Shipping Label

笑着哭i 提交于 2019-12-07 02:15:20

问题


When i put the following into to my functions.php it crashes my entire site. the purpose of this function is to change

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

to this...

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

When i change the one word in the original woocommerce/includes/wc-cart-functions.php it works perfectly. I don't want it to be overwritten with an update.

/**
* 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 );
}

回答1:


if you are using latest woo commerce then following filter will be helpful to you.

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;
}



回答2:


You should rebuild the function through the filter. Using str_replace will not work on translated installs, unless each language is checked in the replacement.

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 );


来源:https://stackoverflow.com/questions/22353854/woocommerce-custom-function-for-free-shipping-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!