我正在尝试制作woocommerce插件。 我需要使用woocommerce REST API。 我在localhost中使用单个php文件尝试了REST API,它可以工作。但是,当我尝试将代码添加到插件中时。它不起作用。
它在管理员区域给了我警告消息。
该站点遇到技术困难。请检查您的 网站管理员电子邮件收件箱中的说明。
错误日志。
[2019年5月19日7:07:14 UTC] PHP致命错误:未捕获的错误:调用 未定义的方法WooCommerce :: get() D:\ wamp64_SSD \ www \ wordpress \ wp-content \ plugins \ myetsy \ index.php:87 堆栈跟踪:
0 D:\ wamp64_SSD \ www \ wordpress \ wp-includes \ class-wp-hook.php(286):etsy_func('')
1 D:\ wamp64_SSD \ www \ wordpress \ wp-includes \ class-wp-hook.php(310):WP_Hook-> apply_filters('',Array)
2 D:\ wamp64_SSD \ www \ wordpress \ wp-includes \ plugin.php(465):WP_Hook-> do_action(Array)
3 D:\ wamp64_SSD \ www \ wordpress \ wp-admin \ admin.php(253):do_action('woocommerce_pag ...')
4 {main}放在D:\ wamp64_SSD \ www \ wordpress \ wp-content \ plugins \ myetsy \ index.php上
第87行
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://localhost/wordpress',
'ck_cb93e80be8b5866514b5e95f71055e3a8f2358d2',
'cs_a0ec55a88a28475373a8ece2e303c40634f56511',
[
'wp_api' => true,
'version' => 'wc/v3' ,
'verify_ssl' => false
]
);
function register_my_custom_submenu_page() {
add_submenu_page( 'woocommerce', 'Etsy', 'Etsy', 'manage_options', 'etsy-link', 'etsy_func' );
}
add_action('admin_menu', 'register_my_custom_submenu_page',99);
function etsy_func() {
echo '<h3>Title</h3>';
$data = [
'name' => 'Premium Qualityssssssssssssss',
'type' => 'simple',
'regular_price' => '22.99',
'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
'categories' => [
[
'id' => 10
],
[
'id' => 14
]
],
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg'
],
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg'
]
]
];
global $woocommerce;
print_r($woocommerce->post('products', $data));
}
?>
谢谢。