2
votes

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

2

2 Answers

3
votes

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;
2
votes

The only available functions related to cart and checkout are in WooCommerce Subscriptions developer documentation

Now I think that what you are looking for is the related subscription data that is remaining in cart. For that, is pretty easy to output the raw data of the cart object (once you have add to cart before a subscription). Then you can use this code to output this data:

1) Outputting the entire cart object raw data:

print_r(WC()->cart);

And then you can use all the WC_cart methods like for example:

WC()->cart->get_cart_contents_count( );

2) Outputing the cart items in a foreach loop:

foreach ( WC()->cart->cart_contents as $item_key => $item ){
    print_r($item); echo '<br><br>'; // displaying the item raw data
    print_r($item['data']); echo '<br><br>'; // displaying the subscription object raw data
    echo $item['line_total']; echo '<br>'; // displaying item total
    echo $item['data']->subscription_price . '<br>'; // displaying subscription object total
}

Once you have looked to this raw data in details, you will understand that for displaying totals in mini cart, you might just need to use the WC()->cart object with WC_Cart dedicated methods like:

WC()->cart->get_cart_total( );

To display