简体   繁体   English

WP Rest API获取翻译的类别(WPML)

[英]WP Rest API get translated categories (WPML)

I'm using Wordpress and the Woocommerce plugin. 我正在使用Wordpress和Woocommerce插件。 Further I have WPML Plugin installed for 4 languages. 此外,我还为4种语言安装了WPML插件。 Additionally I'm using an Ionic 2 App, which is connected to the Shop with WP Rest API. 另外,我正在使用Ionic 2应用程序,该应用程序通过WP Rest API连接到Shop。 I would like now to get the categories in the translated languages in the app. 我现在想在应用程序中获取翻译语言的类别。 Following is the code to get the categories. 以下是获取类别的代码。 I tried to add /?lang=it at the and of the string "/wp-json/wc/v1/products/categories" => "/wp-json/wc/v1/products/categories/?lang=it, but I get everytime the english (native) categories. What I did is I tried this on https://resttesttest.com/ and there I'm able to feed the specific languages. 我试图在/和字符串“ / wp-json / wc / v1 / products / categories” =>“ / wp-json / wc / v1 / products / categories /?lang = it的和处添加/?lang = it,但是我每次都会得到英语(母语)类别。我所做的是我在https://resttesttest.com/上尝试过的方法,在那里我可以提供特定的语言。

 getStoreCategories(params) {
    var service = this;
    let url = service.appConfig.Shop_URL + "/wp-json/wc/v1/products/categories";
    url = this.initUrl(url, params);
    return new Promise(function (resolve, reject) {
      service.http.get(service.initRequest(url, 'get')).catch(err => {
        reject('error');
        return Observable.throw(err);
      }).map(res => res.json()).subscribe(data => {
        if (data) {
          service.cachedData = data;
          resolve(service.cachedData);
        }
        else {
          reject();
        }
      });
    });
  }

Your URL would be different for rest API with WPML in case if you are using below structure 如果使用以下结构,则带有WPML的其余API的URL会有所不同

Assuming sample product url 假设样本产品网址

if URL structure is http://yoursite.com/ar/product/product-01 如果网址结构为http://yoursite.com/ar/product/product-01

then your API call would be 那么您的API调用将是

let url = service.appConfig.Shop_URL + "{lang_code}" + "/wp-json/wc/v1/products/categories";

if URL structure is taking language code as Query string like http://yoursite.com/product/product-01?lang=ar 如果网址结构采用语言代码作为Query string例如http://yoursite.com/product/product-01?lang=ar

then your API call would be 那么您的API调用将是

let url = service.appConfig.Shop_URL + "/wp-json/wc/v1/products/categories?lang=" + {lang_code};

PS i don't know about the syntax in react but i've given you an idea PS:我不知道react的语法,但是我给了你一个主意

I could resolve this. 我可以解决这个问题。 I pass the parameter lang in my request as follows: 我在请求中传递参数lang,如下所示:

this.myService.getStoreCategories({ page: 1, per_page: 100, lang: 'it' }).then((categories: Array<any>) => {

If you are using multilanguage by the WPML Plugin. 如果您正在通过WPML插件使用多语言。 You can use the code for multilanguage by filtering the default category response. 您可以通过过滤默认类别响应将代码用于多语言。

add_filter( 'woocommerce_rest_prepare_product_cat', 'change_product_categories_response', 10, 3 );

function change_product_categories_response( $response, $item, $request ) {
global $sitepress;
$lang  = sanitize_text_field( $_REQUEST['lang'] );

$sitepress->switch_lang( $lang );

return $response;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM