I am trying to translate my products into WooCommerce based on a category.
When WooCommerce loads, I run the action:
add_action( 'woocommerce_init', 'translate_products' );
In my translate_products function, I run WP_Query so that I can translate products based on the current loop id. It translates only all products, but not only those who have the category "en". I cannot get only the identifier of products assigned to the category "en".
It would be great if someone could help me. Thanks in advance!
I am new to Wordpress and especially WooCommerce encoding, so please excuse me if I make a silly mistake.;)
Here is my code:
function translate_products() {
$args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'en' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$current_id = get_the_id();
pll_set_post_language($current_id,'en');
endwhile;
wp_reset_query();
}