我应该在服务器上使用什么WooCommerce API? [英] What WooCommerce API should I use on the server?
问题描述
我正在编写一个脚本来批量更改产品属性,例如价格,重量和尺寸.我需要直接在安装了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:
- WooCommerce非REST API 是我显而易见的选择,但它却被降级为令人恐惧的链接 )似乎有些矫kill过正:当我已经在服务器上并且只能使用PHP时,为什么要通过身份验证并使用HTTP?
- 通过update_post_meta()对数据库进行操作似乎容易出错,并且由于WooCommerce产品属性之间的许多关系而难以维护;只要看它是REST 自v3.0开始,所以我猜想第2点也将在这里适用.
- WooCommerce non-REST API would be my obvious choice, but it is relegated into a scary legacy folder which smells of deprecated.
- 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?
- 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!
- 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屋!