Search code examples
phpwordpresswoocommercecartsubscriptions

Display Woocommerce Subscription recurring total in minicart


Trying to get Woocommerce Subscription recurring total to show up in the minicart. There seems to be a few different functions that might help to achieve that, but none of them seems to work out of the box. Any advice?

Echoing the following function will eventually after a few page refreshes display the recurring total (VAT incl.):

WC_Subscriptions_Cart::calculate_subscription_totals();

Problem is that it also changes the normal one-time payment subtotal (VAT excl.) to recurring subtotal (VAT excl.). Normal subtotal is echoed with WC()->cart->get_cart_subtotal().

These are other recurring total related functions that might help with solving this:

WC_Subscriptions_Cart::get_recurring_cart_contents_total();
WC_Subscriptions_Cart::display_recurring_totals();

If any help, source code can be reviewed in https://github.com/wp-premium/woocommerce-subscriptions


Solution

  • Thanks @LoicTheAztec for your answer which helped me to solve this! Managed to display recurring total with the following code as I only have monthly subscriptions. If someone has for example weekly and monthly recurring prices, this method would not work.

    $recurring_total = 0;
    
    foreach ( WC()->cart->cart_contents as $item_key => $item ){
        $item_quantity = $item['quantity'];
        $item_monthly_price = $item['data']->subscription_price;
        $item_recurring_total = $item_quantity * $item_monthly_price;
        $recurring_total += $item_recurring_total; 
    }
    
    echo $recurring_total;