We have an online store and I would like to modify the label below the shipping method (hook: woocommerce_cart_shipping_method_full_label) based on the stock status of the products in the cart.
Basically, if there is 1 product whose stock status is "to order", then the label for shipping method 1 = "48/72h" and the label for shipping method 2 = "48/72h".
If there is 1 product that is "limited stock", then shipping method label 1 = "24/48h" and shipping method label 2 = "48/72h".
Otherwise (that is, all products are "in stock") the label = ""Shipping by 4pm".
My closest attempt was like this: (but it checks for each product, that is, it adds a label as many times as the number of products in the cart - which is wrong)
add_filter( 'woocommerce_cart_item_name', 'custom_text_based_status_name', 10, 3 );
function custom_text_based_status_name( $item_name, $cart_item, $cart_item_key ) {
if( ! ( is_checkout() ) )
return $item_name;
$shipping_class_1 = 'onbackorder';
$shipping_class = $cart_item['data']->get_stock_status();
if( $shipping_class === $shipping_class_1 ) {
add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_methods_label', 10, 2 );
function custom_shipping_methods_label( $label, $method ) {
switch ( $method->id ) {
case 'fish_n_ships:62': //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis''); // <= Additional text
break;
case 'fish_n_ships:63': //Portugal Continental - Gratuito
$txt = __('Expedição estimada: 72 a 96h úteis'); // <= Additional text
break;
default: //nos restantes casos
$txt = __('Confirmaremos assim que possível'); // <= Additional text
}
return $label . '<br /><small style="color:#777777">' . $txt . '</small>';
}
}
return $item_name;
}`
I also tried but show critical error:
add_filter( 'woocommerce_cart_shipping_method_full_label', 'custom_shipping_method_label', 10, 3 );
function custom_shipping_method_label( $label, $method, $cart_item ) {
switch ([$method->id, $cart_item['data']->get_stock_status]) {
case ['fish_n_ships:62', 'instock']: //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis'); // <= Additional text
break;
case ['fish_n_ships:62', 'stock_limitado']: //Portugal Continental - Transportadora
$txt = __('Expedição estimada: 24 a 48h úteis'); // <= Additional text
break;
default: //nos restantes casos
$txt = __('Confirmaremos assim que possível'); // <= Additional text
}
return $label . '<br /><small style="color:#777777">' . $txt . '</small>';
}
There are mistakes and missing things in your code attempts.
To resume, you have 2 shipping methods:
fish_n_ships:62(Portugal Continental - Paid),fish_n_ships:63(Portugal Continental - Free).Your request (related to cart items):
Try the following:
Code goes in functions.php file of your child theme (or in a plugin). It should work.