Python,WooCommerce:如何添加具有翻译的类别?

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

python, woocommerce: how to add categories with translations?

问题

我需要一个Python代码片段来创建带有翻译的分类。Python 3.8与WooCommerce包。

WordPress与WooCommerce插件版本7.3

WPML多语言CMS: 4.5.14

我有这个Python代码片段:

  1. from woocommerce import API
  2. # 创建API类的实例
  3. wcapi = API(
  4. url="FQDN",
  5. consumer_key="yourconsumerkey",
  6. consumer_secret="yourconsumersecret",
  7. wp_api=True,
  8. version="wc/v3"
  9. )
  10. # 为主语言创建包含分类数据的字典
  11. category_data_en = {
  12. "name": "Category Name in English",
  13. "parent": 0,
  14. "meta_data": [
  15. {
  16. "key": "_wpml_language",
  17. "value": "en"
  18. },
  19. {
  20. "key": "_wpml_translation_status",
  21. "value": "0"
  22. },
  23. {
  24. "key": "_wpml_element_type",
  25. "value": "tax_category"
  26. }
  27. ]
  28. }
  29. # 使用WooCommerce API为主语言创建分类
  30. new_category_en = wcapi.post("products/categories", category_data_en).json()
  31. # 为翻译创建包含分类数据的字典
  32. category_data_pl = {
  33. "name": "Category Name in Polish",
  34. "meta_data": [
  35. {
  36. "key": "_wpml_language",
  37. "value": "pl"
  38. },
  39. {
  40. "key": "_wpml_translation_of",
  41. "value": new_category_en.get("id")
  42. },
  43. {
  44. "key": "_wpml_translation_status",
  45. "value": "1"
  46. },
  47. {
  48. "key": "_wpml_element_type",
  49. "value": "tax_category"
  50. }
  51. ]
  52. }
  53. # 使用WooCommerce API创建翻译
  54. new_category_pl = wcapi.post("products/categories", category_data_pl).json()

它在我的网站上创建了两个英语语言的分类。我做错了什么?

我可以看到在我的WPML插件设置中有以下信息:
WooCommerce多语言未安装
是否需要安装它才能正确创建这些分类?

英文:

I need a python snippet to create categories with translation.
Python 3.8 with woocommerce package.

WordPress with woocommerce plugin in version 7.3

WPML Multilingual CMS: 4.5.14

I have this snippet in python:

  1. from woocommerce import API
  2. # create an instance of the API class
  3. wcapi = API(
  4. url="FQDN",
  5. consumer_key="yourconsumerkey",
  6. consumer_secret="yourconsumersecret",
  7. wp_api=True,
  8. version="wc/v3"
  9. )
  10. # create a dictionary with the category data for the main language
  11. category_data_en = {
  12. "name": "Category Name in English",
  13. "parent": 0,
  14. "meta_data": [
  15. {
  16. "key": "_wpml_language",
  17. "value": "en"
  18. },
  19. {
  20. "key": "_wpml_translation_status",
  21. "value": "0"
  22. },
  23. {
  24. "key": "_wpml_element_type",
  25. "value": "tax_category"
  26. }
  27. ]
  28. }
  29. # create the category using the WooCommerce API for the main language
  30. new_category_en = wcapi.post("products/categories", category_data_en).json()
  31. # create a dictionary with the category data for the translation
  32. category_data_pl = {
  33. "name": "Category Name in Polish",
  34. "meta_data": [
  35. {
  36. "key": "_wpml_language",
  37. "value": "pl"
  38. },
  39. {
  40. "key": "_wpml_translation_of",
  41. "value": new_category_en.get("id")
  42. },
  43. {
  44. "key": "_wpml_translation_status",
  45. "value": "1"
  46. },
  47. {
  48. "key": "_wpml_element_type",
  49. "value": "tax_category"
  50. }
  51. ]
  52. }
  53. # create the translation using the WooCommerce API
  54. new_category_pl = wcapi.post("products/categories", category_data_pl).json()

and it creates two categories in the English language on my website. What am I doing wrong?
Python,WooCommerce:如何添加具有翻译的类别?

I can see that in my WPML plugin setting there is the info:
WooCommerce Multilingual Not installed
Is it necessary to install it to get those categories created properly?

答案1

得分: 0

我要做的是:

  1. 在“en”语言中创建类别(添加:'lang':'en'
  2. 我必须遍历这些类别并在pl中创建新类别(添加:'lang':'pl''translation_of': id_category_en
英文:

What I had to do is:

  1. create categories in 'en' language (added: 'lang':'en')
  2. I had to loop over those categories and create new ones in pl (added: 'lang':'pl' and 'translation_of': id_category_en

huangapple
  • 本文由 发表于 2023年2月19日 08:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497331.html
匿名

发表评论

匿名网友

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

确定