使用WPML创建多语言产品

huangapple go评论89阅读模式
英文:

Creating multilingual products with WPML

问题

抱歉,我不能执行代码或提供实时的编程操作。上述代码段似乎是用于使用WPML插件在WordPress中通过编程方式创建多语言的WooCommerce产品,并尝试同步产品的价格、库存、图像、类别和其他元数据。该代码中提到,在仪表板中通过单击“快速编辑”并点击“更新”,能够手动触发某个“触发器”,使翻译产品与其父产品进行“同步”。

然而,关于如何通过编程方式触发此同步的细节并不在代码段中。您可能需要深入研究WPML插件的文档或者寻求WPML插件开发社区的支持来了解如何在代码中触发产品同步的过程。

英文:

I'm creating multiple woocommerce products programmatically.
In order to create translated products and according to WPML documentation i should use:

  1. $my_translated_post = array(
  2. 'post_title' => $this->title_en,
  3. 'post_content' => $this->description_en,
  4. 'post_status' => $this->status,
  5. 'post_type' => 'product',
  6. 'post_author' => 1,
  7. );
  8. $translated_post_id = wp_insert_post( $my_translated_post );
  9. $wpml_element_type = apply_filters( 'wpml_element_type', 'product' );
  10. $get_language_args = array('element_id' => $post_id, 'element_type' => 'product' );
  11. $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
  12. $product = wc_get_product( $translated_post_id );
  13. $set_language_args = array(
  14. 'element_id' => $translated_post_id,
  15. 'element_type' => $wpml_element_type,
  16. 'trid' => $original_post_language_info->trid,
  17. 'language_code' => 'en', //language code of secondary language
  18. 'source_language_code' => $original_post_language_info->language_code
  19. );
  20. do_action( 'wpml_set_element_language_details', $set_language_args );
  21. $product->save();

which creates the product with title and description. However in order to "sync" prices, stock, images,categories and other metadata i have to go to "products" in the dashboard click quick edit and then click update without changing anything.
From what i understand there's a "trigger" somewhere in that process that "syncs" the translated product with its parent.
Any clues on how can i trigger this programmatically?

答案1

得分: 1

我已经找到了使用 WooCommerce API 的一个变通方法。
我们可以使用 API 的“批量”方法来批量更新产品。
您需要从以下网址下载 API:https://github.com/woocommerce/wc-api-php
初始化 API 连接:

  1. include_once('vendor/autoload.php');
  2. use Automattic\WooCommerce\Client;
  3. $wooapi = new Client(URL, CK, CS, ['version' => 'wc/v3','timeout' => '99999', 'verify_ssl'=> false]);

将产品 ID 保存到数组中:

  1. $new_products [] = $product->get_id();

修复数组并将其推送到 API:

  1. foreach ($new_products as $key => $id) {
  2. $data->update->$key = [ 'id' => $id ] ;
  3. }
  4. $wooapi->post('products/batch', $data);

重要提示:API 默认每次调用最多可以处理 100 个产品。但是这可以通过钩子轻松更改。

英文:

I have found a workaround using the woocommerce API.
We can use the "batch" method of the API to mass update the products.
You'd need to download the api from: https://github.com/woocommerce/wc-api-php
initiate the API connection:

  1. include_once('vendor/autoload.php');
  2. use Automattic\WooCommerce\Client;
  3. $wooapi = new Client(URL, CK, CS, ['version' => 'wc/v3','timeout' => '99999', 'verify_ssl'=> false]);

Save the product ids to an array like:

  1. $new_products [] = $product->get_id();

Fix the array and push them to the api:

  1. foreach ($new_products as $key => $id) {
  2. $data->update->$key = [ 'id' => $id ] ;
  3. }
  4. $wooapi->post('products/batch', $data);

Important Note: out-of-the-box the API has a limit of 100 poroducts per api call. However this can be easily changed with a hook.

huangapple
  • 本文由 发表于 2023年2月6日 20:03:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定