user14906105
user14906105

Reputation:

Subtracting cart subtotal to cart total error issue In WooCommerce

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;
}

Upvotes: 2

Views: 699

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254378

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.

Upvotes: 1

Related Questions