当我使用" WC_Product"的方法创建产品时默认情况下启用WPML后,Woo会用英语创建它。
如何确定产品创建语言?
$product = new WC_Product;
$product->set_name('My test product');
$product->set_slug('test-product');
$product->set_description('This is my test product');
$product->set_sku('some_sku');
$product->save();
感谢您的帮助!
答案 0 :(得分:0)
我使用$ sitepress object:
中的方法完成了它global $sitepress;
// Create the product in defaul language
// First switch de language for the product to create
$language_code_default = 'es';
$sitepress->switch_lang($language_code_default);
// Create product with de Woo methods
$product_es = new WC_Product;
$product_es->set_name('PRODUCTO EN ESPAÑOL');
$product_es->set_slug('producto-wc');
$product_es->set_description('Este producto ha sido creado con WC_Product');
$product_es->set_short_description('Este producto ha sido creado con WC_Product');
$post_id_es = $product_es->save();
// Create the traduction
// Remember, switch the traduction language
$language_code_tr = 'en';
$sitepress->switch_lang($language_code_tr);
$product_en = new WC_Product;
$product_en->set_name('PRODUCT IN ENGLISH');
$product_en->set_slug('producto-wc-en');
$product_en->set_description('This product was made from spanish version');
$product_en->set_short_description('This product was made from spanish version');
$post_id_en = $product_en->save();
// And use the wpml methods to link de traduction with the original language
$element_type = 'post_product';
$trid = $sitepress->get_element_trid($post_id_es, $element_type);
$sitepress->set_element_language_details($post_id_en, $element_type, $trid, $language_code_tr, $language_code_default);
// And then copy de product meta from de original to the traduction
$sitepress->copy_custom_fields($post_id_es, $post_id_en);