首页 > 解决方案 > 如何通过 REST API 将 Woocommerce 订单从客户端商店发送到服务器/中央商店

问题描述

我正在做一个练习,包括将 Woocommerce 订单从客户端发送到服务器。一旦状态变为完成,客户端将通过 REST API 发送订单,届时服务器(或中央存储)将接收订单,并将其保存在自己的订单中。我为客户端和服务器编写了大部分代码,但有些事情我不知道该怎么做。

事实上,我已经使用 Postman 软件进行了测试,以查看服务器侦听器是否正常工作并且确实有效。

我对客户的怀疑是我是否正确发送了整个订单。

至于服务器,我不知道我是否在接收请求的函数中正确执行代码:我的意思是我使用 json_decode() 以及是否必须先使用 json_encode()。

我很感激任何帮助。

非常感谢。

中央存储或服务器代码:

Plugin Name: Server
Plugin URI: https://www.misitio.com/
Version: 1.0
Author: Yo
Author URI: https://www.misitio.com/
License: GPL2

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

    $woocommerce = new Client(                                      
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx862df',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037',
        [

        'version' => 'wc/v3',
        ]
    );

       
    add_action('rest_api_init','registrar_mi_endpoint');

    function registrar_mi_endpoint() {
          register_rest_route( 'wc/v3', '/endpoint2/', array(
              'methods' => 'POST',
              'callback' => 'mi_consulta',
              'permission_callback' => function($request){
                  return current_user_can('manage_options');
              },
          ) );
    }

    function mi_consulta(WP_REST_Request $solicitud) {

        $pedido_procesado=json_decode($solicitud,true);
        
        $woocommerce->post('orders', $pedido_procesado);
   
    }

客户端代码

Plugin Name: Cliente
Plugin URI:  https://www.misitio.com/
Author: Yo
Author URI: https://www.misitio.com/
Version: 1.0
License: GPL2

require __DIR__ . '/vendor/autoload.php';

    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;

;
add_action('woocommerce_order_status_completed', 'funcion1');

function funcion1($order_id) {

    $woocommerce = new Client(
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9372',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdfcd',
        ['version' => 'wc/v3',]);

    $sales = $woocommerce->get('orders/'.$order_id);
    $var = json_encode($sales);
    
    $username ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx62df';
    $password ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037';

    
    $ch = curl_init("https://misitioservidor.com/wp-json/wc/v3/endpoint2/");

    
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

    
    curl_setopt($ch, CURLOPT_POST, TRUE);

    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $var);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (!curl_errno($server_output)) {
        print_r('it works');
        print_r($status_code);
        exit;
    }else{
        print_r("it doesn't work");
        exit;
    }
}

标签: phpwoocommercewordpress-rest-apirest

解决方案


我已经能够为这两个插件编写代码。我把它们留在这里以防它帮助某人。我有一个具有更多功能的改进版本。

服务器代码:

Plugin Name: Server
Plugin URI: https://www.misitio.com/
Version: 1.0
Author: Yo
Author URI: https://www.misitio.com/
License: GPL2

require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;

    $woocommerce = new Client(                                      
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx862df',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037',
        [

        'version' => 'wc/v3',
        ]
    );

       
    add_action('rest_api_init','registrar_mi_endpoint');

    function registrar_mi_endpoint() {
          register_rest_route( 'wc/v3', '/endpoint2/', array(
              'methods' => 'POST',
              'callback' => 'mi_consulta',
              'permission_callback' => function($request){
                  return current_user_can('manage_options');
              },
          ) );
    }

    function mi_consulta(WP_REST_Request $solicitud) {

        $array = json_decode($solicitud->get_body());       
            
        $data2 = [                                          
            'payment_method' => $array->payment_method,
            'payment_method_title' => $array->payment_method_title,
            'billing' => [
                'first_name' => $array->billing->first_name,    
                'last_name' => $array->billing->last_name,      
                'address_1' => $array->billing->address_1,      
                'address_2' => '',
                'city' => $array->billing->city,                
                'state' => $array->billing->state,              
                'postcode' => $array->billing->postcode,        
                'country' => $array->billing->country,          
                'email' => $array->billing->email,              
                'phone' => $array->billing->phone               
            ],
            'shipping' => [
                'first_name' => $array->shipping->first_name,   
                'last_name' => $array->shipping->last_name,     
                'address_1' => $array->shipping->address_1,     
                'address_2' => '',
                'city' => $array->shipping->city,               
                'state' => $array->shipping->state,             
                'postcode' => $array->shipping->postcode,       
                'country' => $array->shipping->country          
            ],
            'line_items' => [
                [
                    'product_id' => $array->line_items[0]->product_id,
                    'quantity' => $array->line_items[0]->quantity
                ]
            ],
            'shipping_lines' => [
                [
                    'method_id' => $array->shipping_lines[0]->method_id,        
                    'method_title' => $array->shipping_lines[0]->method_title,  
                    'total' => $array->shipping_lines[0]->total                 
                ]
            ]
        ];  
            
        if($woocommerce->post('orders', $data2)){                                   
            print_r("PEDIDO INTRODUCIDO");
        }
   
    }

客户代码:

Plugin Name: Cliente
Plugin URI:  https://www.misitio.com/
Author: Yo
Author URI: https://www.misitio.com/
Version: 1.0
License: GPL2

require __DIR__ . '/vendor/autoload.php';

    use Automattic\WooCommerce\Client;
    use Automattic\WooCommerce\HttpClient\HttpClientException;

;
add_action('woocommerce_order_status_completed', 'funcion1');

function funcion1($order_id) {

    $woocommerce = new Client(
        'https://misitio.com/',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9372',
        'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxdfcd',
        ['version' => 'wc/v3',]);

    $sales = $woocommerce->get('orders/'.$order_id);
    $var = json_encode($sales);
    
    $username ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx62df';
    $password ='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf5037';

    
    $ch = curl_init("https://misitioservidor.com/wp-json/wc/v3/endpoint2/");

    
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

    
    curl_setopt($ch, CURLOPT_POST, TRUE);

    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $var);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $server_output = curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (!curl_errno($server_output)) {
        print_r('it works');
        print_r($status_code);
        exit;
    }else{
        print_r("it doesn't work");
        exit;
    }
}

推荐阅读