javascript 如何在任何 Wordpress 页面上检索所有 WooCommerce 数据的 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27205582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:07:23  来源:igfitidea点击:

How do I retrieve a JSON of all WooCommerce data on any Wordpress page?

javascriptjsonwordpresswoocommerce

提问by Sprout Coder

I want to set up a WooCommerce shop for a WordPress site (which I've never done before by the way).

我想为 WordPress 网站建立一个 WooCommerce 商店(顺便说一下,我以前从未做过)。

However, I do not want to use any type of WooCommerce generated pages.

但是,我不想使用任何类型的 WooCommerce 生成的页面。

What I want is to add/remove products (along with all other relevant product data), create categories, subcategories etc.. using the WooCommerce "control panel" from inside the Wordpress Dashboard and every time I do that, I want a WooCommerce object (containing all that data) to be updated/generated and made available across all (or some specific) pages.

我想要的是添加/删除产品(以及所有其他相关产品数据)、创建类别、子类别等。使用 Wordpress 仪表板内部的 WooCommerce“控制面板”,每次我这样做时,我都想要一个 WooCommerce 对象(包含所有这些数据)要更新/生成并在所有(或某些特定)页面上可用。

The reason for that is because I want to build everything myself (product pages, cart page, checkout page, category pages, subcategory pages).

这样做的原因是因为我想自己构建所有内容(产品页面、购物车页面、结帐页面、类别页面、子类别页面)。

In order to populate these pages with the relevant data, however, I'll need to have access to the whole of WooCommerce data from any of those pages.

但是,为了使用相关数据填充这些页面,我需要从这些页面中的任何一个访问整个 WooCommerce 数据。

Some examples will explain better what I'm trying to achieve. (note : I'll be using Javascript and generating everything on the client)

一些示例将更好地解释我想要实现的目标。(注意:我将使用 Javascript 并在客户端生成所有内容)

Case #1Let's say I am in the "Shop" page, and I want to populate a column with all available product categories. I need to be able to do something like that :

案例#1假设我在“商店”页面,我想用所有可用的产品类别填充一列。我需要能够做这样的事情:

var wooCommerceShopData = getAllWooCommerceShopData(); // an Ajax request that will fetch me all that data

var arrayOfAllAvailableCategories = wooCommerceShopData.categories;

Case #2

案例#2

Now suppose I am in the "sportswear" category. I need to be able to do something like that :

现在假设我属于“运动服”类别。我需要能够做这样的事情:

var wooCommerceShopData = getAllWooCommerceShopData();

var productsObjectForCurrentCategory = wooCommerceShopData.categories["sportswear"].products;

Case #3

案例#3

If I am on a "sportwear"-category product page, I need to be able to do something like that :

如果我在“运动装”类产品页面上,我需要能够执行以下操作:

var wooCommerceShopData = getAllWooCommerceShopData();

var currentProductData = wooCommerceShopData.categories["sportswear"].products["BLK123XMENSHORTS"];

var currentProductPrice = currentProductData.price;

// etc......

Is this possible and is there an WooCommerce/Wordpress API for that ?

这可能吗,是否有 WooCommerce/Wordpress API ?

采纳答案by XciD

WooCommerce provides a Rest API.

WooCommerce 提供了一个 Rest API。

You can find the documentation here:

您可以在此处找到文档:

https://woocommerce.github.io/woocommerce-rest-api-docs/

https://woocommerce.github.io/woocommerce-rest-api-docs/

Hope it helps.

希望能帮助到你。

回答by Mrinal

this is probably the best REST API DOCUMENTATIONfor Woocommerce.

这可能是Woocommerce最好的REST API 文档