I have got a subsidy based on shipping method but now both, the shipping company and my subsidy new cost appear at the same time. How do I give the company method a new cost?
Here is what I tried:
/*---- 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;
}
I want just to remove the subsidy amount from the displayed shipping cost
There are some mistakes and complications in your code, try the following instead, that should display the new calculated shipping method cost based on the subsidies total amount:
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;
}
Code goes in functions.php file of your child theme (or in a plugin). It should work.