使用WPML创建多语言产品

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

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:

$my_translated_post = array(
        'post_title'    => $this->title_en,
        'post_content'  => $this->description_en,
        'post_status'   => $this->status,
        'post_type' => 'product', 
        'post_author'   => 1,       
	); 
     
	$translated_post_id = wp_insert_post( $my_translated_post );
	
	$wpml_element_type = apply_filters( 'wpml_element_type', 'product' );
	$get_language_args = array('element_id' => $post_id, 'element_type' => 'product' );
	$original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
    $product = wc_get_product( $translated_post_id );      
	$set_language_args = array(
		'element_id'    => $translated_post_id,
		'element_type'  => $wpml_element_type,
		'trid'   => $original_post_language_info->trid,
		'language_code'   => 'en', //language code of secondary language
		'source_language_code' => $original_post_language_info->language_code
	);	
	do_action( 'wpml_set_element_language_details', $set_language_args );
	$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 连接:

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

将产品 ID 保存到数组中:

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

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

foreach ($new_products as $key => $id) {
		$data->update->$key = [ 'id' => $id ]  ;	
	}
$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:

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

Save the product ids to an array like:

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

Fix the array and push them to the api:

foreach ($new_products as $key => $id) {
		$data->update->$key = [ 'id' => $id ]  ;	
	}
$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:

确定