Skip to content

Commit

Permalink
Merge branch 'next-38048/fix-product-page-cross-selling' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-38048 - Consider all sections of a CMS page to search for reviews and cross-sellings

See merge request shopware/6/product/platform!14616
  • Loading branch information
mitelg committed Aug 27, 2024
2 parents 957c301 + 0eb7c57 commit 09e2876
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/Storefront/Page/Product/ProductPageLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Shopware\Storefront\Page\Product;

use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockCollection;
use Shopware\Core\Content\Cms\SalesChannel\Struct\CrossSellingStruct;
use Shopware\Core\Content\Cms\SalesChannel\Struct\ProductDescriptionReviewsStruct;
use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaCollection;
Expand Down Expand Up @@ -168,11 +169,21 @@ private function addDeprecatedData(ProductPage $page): void
return;
}

$blocks = $page->getCmsPage()?->getSections()?->first()?->getBlocks();
if ($blocks === null) {
$sections = $page->getCmsPage()?->getSections();
if ($sections === null) {
return;
}

$blocks = new CmsBlockCollection();
foreach ($sections as $section) {
$sectionBlocks = $section->getBlocks();
if ($sectionBlocks === null) {
continue;
}

$blocks->merge($sectionBlocks);
}

$descriptionReviewsStruct = $blocks->filterByProperty('type', ProductDescriptionReviewsCmsElementResolver::TYPE)->first()?->getSlots()?->first()?->getData();
if ($descriptionReviewsStruct instanceof ProductDescriptionReviewsStruct) {
$productReviewResult = $descriptionReviewsStruct->getReviews();
Expand Down
15 changes: 10 additions & 5 deletions tests/unit/Storefront/Page/Product/ProductPageLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Shopware\Core\Content\Product\Aggregate\ProductReview\ProductReviewEntity;
use Shopware\Core\Content\Product\Cms\CrossSellingCmsElementResolver;
use Shopware\Core\Content\Product\Cms\ProductDescriptionReviewsCmsElementResolver;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElementCollection;
use Shopware\Core\Content\Product\SalesChannel\Detail\ProductDetailRoute;
use Shopware\Core\Content\Product\SalesChannel\Detail\ProductDetailRouteResponse;
use Shopware\Core\Content\Product\SalesChannel\Review\ProductReviewResult;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function testItLoadsReviews(): void
static::assertInstanceOf(ProductReviewEntity::class, $firstReview);
static::assertSame('this product changed my life', $firstReview->getComment());
$crossSellingDeprecated = $page->getCrossSellings();
static::assertNotNull($crossSellingDeprecated);
static::assertInstanceOf(CrossSellingElementCollection::class, $crossSellingDeprecated);
static::assertCount(0, $crossSellingDeprecated);

$page->assign([
Expand Down Expand Up @@ -162,12 +163,16 @@ private function getCmsPage(SalesChannelProductEntity $productEntity): CmsPageEn
$reviewBlock = $this->getReviewBlock($productEntity);
$crossSellingBlock = $this->getCrossSellingBlock();

$cmsSectionEntity = new CmsSectionEntity();
$cmsSectionEntity->setId(Uuid::randomHex());
$cmsSectionEntity->setBlocks(new CmsBlockCollection([$reviewBlock, $crossSellingBlock]));
$firstCmsSectionEntity = new CmsSectionEntity();
$firstCmsSectionEntity->setId(Uuid::randomHex());
$firstCmsSectionEntity->setBlocks(new CmsBlockCollection([$reviewBlock]));

$secondCmsSectionEntity = new CmsSectionEntity();
$secondCmsSectionEntity->setId(Uuid::randomHex());
$secondCmsSectionEntity->setBlocks(new CmsBlockCollection([$crossSellingBlock]));

$cmsPageEntity = new CmsPageEntity();
$cmsPageEntity->setSections(new CmsSectionCollection([$cmsSectionEntity]));
$cmsPageEntity->setSections(new CmsSectionCollection([$firstCmsSectionEntity, $secondCmsSectionEntity]));

return $cmsPageEntity;
}
Expand Down

0 comments on commit 09e2876

Please sign in to comment.