0

When I foreach my collection filtered by tier price, qty and customer group I get the error: "Item (Magento \ Catalog \ Model \ Product \ Interceptor) with the same ID already exists".

This is my class:

namespace Vendor\Module\Block;
class ProductCollection extends \Magento\Framework\View\Element\Template
{ 

protected $_productCollectionFactory;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
        array $data = []
    )
    {    
        $this->_productCollectionFactory = $productCollectionFactory;    
        parent::__construct($context, $data);
    }

public function getProductCollectionSearch() {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setPageSize(3);

        $collection->getSelect()->join('catalog_product_index_price as price_index', 'e.entity_id = price_index.entity_id'); 
        $collection->getSelect()->join('catalog_product_entity_tier_price as tier', 'price_index.entity_id = tier.entity_id');

        $collection->addFinalPrice()
            ->addMinimalPrice()
            ->getSelect()
            ->where('tier.all_groups = 0')
            ->where('tier.qty = 1000')
            ->where('tier.value <= 0.20');

        return $collection;
    }
}

This problem arises when I try to foreach product in my phtml file. This is my phtml file:

<?php
$blockProductColl = $block->getLayout()->createBlock('\Vendor\Module\Block\ProductCollection');
$collectionSearch = $blockProductColl->getProductCollectionSearch();

$count=0;

//echo $collectionSearch->getSize();
foreach($collectionSearch as $itemSearch){
    echo $itemSearch->getSku().'<br/>';
    $count++;
}
?>

Is there a way to exclude duplicate Item IDs before foreaching the collection? Thanks!

SOLUTION:

public function getProductCollectionSearch() {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToFilter('visibility', 4);
        //$collection->setPageSize(10);
        
        $joinConditions = 'e.entity_id = tier.entity_id';
        $collection->getSelect()->join(
             'catalog_product_entity_tier_price as tier',
             $joinConditions
            ) 
              ->where('tier.customer_group_id = 0')
              ->where('tier.qty = 1000')
              ->where('tier.value <= 0.10')
              ->where('tier.website_id = 0');
         return $collection;
    }

1 Answer 1

1

please add

       ->where('tier.website_id = 0');
7
  • or you can check here rscoder.com/2019/12/18/… May 20, 2021 at 10:19
  • Man i tried to add ->where('tier.website_id = 0'); but issue remain. I don't know whether to try the module you suggested because my collection, if I don't join the table, works correctly. Can I do it any other way in your opinion? Thx
    – Jackom
    May 20, 2021 at 10:27
  • Man, i tried in this moment and now works! works! works! Thans for the help! I updated my code for the final solution ;)
    – Jackom
    May 20, 2021 at 10:43
  • happy to hear it works for you :) May 20, 2021 at 10:45
  • One last questione please: If i set for example ->where('tier.qty = 50500') and this qty not exist in my tier price quantity table, how can i make him take the amount range rounded down, ie 50000 that exists in my database?
    – Jackom
    May 21, 2021 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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