Allows syncing the renewal dates from multiple WooCommerce subscriptions into multiple custom fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx so much!