PHP code example of kmrifat / laravel-shopify

1. Go to this page and download the library: Download kmrifat/laravel-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/ */

    

kmrifat / laravel-shopify example snippets


use Kmrifat\Shopify\Facades\Shopify;

$shopify = Shopify::shop('domain.myshopify.com','access_token');

// use rest get request
$shopify->rest()->get('products/{product_id}');
// you will only need to pass topic instate passing the entire url

// use post method to modify anything in shopify, the first argument will be the topic and second one will be the payload
$shopify->rest()->post('products', []);

// use graphql
$shopify->graph()->executeQuery([
    'query' => 'query_string',
    'variables' => [] // pass query variables
]);

namespace App\Models;
use Kmrifat\Shopify\Facades\Shopify;

class User extends Authenticatable 
{
    public function shopify()
    {
        return return Shopify::shop($this->name, $this->shopify_access_token);
        // $this->name = shopify shop domain
        // $this->shopify_access_token = shopify store access token
        // you can store the shop domain and access token anywhere you want, all you just need to pass this in shop parameter
    }
}


Route::get('/', function () {
    if (request()->has('shop')) {
    // If you want to authenticate in shopify store from the app, you just need to pass the shopify store name in shop query string,
    // i.e: yourdomain.com?shop=your-shopify-store 
        return \Laravel\Socialite\Facades\Socialite::driver('shopify')->stateless()->scopes(config('shopify.scopes'))
            ->with(['redirect_uri' => $request->redirect_uri ?? config('shopify.redirect')])
            ->redirect(); // use ->getTargetUrl() before closing statement if you are build an API oriented application, so it will return your the redirect url instate redirect from the backend
    }
    return view('welcome');
});

Route::get('/auth/callback', function () {
    $shopifyUser = \Laravel\Socialite\Facades\Socialite::driver('shopify')->stateless()->user();
    $user = User::updateOrCreate([
        'email' => $shopifyUser->email,
    ], [
        'name' => $shopifyUser->nickname,
        'email' => $shopifyUser->email,
        'shopify_access_token' => $shopifyUser->token,
    ]);
    
    // you can use passport / sanctum or any of favorite  to perform the authentication
    Auth::login($user, true);

    return redirect('/dashboard');
});

'webhooks' => [
    ['shopify/webhook', \App\Jobs\YourJob::class] // first element of this array will be the topic of webhook and second element of this array will your targeted job of the webhook
]
bash
php artisan vendor:publish --provider="Kmrifat\Shopify\ShopifyServiceProvider"
php artisan migrate