-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Fix/imposible customize product collection #12770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d3e9b9c
2f6e8b1
08bef0e
dde4003
929027f
45fee81
b748dcc
8d43d6e
f598630
157297d
5fdf396
e91f041
ca4eddd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Magento\Catalog\Model\Layer\Resolver; | ||
use Magento\Catalog\Model\Product; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Catalog\Model\Product\ProductList\CollectionLoader; | ||
use Magento\Catalog\Pricing\Price\FinalPrice; | ||
use Magento\Eav\Model\Entity\Collection\AbstractCollection; | ||
use Magento\Framework\App\ActionInterface; | ||
|
@@ -68,12 +69,18 @@ class ListProduct extends AbstractProduct implements IdentityInterface | |
*/ | ||
protected $categoryRepository; | ||
|
||
/** | ||
* @var CollectionLoader | ||
*/ | ||
protected $collectionLoader; | ||
|
||
/** | ||
* @param Context $context | ||
* @param PostHelper $postDataHelper | ||
* @param Resolver $layerResolver | ||
* @param CategoryRepositoryInterface $categoryRepository | ||
* @param Data $urlHelper | ||
* @param CollectionLoader $collectionLoader | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
|
@@ -82,12 +89,14 @@ public function __construct( | |
Resolver $layerResolver, | ||
CategoryRepositoryInterface $categoryRepository, | ||
Data $urlHelper, | ||
CollectionLoader $collectionLoader, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New injections should come last in the construct and should also default to null so as to not break backwards compatibility. More information and examples can be found at http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/index.html#adding-a-constructor-parameter |
||
array $data = [] | ||
) { | ||
$this->_catalogLayer = $layerResolver->get(); | ||
$this->_postDataHelper = $postDataHelper; | ||
$this->categoryRepository = $categoryRepository; | ||
$this->urlHelper = $urlHelper; | ||
$this->collectionLoader = $collectionLoader; | ||
parent::__construct( | ||
$context, | ||
$data | ||
|
@@ -188,7 +197,7 @@ protected function _beforeToHtml() | |
|
||
$this->addToolbarBlock($collection); | ||
|
||
$collection->load(); | ||
$this->collectionLoader->load($collection); | ||
|
||
return parent::_beforeToHtml(); | ||
} | ||
|
@@ -463,7 +472,7 @@ private function initializeProductCollection() | |
if ($origCategory) { | ||
$layer->setCurrentCategory($origCategory); | ||
} | ||
|
||
$this->addToolbarBlock($collection); | ||
|
||
$this->_eventManager->dispatch( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Catalog\Model\Product\ProductList\CollectionLoader; | ||
|
||
class Promotion extends \Magento\Catalog\Block\Product\ListProduct | ||
{ | ||
|
@@ -29,6 +30,7 @@ class Promotion extends \Magento\Catalog\Block\Product\ListProduct | |
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver | ||
* @param CategoryRepositoryInterface $categoryRepository | ||
* @param \Magento\Framework\Url\Helper\Data $urlHelper | ||
* @param CollectionLoader $collectionLoader | ||
* @param CollectionFactory $productCollectionFactory | ||
* @param array $data | ||
*/ | ||
|
@@ -38,6 +40,7 @@ public function __construct( | |
\Magento\Catalog\Model\Layer\Resolver $layerResolver, | ||
CategoryRepositoryInterface $categoryRepository, | ||
\Magento\Framework\Url\Helper\Data $urlHelper, | ||
CollectionLoader $collectionLoader, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New injections should come last in the construct and should also default to null so as to not break backwards compatibility. More information and examples can be found at http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/index.html#adding-a-constructor-parameter |
||
CollectionFactory $productCollectionFactory, | ||
array $data = [] | ||
) { | ||
|
@@ -48,6 +51,7 @@ public function __construct( | |
$layerResolver, | ||
$categoryRepository, | ||
$urlHelper, | ||
$collectionLoader, | ||
$data | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
namespace Magento\Catalog\Block\Product\ProductList; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Catalog\Model\Product\ProductList\CollectionLoader; | ||
|
||
/** | ||
* Catalog product random items block | ||
|
@@ -32,6 +33,7 @@ class Random extends \Magento\Catalog\Block\Product\ListProduct | |
* @param \Magento\Catalog\Model\Layer\Resolver $layerResolver | ||
* @param CategoryRepositoryInterface $categoryRepository | ||
* @param \Magento\Framework\Url\Helper\Data $urlHelper | ||
* @param CollectionLoader $collectionLoader | ||
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory | ||
* @param array $data | ||
*/ | ||
|
@@ -41,6 +43,7 @@ public function __construct( | |
\Magento\Catalog\Model\Layer\Resolver $layerResolver, | ||
CategoryRepositoryInterface $categoryRepository, | ||
\Magento\Framework\Url\Helper\Data $urlHelper, | ||
CollectionLoader $collectionLoader, | ||
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, | ||
array $data = [] | ||
) { | ||
|
@@ -51,6 +54,7 @@ public function __construct( | |
$layerResolver, | ||
$categoryRepository, | ||
$urlHelper, | ||
$collectionLoader, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New injections should come last in the construct and should also default to null so as to not break backwards compatibility. More information and examples can be found at http://devdocs.magento.com/guides/v2.2/contributor-guide/backward-compatible-development/index.html#adding-a-constructor-parameter |
||
$data | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Model\Product\ProductList; | ||
|
||
use Magento\Eav\Model\Entity\Collection\AbstractCollection; | ||
|
||
/** | ||
* Class CollectionLoader | ||
* | ||
* @api | ||
* @since 100.0.2 | ||
*/ | ||
class CollectionLoader | ||
{ | ||
/** | ||
* @param AbstractCollection $collection | ||
* | ||
* @return AbstractCollection | ||
*/ | ||
public function load($collection) | ||
{ | ||
$collection->load(); | ||
return $collection; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Catalog\Test\Unit\Model\Product\ProductList; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use \Magento\Catalog\Model\Product\ProductList\CollectionLoader; | ||
|
||
class CollectionLoaderTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function testGetOrder() | ||
{ | ||
$collectionLoader = new CollectionLoader(); | ||
$mockedCollection = $this->getMockBuilder(\Magento\Catalog\Model\ResourceModel\Product\Collection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$mockedCollection->expects($this->once())->method('load'); | ||
|
||
$collectionLoader->load($mockedCollection); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Review\Model\Plugin\ProductList; | ||
|
||
use Magento\Eav\Model\Entity\Collection\AbstractCollection; | ||
use Magento\Catalog\Model\Product\ProductList\CollectionLoader as Loader; | ||
use Magento\Review\Model\ReviewFactory; | ||
|
||
/** | ||
* Class CollectionLoader | ||
* | ||
* @api | ||
* @since 100.0.2 | ||
*/ | ||
class CollectionLoader | ||
{ | ||
/** | ||
* Review model | ||
* | ||
* @var ReviewFactory | ||
*/ | ||
protected $reviewFactory; | ||
|
||
/** | ||
* @param ReviewFactory $reviewFactory | ||
*/ | ||
public function __construct(ReviewFactory $reviewFactory) | ||
{ | ||
$this->reviewFactory = $reviewFactory; | ||
} | ||
|
||
/** | ||
* Append review summary before rendering html | ||
* | ||
* @param Loader $subject | ||
* @param AbstractCollection $result | ||
* @return AbstractCollection | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function afterLoad( | ||
Loader $subject, | ||
AbstractCollection $result | ||
) { | ||
$this->reviewFactory->create()->appendSummary($result); | ||
return $result; | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New class parameter should be private as we are trying to avoid using protected.