Skip to content

Commit

Permalink
[FEATURE] sorts/prints article images in variant endpoint in the same…
Browse files Browse the repository at this point in the history
… way they would be assigned to frontend details view
  • Loading branch information
EvilBMP committed May 21, 2019
1 parent d330422 commit 76d4306
Showing 1 changed file with 58 additions and 12 deletions.
70 changes: 58 additions & 12 deletions Components/Api/Resource/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Written by Axel Boeswetter <[email protected]>, portrino GmbH
*/

use Shopware\Bundle\StoreFrontBundle\Struct\Media;
use Shopware\Bundle\StoreFrontBundle\Struct\Product;
use Shopware\Components\Api\Exception as ApiException;
use Shopware\Components\Api\Resource\Translation;
use Shopware\Components\Model\QueryBuilder;
Expand Down Expand Up @@ -79,9 +81,12 @@ public function getList($offset = 0, $limit = 25, array $criteria = [], array $o
}

try {
$params = $this->container->get('front')->Request()->getParams();
if (!array_key_exists('language', $options) && array_key_exists('language', $params)) {
$options['language'] = $params['language'];
$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) {
// ...
Expand All @@ -98,6 +103,15 @@ public function getList($offset = 0, $limit = 25, array $criteria = [], array $o
/** @var array $variant */
foreach ($variants as &$variant) {
$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);
}
unset($variant);
$this->translateVariants($variants, $shop);
Expand All @@ -109,9 +123,9 @@ public function getList($offset = 0, $limit = 25, array $criteria = [], array $o
/**
* @param array $variant
*
* @return array
* @throws ApiException\CustomValidationException
*
* @return array
*/
private function considerTaxInput(array $variant)
{
Expand Down Expand Up @@ -153,7 +167,7 @@ protected function getTranslationResource()
* Translate the whole product array.
*
* @param array $data
* @param Shop $shop
* @param Shop $shop
*
* @return array
*/
Expand Down Expand Up @@ -257,7 +271,7 @@ protected function translateArticle(array $data, Shop $shop)
* Translates the passed values array with the passed shop entity.
*
* @param array $values
* @param Shop $shop
* @param Shop $shop
*
* @return mixed
*/
Expand Down Expand Up @@ -292,7 +306,7 @@ protected function translatePropertyValues($values, Shop $shop)
* Translates the passed supplier data.
*
* @param array $supplier
* @param Shop $shop
* @param Shop $shop
*
* @return array
*/
Expand Down Expand Up @@ -321,7 +335,7 @@ protected function translateSupplier($supplier, Shop $shop)
* Translates the passed property group data.
*
* @param array $groupData
* @param Shop $shop
* @param Shop $shop
*
* @return array
*/
Expand Down Expand Up @@ -353,7 +367,7 @@ protected function translatePropertyGroup($groupData, Shop $shop)
* Translates the passed variants array and all associated data.
*
* @param array $details
* @param Shop $shop
* @param Shop $shop
*
* @return mixed
*/
Expand Down Expand Up @@ -430,8 +444,8 @@ protected function mergeTranslation($data, $translation)
/**
* Helper function which translates associated array data.
*
* @param array $association
* @param Shop $shop
* @param array $association
* @param Shop $shop
* @param string $type
*
* @return array
Expand All @@ -457,7 +471,7 @@ protected function translateAssociation(array $association, Shop $shop, $type)
* Helper function to get a single translation.
*
* @param string $type
* @param int $shopId
* @param int $shopId
* @param string $key
*
* @return array
Expand All @@ -472,4 +486,36 @@ protected function getSingleTranslation($type, $shopId, $key)

return $translation['data'][0];
}

/**
* @param array $images
* @param Product $product
* @return array
*/
private function getSortedArticleImages($images, $product)
{
$result = [];

if ($product->getCover()) {
foreach ($images as $image) {
if ($image['mediaId'] === $product->getCover()->getId()) {
$result[] = $image;
}
}
}
if ($product->getMedia()) {
/** @var Media $media */
foreach ($product->getMedia() as $media) {
foreach ($images as $image) {
if ($image['mediaId'] === $media->getId()) {
if (!$product->getCover() || ($product->getCover() && $media->getId() !== $product->getCover()->getId())) {
$result[] = $image;
}
}
}
}
}

return $result;
}
}

0 comments on commit 76d4306

Please sign in to comment.