Niki van Stein
Niki van Stein

Reputation: 10744

Subscription Frequency and Price Change hook in Woocommerce Subscriptions

Maybe I overlooked it, but as much as I searched, I could not find which action to hook when a subscription changes price or frequency in Woocommerce Subscriptions.

The documentation says that to support price changes in your payment gateway you have to list subscription_amount_changes, but nowhere it says which function will be called when the amount actually changes..

Also in the Action reference I was unable to find an action hook which is called when the subscription amount or frequency changes. If anyone has a clue which hook to use or how to implement this particular feature, please tell me.


Edit

Ok thanks for the comments and answer by @Reigel, so if I understand correctly the change of a subscription in the admin menu (which I indeed refer to), has to be handled by the save_post action. Could you provide a small example how to hook this action and check if it is a subscription and get the $order_id (I guess this is the same as post_id?) to use in the subscription management calls?

Thank you very much already!

Upvotes: 2

Views: 2162

Answers (2)

James Jones
James Jones

Reputation: 3899

This should be considered an addon to the answer by @Reigel. If you upvote this, upvote his answer too.

Here's an example of hooking the pre_post_update action. It occurs a little before the save_post action. Both actions are triggered in the wp_insert_post() function in post.php.

function post_save_subscription_check( $post_ID, $data )
{
    if( $data['post_type'] == 'product' ) {
        if (!empty($_POST['_subscription_price']) && get_post_meta($post_ID, '_subscription_price', true) != $_POST['_subscription_price']) {
                /* do stuff here */
        }
        if (!empty($_POST['_subscription_period']) && get_post_meta($post_ID, '_subscription_period', true) != $_POST['_subscription_period']) {
                /* do stuff here */
        }
    }
}
add_action('pre_post_update', 'post_save_subscription_check', 10, 2 );
  • In the logic we are checking for the old value, obtained with get_post_meta() and the new value, held in the $_POST variable and comparing them.
  • This code only executes when a post updates, not for a new post
  • The code gets placed in your theme functions.php or custom plugin code.
  • In live code I would recommend cleaning any $_POST data before using it. I haven't bothered here.

Upvotes: 2

Reigel Gallarde
Reigel Gallarde

Reputation: 65284

I will try to explain about supports.

subscription_amount_changes is just a support and will not fire anything. You can use it for conditional statements, like:

if ( !$chosen_gateway->supports( 'subscription_amount_changes' )) { 
     echo 'Please be considerate and do not change the price for the chosen gateway does not support it.';
}

now, other plugins can then check if the chosen gateway supports subscription_amount_changes and do their appropriate actions.

action hook which is called when the subscription amount or frequency changes

subscription is just a product type. Which means this is just a post with a post_type of product. The amount and frequency are just post meta. all are handled on save_post action. add_action( 'save_post', __CLASS__ . '::save_subscription_meta', 11 );. This is on the post_type=product. You have to check also for save_post on post_type=shop_order as it's more appropriate for checking support. Because there's already a gateway chosen.

Upvotes: 1

Related Questions