仅在结帐页面上更改 WooCommerce 送货方式标签

Change WooCommerce shipping method label only on checkout page

我试图只在结账页面上更改单选按钮的标签,而不是在购物车页面上。标签出现在两个页面上。

当我输入以下代码时,它会更改结帐页面上的标签,但会使购物车页面变为空白。

add_filter( 'woocommerce_cart_shipping_method_full_label', 'change_shipping_label', 10, 2 );
function change_shipping_label( $full_label, $method ){
    if( ! is_checkout()) return; // Only on checkout page?

    $full_label = str_replace( "Custom Carrier (Enter Details Next Page)", "Custom Carrier", $full_label );

    return $full_label;
}

谁知道这是为什么?

你其实return什么都没有,因为你只用return;。虽然应该是 return $label;

  • is_checkout() - Returns 结帐页面为真。
  • str_replace - 用替换字符串替换所有出现的搜索字符串

所以你得到:

function filter_woocommerce_cart_shipping_method_full_label( $label, $method ) {
    // NOT returns true on the checkout page.
    if ( ! is_checkout() )
        return $label;

    $label = str_replace( "Custom Carrier (Enter Details Next Page)", "Custom Carrier", $label );

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