Home > Back-end >  Do Not Fire function after page reload - Wordpress
Do Not Fire function after page reload - Wordpress

Time:11-08

add_action( 'woocommerce_thankyou', 'orderReceived' );
function orderReceived($order_id)
{
    $order = wc_get_order($order_id);

    if (!$order->has_status('failed') && did_action('woocommerce_thankyou') === 1) {
        addPoints(1000, 'add', 'order');
    }
}

How can I make sure the above function fires only once on the woocommerce thankyou page?

At the moment it fires everytime I reload the thank you page. This means that someone can get infinite points.

CodePudding user response:

You can use order meta data to check whether the points is added already or not.

Try out this code :-

$points_check = $order->get_meta('_points_added');

If($points_check != 'added'){
$order->update_meta_data( '_points_added', 'added' );
addPoints(1000, 'add', 'order');
}
  • Related