vienthuong / RecentlyViewedProduct

Recently viewed products plugin for SW6
MIT License
34 stars 10 forks source link

Slider includes current variant product #14

Open paul-per opened 1 year ago

paul-per commented 1 year ago

If you go on a detail page of a variant product, than to another product and than back to the first variant product: The first variant product will be in the slider.

Fix: in "RecentlyViewedProductService" exchange the function "buildRecentProductSliderStruct" with this code:

public function buildRecentProductSliderStruct(SalesChannelContext $context, ?array $excludeProductIds = []): ProductSliderStruct
{
    $products = $this->getRecentProductEntities($context) ?? new ProductCollection([]);

    if (!empty($excludeProductIds)) {
        foreach ($excludeProductIds as $productId) {
            if($products->has($productId)){
                $products->remove($productId);
                continue;
            }

            // If the $productId is from a child, exclude the parent
            $criteria = new Criteria();
            $criteria->addFilter(new EqualsFilter('id', $productId));
            $searchResult = $this->salesChannelProductRepository->search($criteria, $context);
            $product = $searchResult->getEntities()->first();

            $products = $products->filter(function(SalesChannelProductEntity $element) use ($product) {
                if($product->getParentId() !== $element->getId()){
                    return true;
                }
                return false;
            });
        }
    }

    $productSliderStruct = new ProductSliderStruct();
    $productSliderStruct->setProducts($products);

    return $productSliderStruct;
}
LouisEiermann commented 6 months ago

@vienthuong Isnt this fixed already? I noticed that you can give the slider an option to exclude the current variant

vienthuong commented 6 months ago

@LouisEiermann sorry I don't have time to check it in anytime soon, could you give it a go?