You should call wp_localize_script()
in the wp_enqueue_scripts
hook (or a later hook) in which you would get the correct product/post data such as ID, and that conditional tags like is_single()
would also work as expected.
So you should move all the $product
, $product_id
, $dataToBePassed
and wp_localize_script()
parts to inside the wp_enqueue_scripts
callback, and then you’d get the correct product/post ID, etc. See an example below, and note that:
-
is_product()
andwc_get_product()
are WooCommerce functions, and I used the former because I presumed your script should only be loaded on single product pages? (like one atexample.com/product/product-foo/
) -
Also, just like one should do
$post = get_post()
, you should also do$product = wc_get_product()
to properly get the current product/post object/data.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_three_d_scripts' );
function my_theme_enqueue_three_d_scripts() {
if ( ! is_product() ) {
return;
}
$handle="three-d-scripts";
$src = get_stylesheet_directory_uri() . '/src/scripts/three-d-scripts.js';
$deps = array();
$version = '1.0.0';
wp_enqueue_script( $handle, $src, $deps, $version, true );
$product = wc_get_product();
$product_id = $product ? $product->get_id() : 0;
$dataToBePassed = array();
switch ( $product_id ) {
case 1:
$dataToBePassed = array( 'product_id' => 1 );
break;
case 2:
$dataToBePassed = array( 'product_id' => 2 );
break;
default:
$dataToBePassed = array( 'product_id' => 0 );
break;
}
// PS: Instead of php_vars, use a unique name like my_three_d_vars..
wp_localize_script( $handle, 'php_vars', $dataToBePassed );
}