currently i'm working on an extension which show the last products which sold the last 10 day. I got this products with the \Magento\Reports\Model\ResourceModel\Product\Sold\Collection.
Vender/Name/etc/di.xml:
<type name="Magento\Catalog\Model\Layer">
<plugin name="vendor_add_last_sold_products" type="Vendor\Name\Plugin\Layer" />
</type>
Vender/Name/Plugin/Layer.php:
namespace Vendor\Name\Plugin;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\Product;
use Magento\Framework\Registry;
use \Vendor\Name\Helper\Orders;
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(\Magento\Catalog\Model\Layer $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): Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection->addFieldToFilter('category_ids', '37')
#1 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/ResourceModel/Fulltext/Collection.php(458): Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\Interceptor->addFieldToFilter('category_ids', '37')
#2 /var/www/local.sandoro.com/generated/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection/Interceptor.php(128): Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection->addCategoryFilter(Object(Magento\Catalog\Model\Category\Interceptor))
#3 /var/www/local.sandoro.com/vendor/magento/module-catalog-search/Model/Layer/Filter/Category.php(74): Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\Interceptor->addCategoryFilter(Object(Magento\Catalog\Model\Category\Interceptor))
#4 /var/www/local.sandoro.com/vendor/magento/module-layered-navigation/Block/Navigation.php(67): Magento\CatalogSearch\Model\Layer\Filter\Category->apply(Object(Magento\Framework\App\Request\Http))
#5 /var/www/local.sandoro.com/vendor/magento/framework/View/Element/AbstractBlock.php(272): Magento\LayeredNavigation\Block\Navigation->_prepareLayout()
#6 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Generator/Block.php(150): Magento\Framework\View\Element\AbstractBlock->setLayout(Object(Magento\Framework\View\Layout\Interceptor))
#7 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/GeneratorPool.php(80): Magento\Framework\View\Layout\Generator\Block->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#8 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout.php(343): Magento\Framework\View\Layout\GeneratorPool->process(Object(Magento\Framework\View\Layout\Reader\Context), Object(Magento\Framework\View\Layout\Generator\Context))
#9 /var/www/local.sandoro.com/generated/code/Magento/Framework/View/Layout/Interceptor.php(89): Magento\Framework\View\Layout->generateElements()
#10 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(129): Magento\Framework\View\Layout\Interceptor->generateElements()
#11 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Builder.php(55): Magento\Framework\View\Layout\Builder->generateLayoutBlocks()
#12 /var/www/local.sandoro.com/vendor/magento/framework/View/Layout/Builder.php(65): Magento\Framework\View\Page\Builder->generateLayoutBlocks()
#13 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(208): Magento\Framework\View\Layout\Builder->build()
#14 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(544): Magento\Framework\View\Page\Config->build()
#15 /var/www/local.sandoro.com/vendor/magento/framework/View/Page/Config.php(502): Magento\Framework\View\Page\Config->getElementAttribute('body', 'class')
#16 /var/www/local.sandoro.com/vendor/magento/module-catalog/Controller/Category/View.php(202): Magento\Framework\View\Page\Config->addBodyClass('page-products')
#17 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(24): Magento\Catalog\Controller\Category\View->execute()
#18 /var/www/local.sandoro.com/vendor/magento/framework/App/Action/Action.php(107): Magento\Catalog\Controller\Category\View\Interceptor->execute()
#19 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))
#20 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Catalog\Controller\Category\View\Interceptor->___callParent('dispatch', Array)
#21 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Catalog\Controller\Category\View\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#22 /var/www/local.sandoro.com/generated/code/Magento/Catalog/Controller/Category/View/Interceptor.php(39): Magento\Catalog\Controller\Category\View\Interceptor->___callPlugins('dispatch', Array, Array)
#23 /var/www/local.sandoro.com/vendor/magento/framework/App/FrontController.php(55): Magento\Catalog\Controller\Category\View\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#24 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\FrontController->dispatch(Object(Magento\Framework\App\Request\Http))
#25 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Framework\App\FrontController\Interceptor->___callParent('dispatch', Array)
#26 /var/www/local.sandoro.com/vendor/magento/module-store/App/FrontController/Plugin/RequestPreprocessor.php(94): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#27 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): Magento\Store\App\FrontController\Plugin\RequestPreprocessor->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#28 /var/www/local.sandoro.com/vendor/magento/module-page-cache/Model/App/FrontController/BuiltinPlugin.php(69): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#29 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(135): Magento\PageCache\Model\App\FrontController\BuiltinPlugin->aroundDispatch(Object(Magento\Framework\App\FrontController\Interceptor), Object(Closure), Object(Magento\Framework\App\Request\Http))
#30 /var/www/local.sandoro.com/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Framework\App\FrontController\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#31 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/FrontController/Interceptor.php(26): Magento\Framework\App\FrontController\Interceptor->___callPlugins('dispatch', Array, NULL)
#32 /var/www/local.sandoro.com/vendor/magento/framework/App/Http.php(135): Magento\Framework\App\FrontController\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#33 /var/www/local.sandoro.com/generated/code/Magento/Framework/App/Http/Interceptor.php(24): Magento\Framework\App\Http->launch()
#34 /var/www/local.sandoro.com/vendor/magento/framework/App/Bootstrap.php(257): Magento\Framework\App\Http\Interceptor->launch()
#35 /var/www/local.sandoro.com/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#36 {main}
Anyone has an idea/solution?