2

I am working on a 'Sponsor An Orphan' project using the Woocommerce Subscriptions plugin. Monthly or yearly subscriptions are available.

When a new subscription is created, I am allocating an orphan to the subscription when payment completes like this..

add_action('woocommerce_subscription_payment_complete', 'allocate_orphans');

It correctly allocated the new orphan for a new subscription but it is also allocating an orphan on every renewal.

I think I am using wrong action hook. Which action hook should I use for new subscription for new orphan (that should not allocate the orphan on next payment)?

Any help will be appreciated.

fightstarr20
  • 11,682
  • 40
  • 154
  • 278
Ali
  • 315
  • 1
  • 3
  • 13
  • Should I use action 'woocommerce_subscription_status_active' on each subscription for each orphan? – Ali Nov 28 '17 at 17:38

1 Answers1

3

Just had the same problem myself. I think it's very strange WC haven't made this into its own hook, it would be very easy to add an else statement to the if which creates the "non-initial" payment hook that exists...

Anyway, at least we can use their logic in our action:

function assign_orphan($subscription) {

    $last_order = $subscription->get_last_order( 'all', 'any' );
    if ( false !== $last_order || wcs_order_contains_renewal( $last_order ) ) {
        //get out of here
        return;
    }

    //go ahead and allocate on initial sign up
}

add_action('woocommerce_subscription_payment_complete','assign_orphan',10,1);
Ian
  • 590
  • 2
  • 19