Skip to content

Instantly share code, notes, and snippets.

@maxrice
Last active June 2, 2019 09:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxrice/7dc500cd07fa70e2fb5251293d22e485 to your computer and use it in GitHub Desktop.
Save maxrice/7dc500cd07fa70e2fb5251293d22e485 to your computer and use it in GitHub Desktop.
Jilt for WooCommerce - Clear persistent carts for customers who signed up over 30 days ago
<?php
add_filter( 'woocommerce_debug_tools', function( $tools ) {
$tools['wc_jilt_clear_persistent_carts'] = array(
'name' => __( 'Clear Persistent Carts from customers who signed up over 30 days ago.', 'jilt-for-woocommerce' ),
'button' => __( 'Clear', 'woocommerce-plugin-framework' ),
'desc' => __( 'This tool will clear the persistent cart for all registered customers who signed up over 30 days ago.', 'jilt-for-woocommerce' ),
'callback' => 'wc_jilt_clear_persistent_carts'
);
return $tools;
} );
function wc_jilt_clear_persistent_carts() {
$user_query = new WP_User_Query( array(
'date_query' => array( array( 'before' => '30 days ago', 'inclusive' => true ) )
) );
$users = $user_query->get_results();
$blog_id = get_current_blog_id();
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
// persistent carts created in WC 3.1 and below
if ( metadata_exists( 'user', $user->ID, '_woocommerce_persistent_cart' ) ) {
delete_user_meta( $user->ID, '_woocommerce_persistent_cart' );
}
// persistent carts created in WC 3.2+
if ( metadata_exists( 'user', $user->ID, '_woocommerce_persistent_cart_' . $blog_id ) ) {
delete_user_meta( $user->ID, '_woocommerce_persistent_cart_' . $blog_id );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment