WooCommerce - receive active subscriptions in the list between the start and end dates

I use WooCommerce subscriptions.

How can I get all subscriptions (with start date and end date) in lists from one specific date to another date?

Example 09/09/2016 to 09/15/2016

thanks

+2
source share
1 answer

Here is an example of a function that displays a list of all active subscribers from one date to another in a html formatted table. In this example, you will receive for each subscription: the subscription ID, date, customer ID and customer name (you can configure the code to get what you want).

Thus, this function has 2 date parameters. For use and specifications, see the section at the end.

Function Code:

function active_subscription_list($from_date=null, $to_date=null) { // Get all customer orders $subscriptions = get_posts( array( 'numberposts' => -1, 'post_type' => 'shop_subscription', // Subscription post type 'post_status' => 'wc-active', // Active subscription 'orderby' => 'post_date', // ordered by date 'order' => 'ASC', 'date_query' => array( // Start & end date array( 'after' => $from_date, 'before' => $to_date, 'inclusive' => true, ), ), ) ); // Styles (temporary, only for demo display) should be removed echo "<style> .subscription_list th, .subscription_list td{border:solid 1px #666; padding:2px 5px;} .subscription_list th{font-weight:bold} .subscription_list td{text-align:center} </style>"; // Displaying list in an html table echo "<table class='shop_table subscription_list'> <tr> <th>" . __( 'Number ID', 'your_theme_domain' ) . "</th> <th>" . __( 'Date', 'your_theme_domain' ) . "</th> <th>" . __( 'User ID', 'your_theme_domain' ) . "</th> <th>" . __( 'User Name', 'your_theme_domain' ) . "</th> </tr> "; // Going through each current customer orders foreach ( $subscriptions as $subscription ) { $subscription_id = $subscription->ID; // subscription ID $subscription_date = array_shift( explode( ' ', $subscription->post_date ) ); // Date $subscr_meta_data = get_post_meta($subscription->ID); $customer_id = $subscr_meta_data['_customer_user'][0]; // customer ID $customer_name = $subscr_meta_data['_billing_first_name'][0] . ' ' . $subscr_meta_data['_billing_last_name'][0]; echo "</tr> <td>$subscription_id</td> <td>$subscription_date</td> <td>$customer_id</td> <td>$customer_name</td> </tr>"; } echo '</table>'; } 

This code is in the function.php file of your active child theme (or theme), as well as in any plug-in file.


USE (example):

You will have to follow this numeric date format: YEAR-MONTH-DAY

 $from_date = '2016-06-19'; // start date $to_date = '2016-09-21'; // End date active_subscription_list($from_date, $to_date); 

This will display a list of all active subscribers from 2016-06-19 to 2016-09-21 ...

This code has been verified and works.

+8
source

Source: https://habr.com/ru/post/1264600/


All Articles