Skip to content

Commit

Permalink
[TASK] adds getOne() to Variant resource overwrite to correctly proce…
Browse files Browse the repository at this point in the history
…ss translations and article images in ID/single requests as well
  • Loading branch information
EvilBMP committed May 21, 2019
1 parent 76d4306 commit 7efedc9
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Components/Api/Resource/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down

0 comments on commit 7efedc9

Please sign in to comment.