Migrating from the deprecated hooks to woocommerce_get_script_data

This tutorial is about proper migration to the new woocommerce_get_script_data filter hook after update to WooCommerce 3.3.

/8 comments

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

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 8

Leave a comment

php js HTML CSS Code