Migrating from the deprecated hooks to woocommerce_get_script_data

Everything began with a comment on my tutorial about password strength meter customization in WooCommerce.

In that tutorial I used wc_password_strength_meter_params action hook, but since WooCommerce 3.3 this hook cause a PHP warning (notice).

I decided to google it, but I didn’t find any information at all 👻 After that I dived into WooCommerce source code and I found out that all of the below hooks have been deprecated since Woo 3.3:

Every hook from the above list has to be replaced with this one – woocommerce_get_script_data.

I will show you how! Let’s say, that we use something like this on our website.

add_filter( 'wc_checkout_params', 'misha_hook_before' );
 
function misha_hook_before( $data ) {
 
	// do stuff with the $data array and return it
 
}

So, here is what we have to do:

add_filter( 'woocommerce_get_script_data', 'misha_hook_after', 10, 2 );
 
function misha_hook_after( $data, $handle ) {
 
	switch( $handle ) :
		case 'woocommerce':
			// do stuff with the $data array
			break;
		case 'wc_checkout':
			// do stuff with the $data array
			break;
		case 'wc_cart':
			// do stuff
			break;
		case 'wc_geolocation':
			// do stuff
			break;
		case 'wc_single_product':
			// ...
			break;
		case 'wc_address_i18n':
			// ...
			break;
		case 'wc_cart_fragments':
			// ...
			break;
		case 'wc_add_to_cart':
			// ...
			break;
		case 'wc_add_to_cart_variation':
			// ...
			break;
		case 'wc_country_select':
			// ...
			break;
		case 'wc_password_strength_meter':
			// ...
			break;
	endswitch;
 
	return $data;
 
}

If you have more useful information about it, or a question, welcome to comments.

Related

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter