jtl / shopware6-client
Client for the Shopware 6 Management API
This package has no released version yet, and little information is available.
README
This is a client for the Shopware 6 Management RESTful API. Full documentation of the API can be found at https://docs.shopware.com/en/shopware-platform-dev-en/api.
Basic usage
<?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);
Sync Entities
Sync request allows to update / insert / delete multiple entities at once. 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);
Using filters / sort
Currently supported filters are RangeFilter and ValueFilter.
<?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);
Saving entity translations
When saving entity translation you need to set it on translations property in current entity. In order to save translation entity translation object must have valid (UUIDv4) languageId property.
<?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);
Upload media files
Two steps are needed for uploading a new file:
- Create Media entity and save it
- Upload media data
File names are unqiue in Shopware 6. So you can't upload two different files with the same name. Use the "$renameIfNecessary" flag if you want to upload the file in any case.
<?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);
Entity generator
For testing purposes we created an entity generator. You can generate entities with random values easily.
<?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 ]);