Skip to content

Commit

Permalink
Merge pull request #39 from Setono/fix-product-type
Browse files Browse the repository at this point in the history
Product type fix
  • Loading branch information
igormukhingmailcom authored Feb 10, 2021
2 parents eaa2030 + 54dc8cc commit 93c876e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
42 changes: 31 additions & 11 deletions src/FeedContext/Google/Shopping/ProductItemContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Setono\SyliusFeedPlugin\Model\GtinAwareInterface;
use Setono\SyliusFeedPlugin\Model\MpnAwareInterface;
use Setono\SyliusFeedPlugin\Model\SizeAwareInterface;
use Setono\SyliusFeedPlugin\Model\TaxonPathAwareInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ImagesAwareInterface;
Expand All @@ -29,6 +30,9 @@
use Sylius\Component\Core\Model\TaxonInterface;
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Model\TranslatableInterface;
use Sylius\Component\Resource\Model\TranslationInterface;
use Sylius\Component\Taxonomy\Model\TaxonTranslationInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Webmozart\Assert\Assert;
Expand Down Expand Up @@ -60,8 +64,14 @@ public function getContextList(object $product, ChannelInterface $channel, Local
));
}

$productType = $this->getProductType($product);
$excludeRootTaxon = false; // @todo Make it configurable
if ($product instanceof TaxonPathAwareInterface) {
$productType = $product->getTaxonPath($locale, $excludeRootTaxon);
} else {
$productType = $this->getProductType($product, $locale, $excludeRootTaxon);
}

/** @var ProductTranslationInterface|null $translation */
$translation = $this->getTranslation($product, (string) $locale->getCode());
$contextList = new ContextList();
foreach ($product->getVariants() as $variant) {
Expand Down Expand Up @@ -127,10 +137,10 @@ public function getContextList(object $product, ChannelInterface $channel, Local
return $contextList;
}

private function getTranslation(ProductInterface $product, string $locale): ?ProductTranslationInterface
private function getTranslation(TranslatableInterface $translatable, string $locale): ?TranslationInterface
{
/** @var ProductTranslationInterface $translation */
foreach ($product->getTranslations() as $translation) {
/** @var TranslationInterface $translation */
foreach ($translatable->getTranslations() as $translation) {
if ($translation->getLocale() === $locale) {
return $translation;
}
Expand Down Expand Up @@ -213,7 +223,7 @@ private function createPrice(int $price, ChannelInterface $channel): ?Price
return new Price($price, $baseCurrency);
}

private function getProductType(ProductInterface $product): ?string
private function getProductType(ProductInterface $product, LocaleInterface $locale, bool $excludeRoot = false): ?string
{
if ($product->getMainTaxon() !== null) {
$taxon = $product->getMainTaxon();
Expand All @@ -224,14 +234,24 @@ private function getProductType(ProductInterface $product): ?string
return null;
}

$productType = '';

$ancestors = array_reverse($taxon->getAncestors()->toArray());
$breadcrumbs = [];
array_unshift($breadcrumbs, $taxon);
for ($breadcrumb = $taxon->getParent(); null !== $breadcrumb; $breadcrumb = $breadcrumb->getParent()) {
array_unshift($breadcrumbs, $breadcrumb);
}

foreach ($ancestors as $ancestor) {
$productType .= $ancestor->getName() . ' > ';
if ($excludeRoot) {
// In cases when some root taxon assigned to channel's menuTaxon,
// we don't want to display root taxon - remove first item
array_shift($breadcrumbs);
}

return rtrim($productType, ' >');
return implode(' > ', array_map(function (TaxonInterface $breadcrumb) use ($locale): string {
/** @var TaxonTranslationInterface|null $translation */
$translation = $this->getTranslation($breadcrumb, (string) $locale->getCode());

// Fallback to default locale
return null !== $translation ? (string) $translation->getName() : (string) $breadcrumb->getName();
}, $breadcrumbs));
}
}
14 changes: 14 additions & 0 deletions src/Model/TaxonPathAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusFeedPlugin\Model;

use Sylius\Component\Locale\Model\LocaleInterface;

interface TaxonPathAwareInterface
{
// In cases when some root taxon assigned to channel's menuTaxon,
// we should exclude root taxon from path
public function getTaxonPath(LocaleInterface $locale, bool $excludeRoot = false): ?string;
}

0 comments on commit 93c876e

Please sign in to comment.