Based on the answer code from my previous question Add WooCommerce cart link + total as last menu item in wp_nav_menu, I am now trying to add shipping as an additional argument to the link.
I needed an if statement, making sure the cart is not empty. I then thought that it would be easiest to just to a simple calculation and that way, get the shipping total (order total minus subtotal).
Problem is; I get a two notices saying Warning: A non-numeric value encountered
. The warning refers to this line:
$shipping_total = wc_price($order_total - $order_subtotal);
This is the code:
add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {
$order_total = wc_price(WC()->cart->get_total());
$order_subtotal = wc_price(WC()->cart->get_subtotal());
$shipping_total = wc_price($order_total - $order_subtotal);
// cart url
$link_url = wc_get_cart_url();
// icon, product count, subtotal and shipping (based on if statement)
if (WC()->cart->is_empty()){
$link_text = sprintf( __( '<i class="shopping-cart"></i> (0)', 'woocommerce' ));
} else {
$link_text = sprintf( __( '<i class="shopping-cart"></i> %d - %s (%s)', 'woocommerce' ), WC()->cart->cart_contents_count, wc_price(WC()->cart->get_subtotal()), $shipping_total);
}
// link
$minicart_link = '<a title="View my cart" class="wfminicart" href="' . $link_url . '">' . $link_text . '</a>';
// return the link as last menu item
return $items . $minicart_link;
}
You need to remove wc_price()
formatting function from the 2 first variables and to replace WC()->cart->get_total()
with WC()->cart->total
to avoid This issue.
So your code is going to be:
add_filter( 'wp_nav_menu_header-menu_items', 'minicart_link_with_product_count_subtotal_and_shipping', 10, 2 );
function minicart_link_with_product_count_subtotal_and_shipping( $items, $args ) {
$order_total = WC()->cart->total; // <= Here non formatted total
$order_subtotal = WC()->cart->get_subtotal();
$shipping_total = wc_price($order_total - $order_subtotal);
// cart url
$link_url = wc_get_cart_url();
// icon, product count, subtotal and shipping (based on if statement)
if (WC()->cart->is_empty()){
$link_text = sprintf( __( '<i class="shopping-cart"></i> (0)', 'woocommerce' ));
} else {
$link_text = sprintf( __( '<i class="shopping-cart"></i> %d - %s (%s)', 'woocommerce' ), WC()->cart->cart_contents_count, wc_price(WC()->cart->get_subtotal()), $shipping_total);
}
// link
$minicart_link = '<a title="View my cart" class="wfminicart" href="' . $link_url . '">' . $link_text . '</a>';
// return the link as last menu item
return $items . $minicart_link;
}
It should work without errors.