2

I want to add some automated products to the related products and had this working great however suddenly it has become stuck showing the same products. I have used a category Id that I put in the registry from breadcrumbs here to filter the most viewed collection however no matter what this seems to be stuck on the same products. The category ID in the code below is changing fine on each product however the collection remains the same on each page.

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Checkout\Model\ResourceModel\Cart $checkoutCart,
    \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Framework\Module\Manager $moduleManager,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Store\Model\Store $_storeManager,
    \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $mostViewedCollectionFactory,
    Registry $registry,
    array $data = []
) {
    $this->_checkoutCart = $checkoutCart;
    $this->_catalogProductVisibility = $catalogProductVisibility;
    $this->_checkoutSession = $checkoutSession;
    $this->moduleManager = $moduleManager;
    $this->_categoryFactory = $categoryFactory;
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_storeManager = $_storeManager;
    $this->mostViewedCollection = $mostViewedCollectionFactory;
    $this->registry = $registry;
    parent::__construct(
        $context,
        $checkoutCart,
        $catalogProductVisibility,
        $checkoutSession,
        $moduleManager,
        $data
    );
}

protected function _prepareData()
{
    $storeId = $this->_storeManager->getStore()->getId();
    $productreg = $this->_coreRegistry->registry('product');
    if($categoryIds = $this->registry->registry('cat_ids')) {
        $categoryId = $categoryIds[1];
    }
    /* @var $product \Magento\Catalog\Model\Product */

    $this->_itemCollection = $productreg->getRelatedProductCollection()->addAttributeToSelect(
        'required_options'
    )->setPositionOrder()->addStoreFilter();

    if ($this->moduleManager->isEnabled('Magento_Checkout')) {
        $this->_addProductAttributesAndPrices($this->_itemCollection);
    }
    $this->_itemCollection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());

    $count = 8 - count($this->_itemCollection);
    $this->_itemCollection->load();

    if($categoryId) {
        $collectionCat = clone $this->mostViewedCollection->create();
        $collectionCat->clear();
        $collectionCat->addAttributeToSelect(
            ['name','price','small_image']
        )->addCategoriesFilter(['eq' => $categoryId]
        )->addViewsCount(
        )->setStoreId(
            $storeId
        )->addStoreFilter(
            $storeId
        )->setPageSize(
            $count
        );

        foreach($collectionCat as $item)
        {   
            if($item->getSku() != $productreg->getSku()) {
                try {
                    $this->_itemCollection->addItem($item);
                } catch (\Exception $e) {
                }
            }
        }
        return $this;
    }

}

I've tried clone and clear like seen in other collections to try and reset but really was just guessing there but it didn't seem to change the results. I have also disabled all caches with same results on each page.

0

Seemed to be the order of the filters:

$collectionCat->addAttributeToSelect(
        ['name','price','small_image']

    )->addViewsCount(
    )->setStoreId(
        $storeId
    )->addStoreFilter(
        $storeId
    )->setPageSize(
        $count
    )->addCategoriesFilter(['eq' => $categoryId]);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.