Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save verygoodplugins/8908a0af208f8b6b70813a46b5c0fa7e to your computer and use it in GitHub Desktop.
Save verygoodplugins/8908a0af208f8b6b70813a46b5c0fa7e to your computer and use it in GitHub Desktop.
Allows syncing the renewal dates from multiple WooCommerce subscriptions into multiple custom fields
<?php
function wpf_subscription_products_meta_fields( $fields ) {
if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
return;
}
$args = array(
'post_type' => 'product',
'nopaging' => true
);
$products = get_posts( $args );
foreach ( $products as $product ) {
if ( WC_Subscriptions_Product::is_subscription( $product ) ) {
$fields['sub_renewal_date_' . $product->ID] = array( 'label' => 'Renewal Date - ' . $product->post_title, 'type' => 'date', 'group' => 'woocommerce_subs' );
$fields['sub_status_' . $product->ID] = array( 'label' => 'Status - ' . $product->post_title, 'type' => 'text', 'group' => 'woocommerce_subs' );
$fields['sub_start_' . $product->ID] = array( 'label' => 'Start Date - ' . $product->post_title, 'type' => 'date', 'group' => 'woocommerce_subs' );
}
}
return $fields;
}
add_filter( 'wpf_meta_fields', 'wpf_subscription_products_meta_fields' );
// Sync the renewal dates when a subscription status is updated.
function sync_subscription_renewal_dates( $update_data, $subscription ) {
foreach ( $subscription->get_items() as $line_item ) {
$product_id = $line_item->get_product_id();
$update_data[ 'sub_renewal_date_' . $product_id ] = $subscription->get_date( 'next_payment' );
$update_data[ 'sub_status_' . $product_id ] = $subscription->get_status();
$update_data[ 'sub_start_' . $product_id ] = $subscription->get_date( 'date_created' );
}
return $update_data;
}
add_filter( 'wpf_woocommerce_subscription_sync_fields', 'sync_subscription_renewal_dates', 10, 2 );
@mredodos
Copy link

mredodos commented Feb 7, 2020

thx so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment