Ali
Ali

Reputation: 315

Woocommerce subscription hooks case

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.

Upvotes: 2

Views: 1896

Answers (1)

Ian
Ian

Reputation: 590

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);

Upvotes: 3

Related Questions