此包的最新版本(dev-main)没有可用的许可证信息。

Shopware SDK

dev-main 2023-05-08 12:27 UTC

This package is auto-updated.

Last update: 2024-09-08 15:36:58 UTC


README

codecov

赞助

Shopware SDK 正在开发中

通过更快的 SDK API 提高与 Shopware API 的集成,该 API 专为与外部系统无缝连接而设计。

🔄 是时候进化了!放弃使用数组,拥抱对象的力量。🔄

🛠️ Shopware SDK 是您进行这种转换的必备工具。🛠️

🔍 找不到所有的 API 端点?不用担心!如果您发现任何遗漏,请友好地创建一个问题或提交一个 PR。

您的贡献总是受欢迎的!🤗

目录

  1. 如何使用
    1. Composer
    2. 初始化 SDK
  2. 扩展 SDK
    1. 创建新服务
    2. 替换或扩展默认服务
  3. 本地工作

如何使用

Composer

由于 Shopware SDK 目前正在开发中,我们尚未分配版本。完成后,我们将在 Packagist 上创建一个版本。

将此代码添加到您的 composer.json 中

"require": {
    ...
    "shopware-sdk/sdk": "dev-main"
}

初始化 SDK

$config = new \ShopwareSdk\Config(
    'http://my.shopware.com',
    'SWIAxxxxxxxxxxxxxxxxxxZVTG',
    'eWd3Qnc1R0U3ZmFjUDxxxxxxxxxxxxxxxxJCT3JzS3hvUHNyN0w',
);

$adminApi = new \ShopwareSdk\AdminApi($config);

$currencies = $adminApi->currency->getAll();
var_dump($currencies);

输出

array(2) {
  [0]=>
  object(ShopwareSdk\Model\Currency)#3 (5) {
    ["id"]=>
    string(36) "c6d8c3f0-8b1e-4b0e-9b2e-8c3f0b1e4b0e"
    ["name"]=>
    string(3) "USD"
    ["isoCode"]=>
    string(3) "USD"
    ["symbol"]=>
    string(1) "$"
    ["factor"]=>
    int(100)
  }
  [1]=>
  object(ShopwareSdk\Model\Currency)#4 (5) {
    ["id"]=>
    string(36) "c6d8c3f0-8b1e-4b0e-9b2e-8c3f0b1e4b0e"
    ["name"]=>
    string(3) "EUR"
    ["isoCode"]=>
    string(3) "EUR"
    ["symbol"]=>
    string(1) ""
    ["factor"]=>
    int(100)
  }
}

扩展 SDK

创建新服务

可能存在没有服务而你需要的场景。那么你可以简单地创建它并注册给 AdminApi。

示例

我们创建一个 NewService 并扩展 AbstractService 以访问 request 方法。

namespace App\ShopwareSdk\Service;

use ShopwareSdk\Service\AbstractService;

class NewService extends AbstractService
{
    protected const URL = '/api/new_service/';
    
    public function getAll(): NewServiceCollection
    {
        return $this->request('GET', self::URL, NewServiceCollection::class);
    }
}

现在当我们创建 AdminApi 时,我们只需要说服务可以通过哪个名字访问。

use App\ShopwareSdk\Service\NewService;

$config = new Config(
    'https://shopware.test',
    'clientId',
    'clientSecret',
    [
        'newService' => NewService::class,
    ]
);

$adminApi = new AdminApi($config);

现在我们可以通过 AdminApi 访问服务。

/** @var NewService $newService */
$newService = $adminApi->newService;
$newService->getAll();

替换或扩展默认服务

你可能想扩展或更改现有服务。例如,你可能想添加新方法或更改模型类。

示例

我们创建一个 ProductService 并扩展 ProductService 以添加新方法。

namespace App\ShopwareSdk\Service;

use ShopwareSdk\Service\ProductService;

class MyProductService extends ProductService
{
    public function getNewFancyMethod(): NewServiceCollection
    {
        return $this->request('GET', self::URL, NewServiceCollection::class);
    }
}

现在我们只需告诉我们的 Config,Product-Service 是我们的 ProductService。

use App\ShopwareSdk\Service\MyProductService

$config = new Config(
    'https://shopware.test',
    'clientId',
    'clientSecret',
    [
        'product' => MyProductService::class,
    ]
);

$adminApi = new AdminApi($config);

现在我们可以通过 AdminApi 访问服务。

/** @var MyProductService $newService */
$product = $adminApi->product;
$product->getNewFancyMethod();

本地工作

启动 shopware-demo shopware 实例并设置客户端凭证

docker run --rm -p 8000:80 dockware/play

python3 .github/api_sw_client.py

Python 库: pip install requests