#Overview
WP Fusion includes several batch tools that allow you to perform operations in bulk, such as importing users, exporting user meta data, and syncing historical WooCommerce orders to your CRM.
The batch system is based on WP Background Processing, and it can be modified or extended via the use of filters.
#How it works
The batch system makes use of three filters:
wpf_export_options: Registers the operation title and slug, and adds it to the list of available batch tools on the Advanced settings tab
wpf_batch_{slug}_init: Queries the objects IDs (user IDs, post IDs, etc) to be processed and returns them as an array
wpf_batch_{slug}: Is called on each record in the queue
The available batch operations will vary depending on which plugins are active. It』s also possible to register your own batch operations.
#Examples
#Limit the WooCommerce orders export by date
By default the WooCommerce orders export will query all orders that have yet to be processed by WP Fusion (indicated by the wpf_complete postmeta value).
This filter runs on the same operation slug (woocommerce), but at priority 20. This overrides WP Fusion』s built in query, and does a new query only on orders placed since March 1st 2020.
function my_custom_export( $order_ids ) {
$args = array(
'numberposts' => - 1,
'post_type' => 'shop_order',
'post_status' => array( 'wc-processing', 'wc-completed' ),
'fields' => 'ids',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'wpf_complete',
'compare' => 'NOT EXISTS',
),
),
'date_query' => array(
'after' => 'March 1 2020',
),
);
$order_ids = get_posts( $args );
wpf_log( 'info', 0, 'Beginning WooCommerce Orders batch operation on ' . count( $order_ids ) . ' orders', array( 'source' => 'batch-process' ) );
return $order_ids;
}
add_filter( 'wpf_batch_woocommerce_init', 'my_custom_export', 20 );
#Export Easy Digital Downloads order date by month
Syncs the order date and time to the connected CRM for all non-recurring EDD payments made in November of the current year.
/**
* Register the export option.
*
* @return array Options
*/
function edd_export_options( $options ) {
$options['edd_orderdate'] = array(
'label' => __( 'EDD Order Date', 'wp-fusion' ),
'title' => __( 'Orders', 'wp-fusion' ), //
'tooltip' => __( 'Custom: Sync order dates for non-recurring orders in the month of November.', 'wp-fusion' ),
);
return $options;
}
add_filter( 'wpf_export_options', 'edd_export_options' );
/**
* Query the payment IDs to be processed.
*
* @return array Payment IDs
*/
function edd_batch_init() {
$args = array(
'number' => -1,
'fields' => 'ids',
'monthnum' => 11,
'year' => date( 'Y' ),
'post_status' => 'publish',
);
$payments = edd_get_payments( $args );
return $payments;
}
add_filter( 'wpf_batch_edd_orderdate_init', 'edd_batch_init' );
/**
* Sync the order date for each payment
*/
function edd_batch_step( $payment_id ) {
$payment = new EDD_Payment( $payment_id );
$update_data = array(
'order_date' => $payment->get_meta( '_edd_completed_date' ),
);
wp_fusion()->user->push_user_meta( $payment->user_id, $update_data );
}
add_action( 'wpf_batch_edd_orderdate', 'edd_batch_step' );
#Pull user meta for users who registered before a specific date
This example limits the Pull User Meta operation just to users who registered after January 1st 2019.
function limit_by_user_registered( $user_ids ) {
// At this point $user_ids is all users with a CRM contact ID
foreach ( $user_ids as $i => $user_id ) {
$user = get_userdata( $user_id );
if ( strtotime( $user->user_registered ) 'Resync Tags (No Contact ID)',
'title' => 'Users',
'tooltip' => 'Resyncs the contact ID and tags just for users that don't have a stored contact ID.',
);
return $options;
}
add_filter( 'wpf_export_options', 'no_cid_export_options' );
/**
* No contact ID batch init
*
* @return array Users
*/
function no_cid_init() {
$args = array(
'fields' => 'ID',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => wp_fusion()->crm->slug . '_contact_id',
'compare' => 'NOT EXISTS',
),
array(
'key' => wp_fusion()->crm->slug . '_contact_id',
'value' => false,
),
),
);
$users = get_users( $args );
return $users;
}
add_action( 'wpf_batch_no_cid_init', 'no_cid_init' );
/**
* No contact ID batch - single step
*
* @return void
*/
function no_cid_step( $user_id ) {
wp_fusion()->user->get_tags( $user_id, true );
}
add_action( 'wpf_batch_no_cid', 'no_cid_step' );
#Was this helpful?
Let us know if you liked the post. That』s the only way we can improve.
Yes
No