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

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

python, woocommerce: how to add categories with translations?

问题

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

WordPress与WooCommerce插件版本7.3

WPML多语言CMS: 4.5.14

我有这个Python代码片段:

from woocommerce import API

# 创建API类的实例
wcapi = API(
    url="FQDN",
    consumer_key="yourconsumerkey",
    consumer_secret="yourconsumersecret",
    wp_api=True,
    version="wc/v3"
)

# 为主语言创建包含分类数据的字典
category_data_en = {
    "name": "Category Name in English",
    "parent": 0,
    "meta_data": [
        {
            "key": "_wpml_language",
            "value": "en"
        },
        {
            "key": "_wpml_translation_status",
            "value": "0"
        },
        {
            "key": "_wpml_element_type",
            "value": "tax_category"
        }
    ]
}

# 使用WooCommerce API为主语言创建分类
new_category_en = wcapi.post("products/categories", category_data_en).json()

# 为翻译创建包含分类数据的字典
category_data_pl = {
    "name": "Category Name in Polish",
    "meta_data": [
        {
            "key": "_wpml_language",
            "value": "pl"
        },
        {
            "key": "_wpml_translation_of",
            "value": new_category_en.get("id")
        },
        {
            "key": "_wpml_translation_status",
            "value": "1"
        },
        {
            "key": "_wpml_element_type",
            "value": "tax_category"
        }
    ]
}

# 使用WooCommerce API创建翻译
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:

from woocommerce import API

# create an instance of the API class
wcapi = API(
    url="FQDN",
    consumer_key="yourconsumerkey",
    consumer_secret="yourconsumersecret",
    wp_api=True,
    version="wc/v3"
)

# create a dictionary with the category data for the main language
category_data_en = {
    "name": "Category Name in English",
    "parent": 0,
    "meta_data": [
        {
            "key": "_wpml_language",
            "value": "en"
        },
        {
            "key": "_wpml_translation_status",
            "value": "0"
        },
        {
            "key": "_wpml_element_type",
            "value": "tax_category"
        }
    ]
}

# create the category using the WooCommerce API for the main language
new_category_en = wcapi.post("products/categories", category_data_en).json()

# create a dictionary with the category data for the translation
category_data_pl = {
    "name": "Category Name in Polish",
    "meta_data": [
        {
            "key": "_wpml_language",
            "value": "pl"
        },
        {
            "key": "_wpml_translation_of",
            "value": new_category_en.get("id")
        },
        {
            "key": "_wpml_translation_status",
            "value": "1"
        },
        {
            "key": "_wpml_element_type",
            "value": "tax_category"
        }
    ]
}

# create the translation using the WooCommerce API
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:

确定