我应该在服务器上使用什么 WooCommerce API? [英] What WooCommerce API should I use on the server?

查看:49
本文介绍了我应该在服务器上使用什么 WooCommerce API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来批量更改产品属性,例如价格、重量和尺寸.我需要直接在安装了 WordPress (4.7.2) 和 WooCommerce (2.6.13) 的服务器上运行脚本.我能想到的选项对我来说似乎并不理想:

I am writing a script to bulk-change product properties such as price, weight and dimension. I need to run the script directly on the server where WordPress (4.7.2) and WooCommerce (2.6.13) are installed. The options I could think of don't seem ideal to me:

  1. WooCommerce 非 REST API 是我显而易见的选择,但它被降级为可怕的 legacy 文件夹,有点过时的味道.
  2. WooCommerce REST API(链接) 似乎有点矫枉过正:当我已经在服务器上并且只能使用 PHP 时,为什么还要进行身份验证并使用 HTTP?
  3. 通过 update_post_meta() 处理数据库 似乎容易出错并且难以维护,因为 WooCommerce 产品属性之间存在许多关系;看看 这里 逻辑的数量必须复制才能改变产品的价格!
  4. WP-CLI 可以工作,但 AFAIK 它不如 PHP 脚本灵活;无论如何,它是 REST-powered 从 v3.0 开始,所以我想第 2 点也适用于此.
  1. WooCommerce non-REST API would be my obvious choice, but it is relegated into a scary legacy folder which smells of deprecated.
  2. WooCommerce REST API (link) seems overkill: why should I go through authentication and use HTTP when I am already on the server and can just use PHP?
  3. Acting on the database via update_post_meta() seems error-prone and hard to maintain given the many relations between WooCommerce product properties; just look here at the amount of logic one has to duplicate just to change a product's price!
  4. WP-CLI could work but AFAIK it would not be as flexible as PHP script; in any case, it is REST-powered since v3.0, so I guess point 2 will apply here, too.

我觉得我错过了什么,请帮助我,否则我的大脑会爆炸:-D

I feel like I am missing something, please help me otherwise my brain will explode :-D

推荐答案

事实证明,您可以在服务器上使用 REST API,而无需验证或执行 HTTP 请求:您只需要构建一个 WP_REST_Request 对象并将其直接传递给 API.

It turns out that you can use the REST API on the server without neither authenticating nor performing an HTTP request: you just need to build a WP_REST_Request object and pass it directly to the API.

这是一个示例 PHP 脚本,它将使用 REST API 根据产品的 ID 打印产品信息.脚本应该放在 WordPress 文件夹中并在浏览器中执行;产品 ID 作为查询参数给出,例如:http://www.yourwebsite.com/script.php?id=123.

Here's an example PHP script which will print information on a product based on its ID, using the REST API. The script should be placed in the WordPress folder and executed in a browser; the product ID is given as a query parameter, ex: http://www.yourwebsite.com/script.php?id=123.

<?php
/* Load WordPress */
require('wp-load.php');

/* Extract the product ID from the query string */
$product_id = isset( $_GET['id'] ) ? $_GET['id'] : false;

if ( $product_id ) {

    /* Create an API controller */
    $api = new WC_REST_Products_Controller();

    /* Build the request to create a new product */
    $request = new WP_REST_Request ('POST', '', '');
    $request['id'] = $product_id;

    /* Execute the request */
    $response = $api->get_item( $request );

    /* Print to screen the response from the API.
    The product information is in $response->data  */
    print_r( $response );

    /* Also print to screen the product object as seen by WooCommerce */
    print_r( wc_get_product( $product_id ) );

}

示例:创建产品

下一个脚本将创建一个新产品.产品的详细信息应直接在脚本中的 set_body_params() 函数中输入.对于允许的字段列表,只需使用前面的脚本打印任何产品的数据.

Example: Create a Product

The next script will create a new product. The product's deatails should be entered directly in the script, in the set_body_params() function. For a list of the allowed fields, just print any product's data using the previous script.

/* Load WordPress */
require('wp-load.php');

/* Create an API controller */
$api = new WC_REST_Products_Controller();

/* Build the request to create a new product */
$request = new WP_REST_Request ('POST', '', '');
$request->set_body_params( array (
    'name' => 'New Product',
    'slug' => 'new-product',
    'type' => 'simple',
    'status' => 'publish',
    'regular_price' => 60,
    'sale_price' => 40,
));

/* Execute the request */
$response = $api->create_item( $request );

/* Print to screen the response from the API */
print_r( $response );

/* Also print to screen the product object as seen by WooCommerce */
print_r( wc_get_product( $response->data['id'] ) );

一些基本的安全措施

在您的网站上留下可执行的 PHP 脚本不是一个好主意.我宁愿将它们合并到一个插件中,并且只允许授权用户访问它们.为此,将以下代码添加到脚本中可能会很有用:

Some basic security measures

It's not a good idea to leave executable PHP scripts on your website. I would rather incorporate them in a plugin, and make them accessible only to authorized users. To achieve that, it might be useful to prepend the following code to the scripts:

/* Load WordPress. Replace the /cms part in the path if
WordPress is installed in a folder of its own. */
try {
    require($_SERVER['DOCUMENT_ROOT'] . '/cms/wp-load.php');
} catch (Exception $e) {
    require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
}

/* Restrict usage of this script to admins */
if ( ! current_user_can('administrator') ) {
  die;
}

这篇关于我应该在服务器上使用什么 WooCommerce API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆