jtl / shopware6-client
此包已被废弃,不再维护。未建议替代包。
Shopware 6 管理API客户端
此包尚未发布版本,可用的信息很少。
README
这是Shopware 6管理RESTful API的客户端。API的完整文档可以在https://docs.shopware.com/en/shopware-platform-dev-en/api找到。
基本用法
<?php use Jtl\Shopware6\Client\ApiClient; use Jtl\Shopware6\Client\Entity\Product; $clientId = 'SWIAAGVXUKHTWKJLAWDMM2FUVG'; $clientSecret = 'OXZrRGF1TUFtQU11MHFWb0wwZXpYWHNtM3dKY3BiN0JTbkh0eUc'; $baseUri = 'https://my-fancy-shop.url'; $client = new ApiClient($clientId, $clientSecret, $baseUri); // Get a specific entity $product = $client->getEntity("c801ed62484c4381a4cd81fc26e141b9", Product::class); // Get List of entities $list = $client->getEntities(Product::class); // Iterate over entities list as array foreach($list as $uuid => $product) { // ...Do something... } // Or check if an entity with specific uuid exists in the list and get it if($list->hasEntity('awesomeUuid')) { $entity = $list->getEntity('awesomeUuid'); } // Or get entities array without uuid as index $entities = $list->toArray(); // Save (create/update) entity $client->saveEntity($product); // Delete entity $client->deleteEntity($product);
同步实体
同步请求允许一次性更新/插入/删除多个实体。https://docs.shopware.com/en/shopware-platform-dev-en/api/sync-api
<?php use \Jtl\Shopware6\Client\Request\SyncItem; use \Jtl\Shopware6\Client\Request\SyncRequest; $items = [ (new SyncItem( SyncItem::ACTION_UPSERT, $newCategoryEntity )), (new SyncItem( SyncItem::ACTION_UPSERT, $existingCategoryEntity )), (new SyncItem( SyncItem::ACTION_DELETE, $existingProductEntity )) ]; $syncRequest = new SyncRequest(...$items); $client->syncEntities($syncRequest);
使用过滤器/排序
目前支持的过滤器有 范围过滤器 和 值过滤器。
<?php use Jtl\Shopware6\Client\Entity\Product; use Jtl\Shopware6\Client\Request\Filter\AbstractFilter; use Jtl\Shopware6\Client\Request\Filter\ValueFilter; use Jtl\Shopware6\Client\Request\SearchRequest; use Jtl\Shopware6\Client\Request\Sort; use Jtl\Shopware6\Client\Request\Filter\RangeFilter; // Set one or more filters $searchRequest = new SearchRequest(); $searchRequest->setFilters( new ValueFilter(AbstractFilter::TYPE_CONTAINS, 'customer_number', 'pre12-'), (new RangeFilter('related_field')) ->addParameter(RangeFilter::OPERATOR_GREATER_THAN_EQUAL, 0) ->addParameter(RangeFilter::OPERATOR_LESS_THAN, 50) ); // Set one or more sort conditions $searchRequest->setSort( new Sort('last_name','desc'), new Sort('first_name', 'asc') ); $client->getEntities(Product::class, $searchRequest);
保存实体翻译
在保存实体翻译时,您需要在当前实体中将其设置为translations属性。为了保存翻译实体,翻译对象必须具有有效的(UUIDv4)languageId属性。
<?php use Jtl\Shopware6\Client\Entity\Category; use Jtl\Shopware6\Client\Entity\CategoryTranslation; $category = $client->getEntity('828d0e14eee94eedbdff86b55d118f85', Category::class); // directly change properties $category->getTranslation($languageIdEn)->setName("Fancy product"); $category->getTranslation($languageIdDE)->setName("Jeiles Produkt"); // or create a new translation $categoryTranslation = (new CategoryTranslation()) //set required properties... ->setName("Another product") ->setLanguageId('...'); //add/set translation depends on languageId $category->setTranslation($categoryTranslation); $client->saveEntity($category);
上传媒体文件
上传新文件需要两步
- 创建媒体实体并保存
- 上传媒体数据
在Shopware 6中,文件名是唯一的。因此,您不能上传具有相同名称的两个不同文件。如果无论如何都要上传文件,请使用"$renameIfNecessary"标志。
<?php use Jtl\Shopware6\Client\Entity\Media; // Create media if you want to upload a new file $media = new Media(); // Save new media entity $client->saveEntity($media); // Or use an existing media if you want to replace a file $mediaId = $media->getId(); // Upload media data directly from binary string $client->uploadMedia($mediaId, 'unique_filename', 'png', file_get_contents('/path/to/file.png')); // Or upload media data from a resource $client->uploadMediaFromResource($mediaId, 'unique_filename', 'gif', $resourceHandle); // Or upload media data from a file where original file name should be taken $client->uploadMediaFromFile($mediaId, '/path/to/fancy.jpg'); // Or upload media data from a remote url $client->uploadMediaFromUrl($mediaId, 'unique_filename', 'jpg', 'https://remote.url/to/fancy.jpg'); // Delete a specific media by deleting the related entity $client->deleteEntity($media);
实体生成器
出于测试目的,我们创建了一个实体生成器。您可以轻松地使用随机值生成实体。
<?php use Jtl\Shopware6\Client\Entity\Customer; use Jtl\Shopware6\Client\Entity\Generator\CustomerFactory; $customerFactory = new CustomerFactory(); /** @var Customer $customer */ $customer = $customerFactory->makeOne([ 'customerNumber' => 1 ]);