Okay
  Public Ticket #3342064
Wrong Subtotals Again
Closed

Comments

  •  3
    Lee Whitworth started the conversation

    Hi! I'm getting the wrong subtotals issue again - there are a couple of older support threads from Nov / Dec 2022 that dealt with the issue. It was caused by a WooCommerce update.

    Now my products are showing the correct product price discounted by a B2Bking dynamic rule, but showing the full price in the subtotal field, and it is causing the grand total to be off as well. See screenshots. Can I get a fix for this ASAP?

    Attached files:  Radiance-Hazer-–-Atmospherics-Shop.png
      Cart-–-Atmospherics-Shop.png

  •   WebWizards replied privately
  •  3
    Lee Whitworth replied

    Hi Stefan,

    Thank you for taking a look. We're using the Astra theme. I researched the issue further and found this in your documentation:

    https://woocommerce-b2b-plugin.com/docs/multi-currency-support/

    I have Booster.io's Price by Country module running to convert USD prices to CAD or Euro based on the customer's shipping country and indeed prices weren't displaying correctly in the cart. In B2BKing we are using dynamic rules to give customer discounts with the option "Calculated Discounted Price Becomes Sale Price" checked, but the cart wasn't displaying the correct subtotals based on B2BKing's calculated discounts, in any currency, as shown in my previous screenshots.

    Turning off the module enabled B2BKing to work perfectly with just the USD prices. I tried out the recommended plugin (FOX Currency Switcher) but it didn't meet our needs. I even tried other currency converter plugins and ran into the same issues mentioned in your article, that I was seeing with Booster.

    After studying B2BKing's code and some trial and error I was able to come up with these snippets that help resolve the compatibility issue between the Booster module and B2BKing. With these snippets, the correct subtotals and grand totals are now displaying correctly for me in the cart, mini cart widget, checkout, backend, and order notifications... It will also display the regular price under the product name.

    // Update cart item price
    add_filter( 'woocommerce_cart_item_price', 'custom_cart_item_price', 10, 3 );
    function custom_cart_item_price( $price, $cart_item, $cart_item_key ) {
        $product = $cart_item['data'];
        $sale_price = $product->get_sale_price();     if ( $sale_price ) {
            $price = wc_price( $sale_price );
        }     return $price;
    } // Update cart item subtotal
    add_filter( 'woocommerce_cart_item_subtotal', 'custom_cart_item_subtotal', 10, 3 );
    function custom_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
        $product = $cart_item['data'];
        $sale_price = $product->get_sale_price();     if ( $sale_price ) {
            $subtotal = wc_price( $sale_price * $cart_item['quantity'] );
        }     return $subtotal;
    } // Update regular price shown in cart
    add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
    function custom_cart_item_name( $product_name, $cart_item, $cart_item_key ) {
        $product = $cart_item['data'];
        $sale_price = $product->get_sale_price();     if ( $sale_price ) {
            $regular_price = $product->get_regular_price();
            $product_name .= '<br><small><del>' . wc_price( $regular_price ) . '</del></small>';
        }     return $product_name;
    } add_filter( 'woocommerce_cart_subtotal', 'custom_cart_subtotal', 10, 3 );
    function custom_cart_subtotal( $subtotal_html, $compound, $cart ) {
        $subtotal = 0;
        foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
            $product = $cart_item['data'];
            $sale_price = $product->get_sale_price();
            $price = $product->get_price();
            $quantity = $cart_item['quantity'];
            if ( $sale_price ) {
                $subtotal += $sale_price * $quantity;
            } else {
                $subtotal += $price * $quantity;
            }
        }     return wc_price( $subtotal );
    } // Update cart total
    add_filter( 'woocommerce_cart_total', 'custom_cart_total', 10, 1 );
    function custom_cart_total( $total ) {
        $discount_total = 0;
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product = $cart_item['data'];
            $sale_price = $product->get_sale_price();
            $price = $product->get_price();
            $quantity = $cart_item['quantity'];
            if ( $sale_price ) {
                $discount_total += $sale_price * $quantity;
            } else {
                $discount_total += $price * $quantity;
            }
        }     $total = wc_price( $discount_total );
        return $total;
    }