Skip Navigation

[Resolved] Connect listings to multiple Subscriptions with WooCommerce Subscriptions

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1385129

Hi, I'm setting up a Classified Listings website where users can register and publish their business listings.

For example a Plumber will publish a listing with a logo, a description and a phone number.

On the frontend not registered user can see all the published listings and contact them.

I setup this with Toolset, where I built my Business CPT, I added all the necessary custom fields, I created forms to let registered users add their business listing and Views to display the content on the front-end.

I want to offer also a Premium listing with recurring payments, so that the registered user can add some extra custom fields to his published listing (for example more photos or website url).

This is why I bought WooCommerce Subscriptions and I follow your step by step guide here https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/how-to-use-the-woocommerce-subscriptions-plugin-with-toolset/.

Now My problem is how to associate the published listing to the premium subscription, so I can display or not Premium custom fields depending on Subscription status. The main problem is what happens with the subscription auto renew, how can I check is the subscription is still active or not?

As a user can buy more than one subscription, I cannot just use the assigned role, I need to associate a specific subscription to a specific listing.

What I'm looking for is some hook or shortcode I can use to check subscription status and ID, so I can save them in some listing custom fields when they are generated or renewed. In this way I can check them to display or not Premium fields.

I found that Views can work with Products and Orders CPTs. On WooCommerce Subscriptions documentation thay say that Subscriptions ara a special Order, but if I set up a Order view, it doesn't load Subscriptions in the loop, only regular Orders.

Can you help me?

thanks

#1385277

What I'm looking for is some hook or shortcode I can use to check subscription status and ID, so I can save them in some listing custom fields when they are generated or renewed.
Hello, if you're looking subscription action hooks you will find those in the WooCommerce Subscriptions documentation here: https://docs.woocommerce.com/document/subscriptions/develop/action-reference/
There are several subscription change actions you can hook into...none of them are specific to Toolset.

I found that Views can work with Products and Orders CPTs. On WooCommerce Subscriptions documentation thay say that Subscriptions ara a special Order, but if I set up a Order view, it doesn't load Subscriptions in the loop, only regular Orders.
Subscriptions are a private custom post type, so it is not available for Views. Subscriptions are similar to Orders, but they are not the same post type.

#1385609

Hi, thanks for your answer. I planned this setup:

- I have my Listings CPT and a Free Create form for users

- I build the Premium CPT that is connected one-to-one with Listings and has a custom field Subscription ID.

- I use the hook woocommerce_subscription_status_active to check the Woocommerce subscription active status on creation, renew or cancel. This action should:
1. Check if a Premium post with subscription ID = to Woocommerce subscription ID exists
2. If it exist delete it if the Subscription is still not active
3. If it doesn't exist create a Premium CPT post with Subscription ID equal to the current Woocommerce subscription ID

- On frontend user dashboard add a link to a Create form to create a Listings CPT with premium special fields (like extended gallery, website url, etc.) and with the option to set it Premium by connect it to a Premium post.

- If a listing is connected to Premium post, I will display premium fields.

- The user can also buy more then one subscription and get more then one Premium post to connect one-to-one to some new Listings or existing listings.

Do you think this is the correct setup? Any special advice?

thanks

#1385665

Hi, I setup this hook, it works fine:

add_action( 'woocommerce_subscription_status_updated', 'my_woocommerce_subscription_status_updated', 50, 4 );

function my_woocommerce_subscription_status_updated($subscription, $new_status, $old_status) {

   if ( $new_status == "active" ) {
     
      log_me('Subscription '.$subscription->get_id().' '.$new_status);

   } else {
     
      log_me('Subscription '.$subscription->get_id().' '.$new_status);


   }

}
#1385757

Hi, this is my final function, it works as aspected. It create or delete the Premium post depending on the Subscription status:

add_action( 'woocommerce_subscription_status_updated', 'my_woocommerce_subscription_status_updated', 50, 4 );

function my_woocommerce_subscription_status_updated($subscription, $new_status, $old_status) {

   if ( $new_status == "active" ) {
     
     $new_post = array(
       'post_title' => $subscription->get_id(),
       'post_status' => 'draft',
       'post_date' => date('Y-m-d H:i:s'),
       'post_author' => get_current_user_id(),
       'post_type' => 'premium'
     );
     $post_id = wp_insert_post($new_post);
     
     log_me('Subscription '.$subscription->get_id().' '.$new_status);

   } else {
     
     $premium_id = get_page_by_title($subscription->get_id(), OBJECT, 'premium');     
     wp_delete_post( $premium_id->ID, true );
       
     log_me('Subscription '.$subscription->get_id().' '.$new_status);


   }

}
#1386039

This code seems clear to me, but I'm not a subscriptions expert. When inserting the premium post, I can see that you're setting the post author based on get_current_user_id(). Is that always accurate? I mean, can the subscription status change without the owner logged-in to the site? If so, then the current User might not be the subscription owner. I'm not really clear about that.

#1386121

Thanks for the advice, I changed it to:

'post_author' => $subscription->get_customer_id(),

it seems it works fine.

cheers

#1386145

Great, thanks for sharing your solution for other Users!