Magento 2 Add product to product collection Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Unable to synchronized app import configuration failedNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Trying to uninstall moduleMagento2.2.4 : Uninstall a theme throw exceptions?Add configure product in Cart using Magento 2 API facing an issueMagento 2 Front controller reached 100 router match iterations issueDeleted ShipperHQ module causing error in “All Customers” section of Magento 2“Area code is not set” in var/logwhen click on place order then paypal showing error in Magento2magento error logs Error log record number: 1173359093956

2001: A Space Odyssey's use of the song "Daisy Bell" (Bicycle Built for Two); life imitates art or vice-versa?

3 doors, three guards, one stone

Why light coming from distant stars is not discrete?

Can a USB port passively 'listen only'?

How do pianists reach extremely loud dynamics?

What's the purpose of writing one's academic biography in the third person?

String `!23` is replaced with `docker` in command line

Short Story with Cinderella as a Voo-doo Witch

How does the particle を relate to the verb 行く in the structure「A を + B に行く」?

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

How much time will it take to get my passport back if I am applying for multiple Schengen visa countries?

When do you get frequent flier miles - when you buy, or when you fly?

How do I keep my slimes from escaping their pens?

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

Generate an RGB colour grid

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Why did the rest of the Eastern Bloc not invade Yugoslavia?

Understanding Ceva's Theorem

Why are both D and D# fitting into my E minor key?

Should I use a zero-interest credit card for a large one-time purchase?

How to tell that you are a giant?

List *all* the tuples!

What does this icon in iOS Stardew Valley mean?

Echoing a tail command produces unexpected output?



Magento 2 Add product to product collection



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Unable to synchronized app import configuration failedNot able to add/update magento2 category post upgrade from 1.9 to 2.1.0Trying to uninstall moduleMagento2.2.4 : Uninstall a theme throw exceptions?Add configure product in Cart using Magento 2 API facing an issueMagento 2 Front controller reached 100 router match iterations issueDeleted ShipperHQ module causing error in “All Customers” section of Magento 2“Area code is not set” in var/logwhen click on place order then paypal showing error in Magento2magento error logs Error log record number: 1173359093956



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















currently i'm working on an extension which show the last products which sold the last 10 day. I got this products with the MagentoReportsModelResourceModelProductSoldCollection.



Vender/Name/etc/di.xml:



<type name="MagentoCatalogModelLayer">
<plugin name="vendor_add_last_sold_products" type="VendorNamePluginLayer" />
</type>


Vender/Name/Plugin/Layer.php:



namespace VendorNamePlugin;

use MagentoCatalogModelResourceModelProductCollection;
use MagentoCatalogModelResourceModelProductCollectionFactory;
use MagentoCatalogModelProduct;
use MagentoFrameworkRegistry;
use VendorNameHelperOrders;

class Layer

protected $_orders;

protected $_product;

protected $_registry;

public function __construct(
Orders $orders,
Product $product,
Registry $registry
)

$this->_orders = $orders;
$this->_product = $product;
$this->_registry = $registry;


public function aroundGetProductCollection(MagentoCatalogModelLayer $subject,Closure $proceed)

$collection = $proceed();
$category = $this->_registry->registry('current_category')->getId();
$sold_products = $this->_orders->getLastSoldProducts($category);

if($sold_products)
foreach($sold_products as $product)
if (!array_key_exists((int)$product->getId(), $collection->getItems()))
$collection->addItem($this->_product->load((int)$product->getId()));




return $collection;




In $sold_products i saved all sold products. And with:



if($sold_products) {
foreach($sold_products as $product)
if (!array_key_exists((int)$product->getId(), $collection->getItems()))
$collection->addItem($this->_product->load((int)$product->getId()));




I loop through all sold products and add them to the product collection. But i get always the following error:



1 exception(s):
Exception #0 (RuntimeException): Illegal state

Exception #0 (RuntimeException): Illegal state
#0 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(63): MagentoCatalogSearchModelResourceModelFulltextCollection->addFieldToFilter('category_ids', '37')
#1 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php(458): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addFieldToFilter('category_ids', '37')
#2 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(128): MagentoCatalogSearchModelResourceModelFulltextCollection->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
#3 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php(74): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
#4 /var/www/local.sandoro.com/vendor/magento/module-layered-navigation/Block/Navigation.php(67): MagentoCatalogSearchModelLayerFilterCategory->apply(Object(MagentoFrameworkAppRequestHttp))
#5 /var/www/local.sandoro.com/vendor/magento/framework/View/Element/AbstractBlock.php(272): MagentoLayeredNavigationBlockNavigation->_prepareLayout()
#6 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Generator/Block.php(150): MagentoFrameworkViewElementAbstractBlock->setLayout(Object(MagentoFrameworkViewLayoutInterceptor))
#7 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/GeneratorPool.php(80): MagentoFrameworkViewLayoutGeneratorBlock->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
#8 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout.php(343): MagentoFrameworkViewLayoutGeneratorPool->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
#9 /var/www/local.sandoro.com/generated/code/Magento/Framework/View/Layout/Interceptor.php(89): MagentoFrameworkViewLayout->generateElements()
#10 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(129): MagentoFrameworkViewLayoutInterceptor->generateElements()
#11 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Builder.php(55): MagentoFrameworkViewLayoutBuilder->generateLayoutBlocks()
#12 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(65): MagentoFrameworkViewPageBuilder->generateLayoutBlocks()
#13 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(208): MagentoFrameworkViewLayoutBuilder->build()
#14 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(544): MagentoFrameworkViewPageConfig->build()
#15 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(502): MagentoFrameworkViewPageConfig->getElementAttribute('body', 'class')
#16 /var/www/local.sandoro.com/vendor/magento/module-catalog/Controller/Category/View.php(202): MagentoFrameworkViewPageConfig->addBodyClass('page-products')
#17 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(24): MagentoCatalogControllerCategoryView->execute()
#18 /var/www/local.sandoro.com/vendor/magento/framework/App/Action/Action.php(107): MagentoCatalogControllerCategoryViewInterceptor->execute()
#19 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
#20 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoCatalogControllerCategoryViewInterceptor->___callParent('dispatch', Array)
#21 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoCatalogControllerCategoryViewInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#22 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(39): MagentoCatalogControllerCategoryViewInterceptor->___callPlugins('dispatch', Array, Array)
#23 /var/www/local.sandoro.com/vendor/magento/framework/App/FrontController.php(55): MagentoCatalogControllerCategoryViewInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#24 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
#25 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
#26 /var/www/local.sandoro.com/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#27 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoStoreAppFrontControllerPluginRequestPreprocessor->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#28 /var/www/local.sandoro.com/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#29 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoPageCacheModelAppFrontControllerBuiltinPlugin->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
#30 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
#31 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, NULL)
#32 /var/www/local.sandoro.com/vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#33 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
#34 /var/www/local.sandoro.com/vendor/magento/framework/App/Bootstrap.php(257): MagentoFrameworkAppHttpInterceptor->launch()
#35 /var/www/local.sandoro.com/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
#36 main


Anyone has an idea/solution?










share|improve this question




























    0















    currently i'm working on an extension which show the last products which sold the last 10 day. I got this products with the MagentoReportsModelResourceModelProductSoldCollection.



    Vender/Name/etc/di.xml:



    <type name="MagentoCatalogModelLayer">
    <plugin name="vendor_add_last_sold_products" type="VendorNamePluginLayer" />
    </type>


    Vender/Name/Plugin/Layer.php:



    namespace VendorNamePlugin;

    use MagentoCatalogModelResourceModelProductCollection;
    use MagentoCatalogModelResourceModelProductCollectionFactory;
    use MagentoCatalogModelProduct;
    use MagentoFrameworkRegistry;
    use VendorNameHelperOrders;

    class Layer

    protected $_orders;

    protected $_product;

    protected $_registry;

    public function __construct(
    Orders $orders,
    Product $product,
    Registry $registry
    )

    $this->_orders = $orders;
    $this->_product = $product;
    $this->_registry = $registry;


    public function aroundGetProductCollection(MagentoCatalogModelLayer $subject,Closure $proceed)

    $collection = $proceed();
    $category = $this->_registry->registry('current_category')->getId();
    $sold_products = $this->_orders->getLastSoldProducts($category);

    if($sold_products)
    foreach($sold_products as $product)
    if (!array_key_exists((int)$product->getId(), $collection->getItems()))
    $collection->addItem($this->_product->load((int)$product->getId()));




    return $collection;




    In $sold_products i saved all sold products. And with:



    if($sold_products) {
    foreach($sold_products as $product)
    if (!array_key_exists((int)$product->getId(), $collection->getItems()))
    $collection->addItem($this->_product->load((int)$product->getId()));




    I loop through all sold products and add them to the product collection. But i get always the following error:



    1 exception(s):
    Exception #0 (RuntimeException): Illegal state

    Exception #0 (RuntimeException): Illegal state
    #0 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(63): MagentoCatalogSearchModelResourceModelFulltextCollection->addFieldToFilter('category_ids', '37')
    #1 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php(458): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addFieldToFilter('category_ids', '37')
    #2 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(128): MagentoCatalogSearchModelResourceModelFulltextCollection->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
    #3 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php(74): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
    #4 /var/www/local.sandoro.com/vendor/magento/module-layered-navigation/Block/Navigation.php(67): MagentoCatalogSearchModelLayerFilterCategory->apply(Object(MagentoFrameworkAppRequestHttp))
    #5 /var/www/local.sandoro.com/vendor/magento/framework/View/Element/AbstractBlock.php(272): MagentoLayeredNavigationBlockNavigation->_prepareLayout()
    #6 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Generator/Block.php(150): MagentoFrameworkViewElementAbstractBlock->setLayout(Object(MagentoFrameworkViewLayoutInterceptor))
    #7 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/GeneratorPool.php(80): MagentoFrameworkViewLayoutGeneratorBlock->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
    #8 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout.php(343): MagentoFrameworkViewLayoutGeneratorPool->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
    #9 /var/www/local.sandoro.com/generated/code/Magento/Framework/View/Layout/Interceptor.php(89): MagentoFrameworkViewLayout->generateElements()
    #10 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(129): MagentoFrameworkViewLayoutInterceptor->generateElements()
    #11 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Builder.php(55): MagentoFrameworkViewLayoutBuilder->generateLayoutBlocks()
    #12 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(65): MagentoFrameworkViewPageBuilder->generateLayoutBlocks()
    #13 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(208): MagentoFrameworkViewLayoutBuilder->build()
    #14 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(544): MagentoFrameworkViewPageConfig->build()
    #15 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(502): MagentoFrameworkViewPageConfig->getElementAttribute('body', 'class')
    #16 /var/www/local.sandoro.com/vendor/magento/module-catalog/Controller/Category/View.php(202): MagentoFrameworkViewPageConfig->addBodyClass('page-products')
    #17 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(24): MagentoCatalogControllerCategoryView->execute()
    #18 /var/www/local.sandoro.com/vendor/magento/framework/App/Action/Action.php(107): MagentoCatalogControllerCategoryViewInterceptor->execute()
    #19 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
    #20 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoCatalogControllerCategoryViewInterceptor->___callParent('dispatch', Array)
    #21 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoCatalogControllerCategoryViewInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
    #22 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(39): MagentoCatalogControllerCategoryViewInterceptor->___callPlugins('dispatch', Array, Array)
    #23 /var/www/local.sandoro.com/vendor/magento/framework/App/FrontController.php(55): MagentoCatalogControllerCategoryViewInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
    #24 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
    #25 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
    #26 /var/www/local.sandoro.com/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
    #27 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoStoreAppFrontControllerPluginRequestPreprocessor->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
    #28 /var/www/local.sandoro.com/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
    #29 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoPageCacheModelAppFrontControllerBuiltinPlugin->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
    #30 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
    #31 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, NULL)
    #32 /var/www/local.sandoro.com/vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
    #33 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
    #34 /var/www/local.sandoro.com/vendor/magento/framework/App/Bootstrap.php(257): MagentoFrameworkAppHttpInterceptor->launch()
    #35 /var/www/local.sandoro.com/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
    #36 main


    Anyone has an idea/solution?










    share|improve this question
























      0












      0








      0








      currently i'm working on an extension which show the last products which sold the last 10 day. I got this products with the MagentoReportsModelResourceModelProductSoldCollection.



      Vender/Name/etc/di.xml:



      <type name="MagentoCatalogModelLayer">
      <plugin name="vendor_add_last_sold_products" type="VendorNamePluginLayer" />
      </type>


      Vender/Name/Plugin/Layer.php:



      namespace VendorNamePlugin;

      use MagentoCatalogModelResourceModelProductCollection;
      use MagentoCatalogModelResourceModelProductCollectionFactory;
      use MagentoCatalogModelProduct;
      use MagentoFrameworkRegistry;
      use VendorNameHelperOrders;

      class Layer

      protected $_orders;

      protected $_product;

      protected $_registry;

      public function __construct(
      Orders $orders,
      Product $product,
      Registry $registry
      )

      $this->_orders = $orders;
      $this->_product = $product;
      $this->_registry = $registry;


      public function aroundGetProductCollection(MagentoCatalogModelLayer $subject,Closure $proceed)

      $collection = $proceed();
      $category = $this->_registry->registry('current_category')->getId();
      $sold_products = $this->_orders->getLastSoldProducts($category);

      if($sold_products)
      foreach($sold_products as $product)
      if (!array_key_exists((int)$product->getId(), $collection->getItems()))
      $collection->addItem($this->_product->load((int)$product->getId()));




      return $collection;




      In $sold_products i saved all sold products. And with:



      if($sold_products) {
      foreach($sold_products as $product)
      if (!array_key_exists((int)$product->getId(), $collection->getItems()))
      $collection->addItem($this->_product->load((int)$product->getId()));




      I loop through all sold products and add them to the product collection. But i get always the following error:



      1 exception(s):
      Exception #0 (RuntimeException): Illegal state

      Exception #0 (RuntimeException): Illegal state
      #0 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(63): MagentoCatalogSearchModelResourceModelFulltextCollection->addFieldToFilter('category_ids', '37')
      #1 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php(458): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addFieldToFilter('category_ids', '37')
      #2 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(128): MagentoCatalogSearchModelResourceModelFulltextCollection->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
      #3 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php(74): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
      #4 /var/www/local.sandoro.com/vendor/magento/module-layered-navigation/Block/Navigation.php(67): MagentoCatalogSearchModelLayerFilterCategory->apply(Object(MagentoFrameworkAppRequestHttp))
      #5 /var/www/local.sandoro.com/vendor/magento/framework/View/Element/AbstractBlock.php(272): MagentoLayeredNavigationBlockNavigation->_prepareLayout()
      #6 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Generator/Block.php(150): MagentoFrameworkViewElementAbstractBlock->setLayout(Object(MagentoFrameworkViewLayoutInterceptor))
      #7 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/GeneratorPool.php(80): MagentoFrameworkViewLayoutGeneratorBlock->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
      #8 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout.php(343): MagentoFrameworkViewLayoutGeneratorPool->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
      #9 /var/www/local.sandoro.com/generated/code/Magento/Framework/View/Layout/Interceptor.php(89): MagentoFrameworkViewLayout->generateElements()
      #10 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(129): MagentoFrameworkViewLayoutInterceptor->generateElements()
      #11 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Builder.php(55): MagentoFrameworkViewLayoutBuilder->generateLayoutBlocks()
      #12 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(65): MagentoFrameworkViewPageBuilder->generateLayoutBlocks()
      #13 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(208): MagentoFrameworkViewLayoutBuilder->build()
      #14 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(544): MagentoFrameworkViewPageConfig->build()
      #15 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(502): MagentoFrameworkViewPageConfig->getElementAttribute('body', 'class')
      #16 /var/www/local.sandoro.com/vendor/magento/module-catalog/Controller/Category/View.php(202): MagentoFrameworkViewPageConfig->addBodyClass('page-products')
      #17 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(24): MagentoCatalogControllerCategoryView->execute()
      #18 /var/www/local.sandoro.com/vendor/magento/framework/App/Action/Action.php(107): MagentoCatalogControllerCategoryViewInterceptor->execute()
      #19 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #20 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoCatalogControllerCategoryViewInterceptor->___callParent('dispatch', Array)
      #21 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoCatalogControllerCategoryViewInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #22 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(39): MagentoCatalogControllerCategoryViewInterceptor->___callPlugins('dispatch', Array, Array)
      #23 /var/www/local.sandoro.com/vendor/magento/framework/App/FrontController.php(55): MagentoCatalogControllerCategoryViewInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #24 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #25 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
      #26 /var/www/local.sandoro.com/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #27 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoStoreAppFrontControllerPluginRequestPreprocessor->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
      #28 /var/www/local.sandoro.com/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #29 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoPageCacheModelAppFrontControllerBuiltinPlugin->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
      #30 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #31 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, NULL)
      #32 /var/www/local.sandoro.com/vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #33 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
      #34 /var/www/local.sandoro.com/vendor/magento/framework/App/Bootstrap.php(257): MagentoFrameworkAppHttpInterceptor->launch()
      #35 /var/www/local.sandoro.com/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
      #36 main


      Anyone has an idea/solution?










      share|improve this question














      currently i'm working on an extension which show the last products which sold the last 10 day. I got this products with the MagentoReportsModelResourceModelProductSoldCollection.



      Vender/Name/etc/di.xml:



      <type name="MagentoCatalogModelLayer">
      <plugin name="vendor_add_last_sold_products" type="VendorNamePluginLayer" />
      </type>


      Vender/Name/Plugin/Layer.php:



      namespace VendorNamePlugin;

      use MagentoCatalogModelResourceModelProductCollection;
      use MagentoCatalogModelResourceModelProductCollectionFactory;
      use MagentoCatalogModelProduct;
      use MagentoFrameworkRegistry;
      use VendorNameHelperOrders;

      class Layer

      protected $_orders;

      protected $_product;

      protected $_registry;

      public function __construct(
      Orders $orders,
      Product $product,
      Registry $registry
      )

      $this->_orders = $orders;
      $this->_product = $product;
      $this->_registry = $registry;


      public function aroundGetProductCollection(MagentoCatalogModelLayer $subject,Closure $proceed)

      $collection = $proceed();
      $category = $this->_registry->registry('current_category')->getId();
      $sold_products = $this->_orders->getLastSoldProducts($category);

      if($sold_products)
      foreach($sold_products as $product)
      if (!array_key_exists((int)$product->getId(), $collection->getItems()))
      $collection->addItem($this->_product->load((int)$product->getId()));




      return $collection;




      In $sold_products i saved all sold products. And with:



      if($sold_products) {
      foreach($sold_products as $product)
      if (!array_key_exists((int)$product->getId(), $collection->getItems()))
      $collection->addItem($this->_product->load((int)$product->getId()));




      I loop through all sold products and add them to the product collection. But i get always the following error:



      1 exception(s):
      Exception #0 (RuntimeException): Illegal state

      Exception #0 (RuntimeException): Illegal state
      #0 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(63): MagentoCatalogSearchModelResourceModelFulltextCollection->addFieldToFilter('category_ids', '37')
      #1 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php(458): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addFieldToFilter('category_ids', '37')
      #2 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(128): MagentoCatalogSearchModelResourceModelFulltextCollection->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
      #3 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php(74): MagentoCatalogSearchModelResourceModelFulltextCollectionInterceptor->addCategoryFilter(Object(MagentoCatalogModelCategoryInterceptor))
      #4 /var/www/local.sandoro.com/vendor/magento/module-layered-navigation/Block/Navigation.php(67): MagentoCatalogSearchModelLayerFilterCategory->apply(Object(MagentoFrameworkAppRequestHttp))
      #5 /var/www/local.sandoro.com/vendor/magento/framework/View/Element/AbstractBlock.php(272): MagentoLayeredNavigationBlockNavigation->_prepareLayout()
      #6 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Generator/Block.php(150): MagentoFrameworkViewElementAbstractBlock->setLayout(Object(MagentoFrameworkViewLayoutInterceptor))
      #7 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/GeneratorPool.php(80): MagentoFrameworkViewLayoutGeneratorBlock->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
      #8 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout.php(343): MagentoFrameworkViewLayoutGeneratorPool->process(Object(MagentoFrameworkViewLayoutReaderContext), Object(MagentoFrameworkViewLayoutGeneratorContext))
      #9 /var/www/local.sandoro.com/generated/code/Magento/Framework/View/Layout/Interceptor.php(89): MagentoFrameworkViewLayout->generateElements()
      #10 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(129): MagentoFrameworkViewLayoutInterceptor->generateElements()
      #11 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Builder.php(55): MagentoFrameworkViewLayoutBuilder->generateLayoutBlocks()
      #12 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(65): MagentoFrameworkViewPageBuilder->generateLayoutBlocks()
      #13 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(208): MagentoFrameworkViewLayoutBuilder->build()
      #14 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(544): MagentoFrameworkViewPageConfig->build()
      #15 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(502): MagentoFrameworkViewPageConfig->getElementAttribute('body', 'class')
      #16 /var/www/local.sandoro.com/vendor/magento/module-catalog/Controller/Category/View.php(202): MagentoFrameworkViewPageConfig->addBodyClass('page-products')
      #17 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(24): MagentoCatalogControllerCategoryView->execute()
      #18 /var/www/local.sandoro.com/vendor/magento/framework/App/Action/Action.php(107): MagentoCatalogControllerCategoryViewInterceptor->execute()
      #19 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppActionAction->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #20 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoCatalogControllerCategoryViewInterceptor->___callParent('dispatch', Array)
      #21 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoCatalogControllerCategoryViewInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #22 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(39): MagentoCatalogControllerCategoryViewInterceptor->___callPlugins('dispatch', Array, Array)
      #23 /var/www/local.sandoro.com/vendor/magento/framework/App/FrontController.php(55): MagentoCatalogControllerCategoryViewInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #24 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): MagentoFrameworkAppFrontController->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #25 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): MagentoFrameworkAppFrontControllerInterceptor->___callParent('dispatch', Array)
      #26 /var/www/local.sandoro.com/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #27 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoStoreAppFrontControllerPluginRequestPreprocessor->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
      #28 /var/www/local.sandoro.com/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #29 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): MagentoPageCacheModelAppFrontControllerBuiltinPlugin->aroundDispatch(Object(MagentoFrameworkAppFrontControllerInterceptor), Object(Closure), Object(MagentoFrameworkAppRequestHttp))
      #30 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): MagentoFrameworkAppFrontControllerInterceptor->MagentoFrameworkInterceptionclosure(Object(MagentoFrameworkAppRequestHttp))
      #31 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): MagentoFrameworkAppFrontControllerInterceptor->___callPlugins('dispatch', Array, NULL)
      #32 /var/www/local.sandoro.com/vendor/magento/framework/App/Http.php(135): MagentoFrameworkAppFrontControllerInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
      #33 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/Http/Interceptor.php(24): MagentoFrameworkAppHttp->launch()
      #34 /var/www/local.sandoro.com/vendor/magento/framework/App/Bootstrap.php(257): MagentoFrameworkAppHttpInterceptor->launch()
      #35 /var/www/local.sandoro.com/index.php(39): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttpInterceptor))
      #36 main


      Anyone has an idea/solution?







      magento2 product-collection






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      Maximilian SchwarzMaximilian Schwarz

      11




      11




















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270107%2fmagento-2-add-product-to-product-collection%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f270107%2fmagento-2-add-product-to-product-collection%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form