Akhtarujjaman Shuvo
Akhtarujjaman Shuvo

Reputation: 690

Avoid fatal error on update_checkout event when Calculating Shipping in WooCommerce

I want to calculate shipping when update_checkout triggered. i used below code in my plugin function

function action_woocommerce_checkout_update_order_review($array, $int)
{
    WC()->cart->calculate_shipping();
    return;
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 2);

when i updating checkout i am getting below Fatal error on AJAX response

<br />
<b>Fatal error</b>:  Uncaught ArgumentCountError: Too few arguments to function action_woocommerce_checkout_update_order_review(), 1 passed in C:\xampp\htdocs\woo\wp-includes\class-wp-hook.php on line 286 and exactly 2 expected in C:\xampp\htdocs\woo\wp-content\plugins\wooinstant\inc\wooinstant-functions.php:82
Stack trace:
#0 C:\xampp\htdocs\woo\wp-includes\class-wp-hook.php(286): action_woocommerce_checkout_update_order_review('billing_first_n...')
#1 C:\xampp\htdocs\woo\wp-includes\class-wp-hook.php(310): WP_Hook-&gt;apply_filters('', Array)
#2 C:\xampp\htdocs\woo\wp-includes\plugin.php(453): WP_Hook-&gt;do_action(Array)
#3 C:\xampp\htdocs\woo\wp-content\plugins\woocommerce\includes\class-wc-ajax.php(281): do_action('woocommerce_che...', 'billing_first_n...')
#4 C:\xampp\htdocs\woo\wp-includes\class-wp-hook.php(286): WC_AJAX::update_order_review('')
#5 C:\xampp\htdocs\woo\wp-includes\class-wp-hook.php(310): WP_Hook-&gt;apply_filters('', Array)
#6 C:\xampp\htdocs\woo\wp-includes\plugin.php(453): WP_Hook-&gt;do_action(Array)
#7 C:\xampp\htd in <b>C:\xampp\htdocs\woo\wp-content\plugins\wooinstant\inc\wooinstant-functions.php</b> on line <b>82</b><br />

How can I calculate shipping on update_checkout? Any help is appreciated.

Upvotes: 1

Views: 1275

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253919

The woocommerce_checkout_update_order_review hook, is an action hook with a unique argument. As it's an action hook, nothing is need to be returned as the filter hooks does.

Try this revisited code version, tested without any thrown errors:

add_action( 'woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1 );
function action_woocommerce_checkout_update_order_review( $posted_data ) {
    WC()->cart->calculate_shipping();
}

Upvotes: 1

Related Questions