[Shopify]订单下载和物流推送

398 阅读1分钟

使用的包:

phpclassic/php-shopify
1.订单下载
use PHPShopify\ShopifySDK;

// 下载
public function download()
{
        $config = array(
            'ShopUrl' => '我的店铺.myshopify.com',
            'ApiKey' => '',
            'Password' => '',
            'Curl' => array(
                CURLOPT_TIMEOUT => 10,
                CURLOPT_FOLLOWLOCATION => true
            )
        );
        $shopify = ShopifySDK::config($config);

        $params = array(
            'status' => 'archived',
            // 'created_at_min' => '2022-10-10T16:15:47-04:00',
            'fields' => 'id,name,total_price,currency,shipping_address,created_at,line_items', //line_items,
            'limit'  => 10,
        );

        $orders = $shopify->Order->get($params);
        dd($rrder);
    }

2.物流推送
<?php

namespace App\Console\Commands\Shopify;
use Illuminate\Console\Command;

use PHPShopify\ShopifySDK;

class Fulfillment extends Command
{
 
    protected $client;
    public function __construct()
    {
        $config = array(
            'ShopUrl' => '我的店铺.myshopify.com',
            'ApiKey' => '',
            'Password' => '',
            'ApiVersion' => '2022-07',
            'Curl' => array(
                CURLOPT_TIMEOUT => 10,
                CURLOPT_FOLLOWLOCATION => true
            )
        );
        $this->client = ShopifySDK::config($config);
    }

    public function handle()
    {
        
        $list = []; // 此处更换为订单列表
        
        foreach ($list as $order) {
            
            $track_number  = '跟踪号';
            $track_url = 'https://www.swiship.co.uk/track'; // 一些查询物流的网站
            $company = '物流公司名';

            // 回传物流单号 (这个ID是下载Shopify订单时的ID)
            $fulfillment_order_id = $this->create_fulfillment_order($order['site_order_id']);
         
            
            if (!  $fulfillment_order_id) {
                exit("创建履约单失败");
            } 

            try {
                $res = $this->fulfillment_order($fulfillment_order_id, $track_number, $company, $track_url);
                if ($res) echo "success";
           } catch(\Exception $e) {
               echo "error";
           }
        }
    }
    
    /**
     *  step_1: 创建履约单, 也可以直接返回履约单ID
     */
    public function create_fulfillment_order($order_id)
    {
        $order = $this->client->Order($order_id)->FulfillmentOrder->get();

        if (empty($order)) {
            "create fail";
        }
        return $order[0]['id'];
    }
    
    /**
     * step_2.回传单号
     * */
    public function fulfillment_order($fulfillment_order_id, $track_number, $company, $track_url)
    {
        $data = [
            'message' => 'The package was shipped',
            'notify_customer' => self::MOTIFY_CUSTOMER,
            'tracking_info' => [
                'number' => $track_number,
                "company" => $company,
                "url" => $track_url, 
            ],
            'line_items_by_fulfillment_order' => [
                [
                    'fulfillment_order_id' => $fulfillment_order_id,
                ]
            ],
        ];
        if ($track_url) {
            $data['line_items_by_fulfillment_order'][0]['url'] = $track_url;
        }
        
        $res = $this->client->Fulfillment->post($data);
        return $res;
    }


    /**
     *  step_x: 获取履约单 (如果没有提交过履约, 还是空的, 所以新的订单创建和获取的话都用上面那个)
     */
    public function get_fulfillment_order($order_id)
    {
        $order = $this->client->Order($order_id)->Fulfillment->get();
        dd($order);
        return $order[0]['id'];
    }
}