使用REST API从Woocommerce获取所有产品

时间:2018-01-27 14:00:13

标签: php wordpress rest woocommerce-rest-api

我正在尝试使用rest api检索所有产品。我看过this个问题。我正在用邮递员打电话。这是我的查询

https://squatwolf.com/wp-json/wc/v2/products?filter[posts_per_page] =-1

查询仅显示10个结果。

7 个答案:

答案 0 :(得分:5)

这不是最新的API端点:

/wc-api/v3/products?filter[limit]=

您必须获取每页的页面才能获得所有产品:

$page = 1;
$products = [];
$all_products = [];
do{
  try {
    $products = $wc->get('products',array('per_page' => 100, 'page' => $page));
  }catch(HttpClientException $e){
    die("Can't get products: $e");
  }
  $all_products = array_merge($all_products,$products);
  $page++;
} while (count($products) > 0);

答案 1 :(得分:4)

我能够使用以下解决方案找到数据,

https://squatwolf.com/wc-api/v3/products?filter[limit] =-1

答案 2 :(得分:1)

将代码添加到function.php

Tidyverse

答案 3 :(得分:0)

try:                        # In order to be able to import tkinter for
    import tkinter as tk    # either in python 2 or in python 3
except ImportError:
    import Tkinter as tk


class MyToplevel(tk.Toplevel):
    def __init__(self, master, text):
        tk.Toplevel.__init__(self, master)
        self.label = tk.Label(self, text=text)
        self.button = tk.Button(self, text="Next", command=self.destroy)
        self.label.grid(row=0, column=0)
        self.button.grid(row=1, column=0)


def next_window(window_widget, index, a_list):
    if window_widget:
        window_widget.destroy()
    if index < len(a_list):
        window_widget = MyToplevel(root, a_list[index])
        if (index + 1) < len(a_list):
            window_widget.button['command'] = lambda: next_window(
                                            window_widget, index + 1, a_list)


if __name__ == '__main__':
    root = tk.Tk()
    a_list = [1, 2, 3, 4, 5]
    current_window = None
    go_button = tk.Button(root, text="Go", command=lambda:next_window(
                                                current_window, 0, a_list))
    go_button.pack()
    root.mainloop()

/wp-json/wc/v2/products

两者似乎都有效,但要获得我正在使用的特定数量的产品

/wc-api/v3/products

将产品数量放在那里。所有产品均为-1

答案 4 :(得分:0)

see the Docs不再支持filter参数。因此,您确实需要循环浏览页面。

以下是如何使用JavaScript获取所有产品的信息(对于Gutenberg Block商店):

let allProducts = [],
    page = 1

while (page !== false) {
    const products = yield actions.receiveProducts(`/wc-pb/v3/products?per_page=100&page=${page}`)

    if (products.length) {
        allProducts = allProducts.concat(products)
        page++
    } else {
        page = false // last page
    }
}

return actions.setProducts(allProducts)

答案 5 :(得分:0)

答案 6 :(得分:0)

这对我有用。使用API​​ v3

/wc-api/v3/products?

默认检索前500种产品或

/wc-api/v3/products?per_page=900

获得900种产品

function maximum_api_filter($query_params) {
   $query_params['per_page']['maximum'] = 10000;
   $query_params['per_page']['default'] = 500;
   return $query_params;
}
add_filter('rest_product_collection_params', 'maximum_api_filter', 10, 1 );