Last active
June 2, 2019 09:00
-
-
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
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 | |
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