I've a function that fetches more than 2000+ Mailchimp contacts, so it's taking almost 30s time to run this query and to do some other things, this query is being run after placing an order from the checkout page.
But what I'm trying to do is, the Mailchimp contacts query needs to be run once the thank-you page is completely loaded, otherwise right now it's taking that 30s time to load the thank-you page, which is not user-friendly. But I'm not getting any way to run the function only after loading the thank-you page content, not before it.
I'm currently using this hook
woocommerce_thankyou
Thanks in advance!
You can use the woocommerce_thankyou
action hook that called after content load. check below code. code will goto active theme funcions.php file.
add_action( 'woocommerce_thankyou', 'run_mailchimp_query' );
function run_mailchimp_query( $order_id ) {
// your mailchimp code.
}
You can also use the wp_footer
action hook.
add_action( 'wp_footer', 'run_mailchimp_query' );
function run_mailchimp_query() {
// returm early if is not on the Thank you page.
if( !is_wc_endpoint_url( 'order-received' ) ) return;
// your mailchimp code.
}
Also, you can use AJAX
add_action( 'woocommerce_thankyou', 'run_mailchimp_query' );
function run_mailchimp_query( $order_id ) {
?>
<script type="text/javascript">
(function($){
$(document).ready(function(){
$.ajax({
url: '<?php echo admin_url('admin-ajax.php'); ?>',
type : 'post',
data: {
'action' : 'run_mailchimp_query'
'order_id': '<?php echo $order_id; ?>'
},
success: function( data) {
}
});
});
})(jQuery);
</script>
<?php
}
add_action("wp_ajax_run_mailchimp_query", "run_mailchimp_query");
add_action("wp_ajax_nopriv_run_mailchimp_query", "run_mailchimp_query");
function run_mailchimp_query(){
$order_id = $_POST['order_id'];
create_coupon_based_on_user_registration( $order_id );
wp_send_json_success();
}
Also You can use the CRON job and then get all orders. it's better to run create_coupon_based_on_user_registration
in cron job.
function add_five_minute_cron_schedules($schedules){
if( !isset( $schedules["five_minute"] ) ){
$schedules["five_minute"] = array(
'interval' => 5*60,
'display' => __('Once every 5 minutes')
);
}
return $schedules;
}
add_filter('cron_schedules','add_five_minute_cron_schedules');
if( ! wp_next_scheduled( 'create_coupon_based_on_user_registration_cron' ) ){
wp_schedule_event( time(), 'five_minute', 'create_coupon_based_on_user_registration_cron' );
}
//Hook into that action that'll fire every 5 minutes.
add_action( 'create_coupon_based_on_user_registration_cron', 'create_coupon_based_on_user_registration_callback' );
function create_coupon_based_on_user_registration_callback(){
$args = array(
'post_status' => array( 'wc-processing' ),
'posts_per_page' => -1,
'meta_key' => 'coupon_created',
'meta_compare' => 'NOT EXISTS'
);
$orders = wc_get_orders($args);
if( !empty( $orders ) ){
// Loop through each $order objects.
foreach( $orders as $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
create_coupon_based_on_user_registration( $order_id );
update_post_meta( $order_id, 'coupon_created', 1 );
}
}
}