add shopware store-api
This commit is contained in:
93
src/Shopware/ShopwareCriteriaBuilder.php
Normal file
93
src/Shopware/ShopwareCriteriaBuilder.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shopware;
|
||||
|
||||
use App\Commerce\Dto\CommerceSearchQuery;
|
||||
|
||||
final class ShopwareCriteriaBuilder
|
||||
{
|
||||
public function build(CommerceSearchQuery $query, int $limit = 25): array
|
||||
{
|
||||
$criteria = [
|
||||
'page' => 1,
|
||||
'limit' => max(1, $limit),
|
||||
"grouping" => ["parentId"],
|
||||
'total-count-mode' => 0,
|
||||
'includes' => [
|
||||
'product' => [
|
||||
'id',
|
||||
'name',
|
||||
'description',
|
||||
'productNumber',
|
||||
'available',
|
||||
'calculatedPrice',
|
||||
'seoUrls',
|
||||
'manufacturer',
|
||||
'translated.name'
|
||||
],
|
||||
'product_manufacturer' => [
|
||||
'name',
|
||||
],
|
||||
'calculated_price' => [
|
||||
'unitPrice',
|
||||
'totalPrice',
|
||||
'referencePrice',
|
||||
'listPrice',
|
||||
'regulationPrice'
|
||||
],
|
||||
'seo_url' => [
|
||||
'seoPathInfo',
|
||||
],
|
||||
],
|
||||
'associations' => [
|
||||
'manufacturer' => new \stdClass(),
|
||||
'seoUrls' => new \stdClass(),
|
||||
],
|
||||
'sort' => [
|
||||
[
|
||||
'field' => 'name',
|
||||
'order' => 'ASC',
|
||||
'naturalSorting' => true,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
if ($query->searchText !== '') {
|
||||
$criteria['term'] = $query->searchText;
|
||||
}
|
||||
|
||||
$filters = [
|
||||
[
|
||||
'type' => 'equals',
|
||||
'field' => 'active',
|
||||
'value' => true,
|
||||
],
|
||||
[
|
||||
'type' => 'equals',
|
||||
'field' => 'available',
|
||||
'value' => true,
|
||||
],
|
||||
[
|
||||
'type' => 'range',
|
||||
'field' => 'price.gross',
|
||||
'parameters' => [
|
||||
'gt' => 0,
|
||||
],
|
||||
]
|
||||
];
|
||||
|
||||
if ($query->priceMin !== null) {
|
||||
$criteria['min-price'] = $query->priceMin;
|
||||
}
|
||||
|
||||
if ($query->priceMax !== null) {
|
||||
$criteria['max-price'] = $query->priceMax;
|
||||
}
|
||||
|
||||
$criteria['filter'] = $filters;
|
||||
|
||||
return $criteria;
|
||||
}
|
||||
}
|
||||
53
src/Shopware/StoreApiClient.php
Normal file
53
src/Shopware/StoreApiClient.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Shopware;
|
||||
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
final readonly class StoreApiClient
|
||||
{
|
||||
public function __construct(
|
||||
private HttpClientInterface $httpClient,
|
||||
private string $baseUrl,
|
||||
private string $salesChannelAccessKey,
|
||||
private int $timeoutSeconds = 5,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TransportExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
public function searchProducts(array $criteria): array
|
||||
{
|
||||
$url = rtrim($this->baseUrl, '/') . '/store-api/product';
|
||||
|
||||
$response = $this->httpClient->request('POST', $url, [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
'sw-access-key' => $this->salesChannelAccessKey,
|
||||
],
|
||||
'json' => $criteria,
|
||||
'timeout' => $this->timeoutSeconds,
|
||||
]);
|
||||
|
||||
$statusCode = $response->getStatusCode();
|
||||
if ($statusCode < 200 || $statusCode >= 300) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $response->toArray(false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user