sairum123/laravel-bigcommerce

Laravel Bigcommerce 包

v2.3.1 2023-08-31 15:55 UTC

This package is auto-updated.

Last update: 2024-09-30 01:18:15 UTC


README

Laravel Bigcommerce 是一个简单的包,它帮助构建强大的 Bigcommerce 集成。此包支持 Bigcommerce Api 的第 2 版和第 3 版,以及 Laravel 5 或更高版本。

注意

我已将此包的 Laravel 支持 从 Laravel 版本 5 更改为 Laravel 8

安装

将包添加到 composer.json 文件中

composer require sairum123/laravel-bigcommerce

将服务提供者添加到 config/app.php 文件中的 providers 数组。

<?php

'providers' => [
    ...
    Oseintow\Bigcommerce\BigcommerceServiceProvider::class,
],

为 Facade 设置别名

<?php

'aliases' => [
    ...
    'Bigcommerce' => Oseintow\Bigcommerce\Facades\Bigcommerce::class,
],

配置

Laravel Bigcommerce 需要连接配置。您需要发布供应商资产

php artisan vendor:publish

这将在配置目录中创建一个 bigcommerce.php 文件。您需要设置您的 auth 密钥

OAUTH

设置 CLIENT IDCLIENT SECRETREDIRECT URL

BasicAuth

设置 API_KEYUSERNAMESTORE URL

让我们获取访问令牌

Route::get("process_oauth_result",function(\Illuminate\Http\Request $request)
{
    $response = Bigcommerce::getAccessToken($request->code, $request->scope, $request->context));

    dd($response);
});

用法

使用此包从 Bigcommerce 访问资源有两种方式。

  1. 使用 HTTP 动词(例如,这提供了更大的灵活性,也支持 api v3,并返回 laravel 集合)
  2. 使用 Bigcommerce 集合(这不支持 api v3 和 laravel 集合)。

默认情况下,该包支持 API v3

要将它设置为版本 2 或 3,请使用

Bigcommerce::setApiVersion('v2');

Bigcommerce::setApiVersion('v3');

使用 Http 动词

Bigcommerce::get("resource uri",["query string params"]);
Bigcommerce::post("resource uri",["post body"]);
Bigcommerce::put("resource uri",["put body"]);
Bigcommerce::delete("resource uri");

让我们使用我们的访问令牌从 Bigcommerce 获取产品。

注意:您可以使用此方法访问 Bigcommerce 上的任何资源(无论是产品、商店、订单等),并且在使用基本身份验证时不需要存储散列和访问令牌。

$storeHash = "ecswer";
$accessToken = "xxxxxxxxxxxxxxxxxxxxx";
$products = Bigcommerce::setStoreHash($storeHash)->setAccessToken($accessToken)->get("products");

传递查询参数

// returns Collection
$bigcommerce = Bigcommerce::setStoreHash($storeHash)->setAccessToken($accessToken);
$products = $bigcommerce->get("admin/products.json", ["limit"=>20, "page" => 1]);

控制器示例

如果您像我一样喜欢使用依赖注入而不是外观,则可以注入该类

use Illuminate\Http\Request;
use Oseintow\Bigcommerce\Bigcommerce;

class Foo
{
    protected $bigcommerce;

    public function __construct(Bigcommerce $bigcommerce)
    {
        $this->bigcommerce = $bigcommerce;
    }

    /*
    * returns Collection
    */
    public function getProducts(Request $request)
    {
        $products = $this->bigcommerce->setStoreHash($storeHash)
            ->setAccessToken($accessToken)
            ->get('products');

        $products->each(function($product){
             \Log::info($product->title);
        });
    }
}

杂项

获取响应头

Bigcommerce::getHeaders();

获取特定的头

Bigcommerce::getHeader("Content-Type");

获取响应状态码或状态消息

Bigcommerce::getStatus(); // 200

使用 Bigcommerce 集合

测试配置

使用以下代码测试配置是否正确。如果失败则返回 false,否则返回 DateTime 对象。

$time = Bigcommerce::getTime();

访问资源

//  oauth
$storeHash = "afw2w";
$accessToken = "xxxxxxxxxxxxxxxxxxxxx";
$products = Bigcommerce::setStoreHash($storeHash)->setAccessToken($accessToken)->getProducts();

//Basic Auth
$products = Bigcommerce::getProducts();

分页和过滤

所有默认集合方法都支持分页,通过将页码作为整数传递给方法

$products = Bigcommerce::getProducts(3);

如果您需要更具体的编号和分页,可以显式指定一个限制参数

$filter = array("page" => 3, "limit" => 30);

$products = Bigcommerce::getProducts($filter);

要过滤集合,也可以传递参数作为键值对来过滤

$filter = array("is_featured" => true);

$featured = Bigcommerce::getProducts($filter);

请参阅每个资源的 API 文档以获取支持的过滤参数列表。

更新现有资源(PUT)

要更新单个资源

$product = Bigcommerce::getProduct(11);

$product->name = "MacBook Air";
$product->price = 99.95;
$product->update();

有关 Bigcommerce 集合的更多信息,请参阅此处