Hook after subscription added to cart for Woocommerce Subscriptions

During this week I’ve been working on a special feature for one client’s store and found this useful hooks in the Woocommerce Subscriptions plugin, so I want to share what I’ve learned.

If you want to do something after a subscription is added, you can use any of this four hooks:

  • woocommerce_setup_cart_for_subscription_initial_payment
  • woocommerce_setup_cart_for_subscription_renewal
  • woocommerce_setup_cart_for_subscription_resubscribe
  • woocommerce_setup_cart_for_subscription_switch

Each one of the hooks are triggered depending on the status of the subscription, e.g. if is the first time the subscription is purchased the subscription_initial_payment is triggered.

The woocommerce_setup_cart_for_ hook is triggered at the end of the method setup_cart under the class WCS_Cart_Renewal.

Using this hook you can do anything after the product is added to the cart, this hook has two parameters available:

  • $subscription WC_Subscription with the subscription.
  • $cart_item_data array with the cart item data.

Example add a coupon code automatically if not purchasing a trial subscription

function add_coupon_code_for_subscription($subscription, $cart_item_data){
	if(  wcs_get_line_items_with_a_trial($subscription) == false ){
add_action("woocommerce_setup_cart_for_subscription_initial_payment", "add_coupon_code_for_subscription");

Leave a Comment