Skip to content

Commit

Permalink
Fix potential issues with products only in Root Category
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Feb 3, 2024
1 parent 25218ed commit 772022f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fix integration test
- Fix potential issues with products only in Root Category

## [3.7.8] - 3 February 2024
### Fixed
Expand Down
1 change: 0 additions & 1 deletion Test/Integration/Page/AddToWishlistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Customer\CustomerData\SectionPool;
use Magento\Customer\Model\Session as CustomerSession;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\Exception\LocalizedException;
Expand Down
33 changes: 25 additions & 8 deletions Util/CategoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ public function __construct(
*/
public function addCategoryIds(array $categoryIds)
{
$categoryIds = $this->filterRootCategoryIdFromCategoryIds($categoryIds);
if (empty($categoryIds)) {
return;
}

$rootCategoryId = $this->getRootCategoryId();
$categoryIds = array_filter($categoryIds, function ($categoryId) use ($rootCategoryId) {
return (int)$categoryId !== $rootCategoryId;
});

$this->categoryIds = array_unique(array_merge($this->categoryIds, $categoryIds));
}

Expand Down Expand Up @@ -111,15 +107,23 @@ public function getLoadedCategories(): array
*/
public function getFirstByProduct(ProductInterface $product): CategoryInterface
{
$productCategoryIds = $product->getCategoryIds();
$productCategoryIds = $this->filterRootCategoryIdFromCategoryIds($product->getCategoryIds());
if (empty($productCategoryIds)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}

$productCategoryId = array_shift($productCategoryIds);
$this->addCategoryIds([$productCategoryId]);
if (empty($this->categoryIds)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}

$categories = $this->getLoadedCategories();
if (false === array_key_exists($productCategoryId, $categories)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}

return $this->getLoadedCategories()[$productCategoryId];
return $categories[$productCategoryId];
}

/**
Expand All @@ -129,7 +133,7 @@ public function getFirstByProduct(ProductInterface $product): CategoryInterface
*/
public function getAllByProduct(ProductInterface $product): array
{
$productCategoryIds = $product->getCategoryIds();
$productCategoryIds = $this->filterRootCategoryIdFromCategoryIds($product->getCategoryIds());
if (empty($productCategoryIds)) {
throw new NoSuchEntityException(__('Product "' . $product->getSku() . '" has no categories'));
}
Expand Down Expand Up @@ -181,6 +185,19 @@ private function loadCategoriesByIds(array $categoryIds): array
return $this->categoryListRepository->getList($searchCriteria)->getItems();
}

/**
* @param array $categoryIds
* @return array
* @throws NoSuchEntityException
*/
private function filterRootCategoryIdFromCategoryIds(array $categoryIds): array
{
$rootCategoryId = $this->getRootCategoryId();
return array_filter($categoryIds, function ($categoryId) use ($rootCategoryId) {
return (int)$categoryId !== $rootCategoryId;
});
}

/**
* @return int
* @throws NoSuchEntityException
Expand Down

0 comments on commit 772022f

Please sign in to comment.