Things that will need to be done:
- Unhook the
WC_Subscriptions::maybe_empty_cart()
and replace it with a similar function that doesn't empty the cart when a subscription is added (but does still callWC_Subscriptions::remove_subscriptions_from_cart()
if the cart contains a subscription (WC_Subscriptions_Cart::cart_contains_subscription()
) - You probably also want to remove the redirection to the checkout page immediately after a subscription is added to the cart. To do that, remove the
WC_Subscriptions::add_to_cart_redirect()
function from the'add_to_cart_redirect'
hook, or if you want to redirect to the cart page, use similar code to this: https://gist.github.com/thenbrent/7254424 - Make sure the totals are calculated correctly. This is the hard part. More details below.
- Make sure order line items show up correctly after purchase - I don't think you'll need to change anything here, hopefully it will just work.
To understand how totals are calculated, refer to the following functions:
WC_Subscriptions_Cart::add_calculation_price_filter()
hooked to'woocommerce_before_calculate_totals'
WC_Subscriptions_Cart::set_subscription_prices_for_calculation()
hooked to'woocommerce_get_price'
WC_Subscriptions_Cart::remove_calculation_price_filter()
hooked to'woocommerce_calculate_totals'
WC_Subscriptions_Cart::calculate_subscription_totals()
hooked to'woocommerce_calculated_total'
WC_Subscriptions_Cart::set_calculated_total()
hooked to'woocommerce_calculated_total'
Each of these has pretty good inline documentation to explain their purpose, but it will take a bit to get your head around the system as a whole and how it hooks in and where/when.
The most important thing is to understand the WC_Subscriptions_Cart::$calculation_type
flag and that multiple calculations are completed for each of a subscription's pricing elements.
Hopefully, it will be possible to set the $sign_up_fee
in WC_Subscriptions_Cart::set_subscription_prices_for_calculation()
to be: any sign up fee on the subscription in the cart plus the total base price for all the products in the cart. That way, Subscriptions/WooCommerce should run all the appropriate calculations correctly. It's a complex area though so it may be more complicated than that.
You'll need a new filter in WC_Subscriptions_Cart::set_subscription_prices_for_calculation()
for setting the sign-up fee, so I've added a 'woocommerce_subscriptions_cart_get_price'
filter (woothemes/woocommerce-subscriptions@734397c).
Other considerations:
- Let me know if you need any additional hooks and I can add them in
- Although this will start as a plugin, I'd like to merge into the main code base for 1.5 a few months down the track, so please take general usage into consideration (like supporting free trials with no sign-up fee etc.)