-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #15139 Fix "Show product in the shop page" button (mamazu, jakubt…
…obiasz, GSadee) This PR was merged into the 1.12 branch. Discussion ---------- | Q | A | |-----------------|--------------------------------------------------------------| | Branch? | 1.12 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | replaces #14663 | License | MIT Commits ------- d24183f Adding default if product has no slug 98d853e Adding try logic to get the shop's slug 9856b1e Refactor logic behind a button redirecting to the product's shop page a394383 Add support for secure and unsecured URLs for generating product show page 60000de Remove unknown localeCode named paramter bfb011d Allow passing a desired locale code to DefaultChannelFactory cf86d24 Provide post-CR fixes ae721b5 Refactor to provide a product translation instead of generate product shop url
- Loading branch information
Showing
21 changed files
with
500 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
features/product/managing_products/accessing_stores_product_page.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@managing_products | ||
Feature: Accessing a store's product page | ||
In order to access a store's product page from the admin panel | ||
As an Administrator | ||
I want to have a way to be redirected to the store's product page from the admin panel | ||
|
||
Background: | ||
Given the store operates on a single channel in "German (Germany)" locale | ||
And the store has a product "Französischer Bulldoggen T-Shirt" | ||
And I am logged in as an administrator | ||
And I am using "Polish (Poland)" locale for my panel | ||
|
||
@ui @no-api | ||
Scenario: Opening a product's page from the admin panel when the product has a translation with a defined slug in the administrator's chosen language | ||
Given the locale "Polish (Poland)" is enabled | ||
And this product is named "Bulldog francuski T-Shirt" in the "Polish (Poland)" locale | ||
When I want to edit this product | ||
Then the show product's page button should be enabled | ||
And it should be leading to the product's page in the "Polish (Poland)" locale | ||
|
||
@ui @no-api | ||
Scenario: Opening a product's page from the admin panel when the product has a translation with a defined slug in the default channel's language | ||
Given the locale "French (France)" is enabled | ||
And this product is named "Tee-shirt bouledogue français" in the "French (France)" locale | ||
When I want to edit this product | ||
Then the show product's page button should be enabled | ||
And it should be leading to the product's page in the "German (Germany)" locale | ||
|
||
@ui @no-api | ||
Scenario: Opening a product's page from the admin panel with using first available locale with slug and enabled in the channel | ||
Given the locale "French (France)" is enabled | ||
And the store also operates in "French (France)" locale | ||
And this product has no slug in the "German (Germany)" locale | ||
And this product is named "T-shirt bouledogue français" in the "French (France)" locale | ||
When I want to edit this product | ||
Then it should be leading to the product's page in the "French (France)" locale | ||
|
||
@ui @no-api | ||
Scenario: Not being able to open a product's page from the admin panel when the product has no translations meeting the criteria | ||
Given this product has no translations with a defined slug | ||
When I want to edit this product | ||
Then the show product's page button should be disabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Sylius/Bundle/CoreBundle/Provider/ChannelBasedProductTranslationProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Sylius Sp. z o.o. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\CoreBundle\Provider; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
use Sylius\Component\Core\Model\ChannelInterface; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Sylius\Component\Core\Model\ProductTranslationInterface; | ||
use Sylius\Component\Locale\Context\LocaleContextInterface; | ||
use Sylius\Component\Locale\Model\LocaleInterface; | ||
|
||
final class ChannelBasedProductTranslationProvider implements ChannelBasedProductTranslationProviderInterface | ||
{ | ||
public function __construct(private LocaleContextInterface $localeContext) | ||
{ | ||
} | ||
|
||
public function provide(ProductInterface $product, ChannelInterface $channel): ?ProductTranslationInterface | ||
{ | ||
/** @var Collection<array-key, ProductTranslationInterface> $productTranslations */ | ||
$productTranslations = $product->getTranslations(); | ||
|
||
$contextLocaleCode = $this->localeContext->getLocaleCode(); | ||
$productTranslation = $this->findTranslationWithSlugForLocales($productTranslations, [$contextLocaleCode]); | ||
|
||
if (null !== $productTranslation) { | ||
return $productTranslation; | ||
} | ||
|
||
/** @var string $channelDefaultLocaleCode */ | ||
$channelDefaultLocaleCode = $channel->getDefaultLocale()->getCode(); | ||
$productTranslation = $this->findTranslationWithSlugForLocales($productTranslations, [$channelDefaultLocaleCode]); | ||
|
||
if (null !== $productTranslation) { | ||
return $productTranslation; | ||
} | ||
|
||
$localesEnabledInChannel = $this->getLocalesCodesEnabledInChannel($channel); | ||
$productTranslation = $this->findTranslationWithSlugForLocales($productTranslations, $localesEnabledInChannel); | ||
|
||
return $productTranslation; | ||
} | ||
|
||
/** | ||
* @param Collection<array-key, ProductTranslationInterface> $productTranslations | ||
* @param array<string> $localeCodes | ||
*/ | ||
private function findTranslationWithSlugForLocales(Collection $productTranslations, array $localeCodes): ?ProductTranslationInterface | ||
{ | ||
foreach ($productTranslations as $productTranslation) { | ||
$isLocaleCodeMatching = in_array($productTranslation->getLocale(), $localeCodes); | ||
$isSlugPresent = '' !== $productTranslation->getSlug() && null !== $productTranslation->getSlug(); | ||
|
||
if ($isLocaleCodeMatching && $isSlugPresent) { | ||
return $productTranslation; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** @return array<array-key, string> */ | ||
private function getLocalesCodesEnabledInChannel(ChannelInterface $channel): array | ||
{ | ||
return $channel->getLocales()->map(function (LocaleInterface $locale): string { | ||
return $locale->getCode(); | ||
})->toArray(); | ||
} | ||
} |
Oops, something went wrong.