PHP code example of cian / shopify

1. Go to this page and download the library: Download cian/shopify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cian / shopify example snippets


// <Root>/config/app.php
[
    "providers" => [
        // other providers ...
        Cian\Shopify\ShopifyServiceProvider::class
    ],

    "aliases" => [
        // other aliases ...
        'Shopify' => \Cian\Shopify\ShopifyFacade::class,
        'ShopifyMacro' => \Cian\Shopify\ShopifyMacroFacade::class
    ]
]



namespace App\Services;

use Cian\Shopify\Shopify;

class GetShopifyOrdersService
{
    protected $shopify;

    public function __construct(Shopify $shopify)
    {
        $this->shopify = $shopify;
    }

    public function exec()
    {
        $response = $this->shopify
            ->setWebsite('mystore')
            ->getOrders([
                'limit' => 100,
                // more options of get orders api ...
            ]);

        // always get response body using this way.
        $orders = $response->getBody();

        return $orders;
    }
}



namespace App;

// use laravel facade.
use Shopify;

$response = Shopify::setWebsite('mystore')->getOrders([/** options */]);

$response->hasNextPage(); // boolean
$response->getNextLink(); // null or next page api url string.

$response->hasPreviousPage(); // boolean
$response->getPreviousLink(); // null or previous page api url string.

$response->isLastPage(); // boolean

/**
 * This method get content from guzzle response.
 * and run json_decode before return.
 * you can pass json_decode options via this method,
 * here just show you the default values, all options are optional!
 */
$response->getBody(true, 512, 0);

/**
 * If you prefer handle response by your self.
 * you can get original response like below.
 */
$guzzleResponse = $response->getOriginalResponse();

// config/shopify.php

return [
    // othere properties...
    'api_presets' => [
        // the key(my_order_common) can be any string, just don't duplicate.
        'my_order_common' => [
            'fields' => [
                'id',
                'line_items',
                'billing_address'
            ]
        ]
    ]
];



namespace App;

use Cian\Cian\Shopify;

$shopify = new Shopify($guzzleClient, $config);

$keep = false;  // keep using this preset for each call or not, default is false.

$response = $shopify
    ->setWebsite('mystore')
    ->setApiPreset('my_order_common', $keep)
    ->getOrders([/** more options */]);

namespace App\Services;

use Cian\Shopify\ShopifyMacro;

class MyService
{
    protected $shopifyMacro;

    public function __construct(ShopifyMacro $shopifyMacro)
    {
        $this->shopifyMacro = $shopifyMacro;
    }

    public function exec()
    {
        $options = [
            'limit' => 250, // get 250 records per request.
            'created_at_min' => '2020-04-08T12:00:00+00:00' // set min date
            // other getOrders options ..
        ];

        // You will get response body instead of \Cian\Shopify\Response instance.
        $orders = $this->shopifyMacro
            ->setWebsite('mystore')
            /**
             * setFormatter is optional!
             * it can let getters return specific format.
             * this may decline some memory usage.
             */
            ->setFormatter(function ($order) {
                return [
                    'id' => $order['id']
                ];
            })
            ->getOrders($options);

        // do something with orders ...
    }
}
shell
php artisan vendor:publish --provider="Cian\Shopify\ShopifyServiceProvider"