更改 WooCommerce 中显示的运输方式标签中的格式化成本

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

我根据运输方式获得了补贴,但现在运输公司和我的补贴新成本同时出现。 我如何给公司方法一个新的成本?

这是我尝试过的:

/*---- Remove subsidy from displayed shipping cost -----*/
add_filter( 'woocommerce_cart_shipping_method_full_label', 'costo_del_metodo_menos_subscidio', 10, 2 );
function costo_del_metodo_menos_subscidio( $label, $method ) {
    // If shipping method cost is 0 or null, display 'Free' (except for free shipping method)
     $cost=$method->cost;   
     $subsidy = get_total_subsidies( WC()->cart->get_cart() );
    if (  ( ($cost-$subsidy) < 0 ) && $method->method_id !== 'local_pickup' ) {
        $label .= ': ' .' <span class="shipping_method gratis">' . __('¡Gratis!').'</span>';
    }
    else
    if (  ( ($cost-$subsidy) > 0 ) && $method->method_id !== 'local_pickup' ) {
        $label .= ': ' .' <span class="shipping_method gratis">' . wc_price($cost-$subsidy).'</span>';
    }
    return  $label;
}

我只想从显示的运费中删除补贴金额

php wordpress woocommerce checkout shipping-method
1个回答
0
投票

您的代码中有一些错误和复杂性,请尝试以下操作,应该显示根据补贴总额计算的新运输方式成本:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_method_full_label', 10, 2 );
function custom_shipping_method_full_label( $label, $method ) {
    if ( !in_array( $method->get_method_id(), array( 'free_shipping', 'local_pickup' ), true ) && 0 < $method->cost ) {
        $label    = $method->get_label(); // Get shipping label without cost
        $new_cost = $method->cost - get_total_subsidies( WC()->cart->get_cart() );

        if ( $new_cost > 0 ) {
            $label .= ': ' . wc_price( $new_cost );
        } else {
            $label .= ': ' .' <span class="free-cost">' . __('Free!', 'woocommerce').'</span>';
        }
    }
    return $label;
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.