从 WooCommerce 购物车和结帐页面自定义运输标签

问题描述 投票:0回答:1

当值分配给运输时,我想删除购物车和结账页面中的运输标签值。例如:- 将“统一费率 100.00$”更改为“100.00$”。这意味着我只想要没有标签文本的值。你能帮我实现这个目标吗?

我已在shipping_package_rates_filter_callback函数中更改为

$rates[$targeted_rate_id]->label = '';
。但它仍然在值的开头显示“:”(冒号符号)。例如:- :100.00$ .

shipping_package_rates_filter_callback

add_filter( 'woocommerce_package_rates', 'shipping_package_rates_filter_callback', 100, 2 );
function shipping_package_rates_filter_callback( $rates, $package ) {
    // HERE define your targeted rate id
    $targeted_rate_id = 'flat_rate:4'; 
    
    if ( ! array_key_exists($targeted_rate_id, $rates) ) 
        return $rates;
    
    $new_cost = WC()->session->get('shipping_cost'); // Get new cost from WC sessions

    if ( ! empty($new_cost) ) {
        // Set shipping label empty
        $rates[$targeted_rate_id]->label = '';
        $rate = $rates[$targeted_rate_id];
        $cost = $rate->cost; // Set the initial cost in a variable for taxes calculations
        $rates[$targeted_rate_id]->cost = $new_cost; // Set the new cost

        $has_taxes = false; // Initializing
        $taxes     = array(); // Initializing
        foreach ($rate->taxes as $key => $tax_cost){
            if( $tax_cost > 0 ) {
                $tax_rate    = $tax_cost / $cost; // Get the tax rate conversion
                $taxes[$key] = $new_cost * $tax_rate; // Set the new tax cost in taxes costs array
                $has_taxes   = true; // There are taxes (set it to true)
            }
        }
        if( $has_taxes ) {
            $rates[$targeted_rate_id]->taxes = $taxes; // Set taxes costs array
        }
    } else {
        unset($rates[$targeted_rate_id]);
    }
    return $rates;
}
php wordpress woocommerce hook-woocommerce shipping-method
1个回答
0
投票

要从显示的运输方式中删除冒号符号

": "
,请使用以下代码:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'display_only_shipping_method_price', 10, 2 );
function display_only_shipping_method_price( $label, $method ) {
    // HERE define your targeted rate id
    $targeted_rate_id = 'flat_rate:4';

    if ( $method->id === $targeted_rate_id && 0 < $method->cost ) {
        $label = str_replace( $method->label . ': ', '', $label);
    }
    return $label;
}

相关:

© www.soinside.com 2019 - 2024. All rights reserved.