From 7efedc9717253401d41435a0bcd9885e7ebb1669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20B=C3=B6swetter?= Date: Tue, 21 May 2019 15:43:20 +0200 Subject: [PATCH] [TASK] adds getOne() to Variant resource overwrite to correctly process translations and article images in ID/single requests as well --- Components/Api/Resource/Variant.php | 91 +++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/Components/Api/Resource/Variant.php b/Components/Api/Resource/Variant.php index c784b4b..1e4ca35 100644 --- a/Components/Api/Resource/Variant.php +++ b/Components/Api/Resource/Variant.php @@ -13,6 +13,7 @@ use Shopware\Components\Api\Exception as ApiException; use Shopware\Components\Api\Resource\Translation; use Shopware\Components\Model\QueryBuilder; +use Shopware\Models\Article\Detail; use Shopware\Models\Shop\Shop; /** @@ -23,6 +24,96 @@ class Variant extends \Shopware\Components\Api\Resource\Variant { + /** + * @param int $id + * @param array $options + * + * @return array|Detail + * @throws ApiException\CustomValidationException + * @throws ApiException\NotFoundException + * @throws ApiException\ParameterMissingException + * @throws ApiException\PrivilegeException + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function getOne($id, array $options = []) + { + $this->checkPrivilege('read'); + + if (empty($id)) { + throw new ApiException\ParameterMissingException(); + } + + $builder = $this->getRepository()->createQueryBuilder('detail') + ->addSelect([ + 'prices', + 'attribute', + 'partial article.{id,name,description,descriptionLong,active,taxId,changed}', + 'customerGroup', + 'options', + 'images' + ]) + ->leftJoin('detail.prices', 'prices') + ->innerJoin('prices.customerGroup', 'customerGroup') + ->leftJoin('detail.attribute', 'attribute') + ->innerJoin('detail.article', 'article') + ->leftJoin('article.images', 'images') + ->leftJoin('detail.configuratorOptions', 'options'); + + $builder->andWhere('detail.id = :variantId') + ->addOrderBy('detail.id', 'ASC') + ->addOrderBy('customerGroup.id', 'ASC') + ->addOrderBy('prices.from', 'ASC') + ->setParameter('variantId', $id); + + /** @var Detail|array $variant */ + $variant = $builder->getQuery()->getOneOrNullResult($this->getResultMode()); + + if (!$variant) { + throw new ApiException\NotFoundException(sprintf('Variant by id %d not found', $id)); + } + + if (($this->getResultMode() === self::HYDRATE_ARRAY) + && isset($options['considerTaxInput']) + && $options['considerTaxInput'] + ) { + $variant = $this->considerTaxInput($variant); + } + + try { + $frontController = Shopware()->Front(); + if ($frontController) { + $params = $frontController->Request()->getParams(); + if (!array_key_exists('language', $options) && array_key_exists('language', $params)) { + $options['language'] = $params['language']; + } + } + } catch (\Exception $e) { + // ... + } + + if ($this->getResultMode() === self::HYDRATE_ARRAY + && isset($options['language']) + && !empty($options['language'])) { + /** @var Shop $shop */ + $shop = $this->findEntityByConditions(Shop::class, [ + ['id' => $options['language']], + ]); + + $variant['article'] = $this->translateArticle($variant['article'], $shop); + + /** @var \Shopware\Bundle\StoreFrontBundle\Service\ProductServiceInterface $contextService */ + $productService = $this->container->get('shopware_storefront.product_service'); + /** @var \Shopware\Bundle\StoreFrontBundle\Service\ContextServiceInterface $contextService */ + $contextService = $this->container->get('shopware_storefront.context_service'); + /** @var Product $product */ + $product = $productService->get($variant['number'], $contextService->createShopContext($shop->getId())); + + $variant['article']['images'] = $this->getSortedArticleImages($variant['article']['images'], $product); + } + + return $variant; + } + /** * @param int $offset * @param int $limit