Skip to content

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions app/code/Magento/Catalog/Block/Product/ListProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,12 +69,18 @@ class ListProduct extends AbstractProduct implements IdentityInterface
*/
protected $categoryRepository;

/**
* @var CollectionLoader
*/
protected $collectionLoader;
Copy link
Contributor

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.


/**
* @param Context $context
* @param PostHelper $postDataHelper
* @param Resolver $layerResolver
* @param CategoryRepositoryInterface $categoryRepository
* @param Data $urlHelper
* @param CollectionLoader $collectionLoader
* @param array $data
*/
public function __construct(
Expand All @@ -82,12 +89,14 @@ public function __construct(
Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
Data $urlHelper,
CollectionLoader $collectionLoader,
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -188,7 +197,7 @@ protected function _beforeToHtml()

$this->addToolbarBlock($collection);

$collection->load();
$this->collectionLoader->load($collection);

return parent::_beforeToHtml();
}
Expand Down Expand Up @@ -463,7 +472,7 @@ private function initializeProductCollection()
if ($origCategory) {
$layer->setCurrentCategory($origCategory);
}

$this->addToolbarBlock($collection);

$this->_eventManager->dispatch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -38,6 +40,7 @@ public function __construct(
\Magento\Catalog\Model\Layer\Resolver $layerResolver,
CategoryRepositoryInterface $categoryRepository,
\Magento\Framework\Url\Helper\Data $urlHelper,
CollectionLoader $collectionLoader,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 = []
) {
Expand All @@ -48,6 +51,7 @@ public function __construct(
$layerResolver,
$categoryRepository,
$urlHelper,
$collectionLoader,
$data
);
}
Expand Down
4 changes: 4 additions & 0 deletions app/code/Magento/Catalog/Block/Product/ProductList/Random.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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 = []
) {
Expand All @@ -51,6 +54,7 @@ public function __construct(
$layerResolver,
$categoryRepository,
$urlHelper,
$collectionLoader,
Copy link
Contributor

Choose a reason for hiding this comment

The 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
);
}
Expand Down
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.

3 changes: 3 additions & 0 deletions app/code/Magento/Review/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\Product\ProductList\CollectionLoader">
<plugin name="ProductListCollectionLoader" type="Magento\Review\Model\Plugin\ProductList\CollectionLoader" sortOrder="1" />
</type>
</config>
3 changes: 0 additions & 3 deletions app/code/Magento/Review/etc/frontend/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
<event name="tag_tag_product_collection_load_after">
<observer name="review" instance="Magento\Review\Observer\TagProductCollectionLoadAfterObserver" shared="false" />
</event>
<event name="catalog_block_product_list_collection">
<observer name="review" instance="Magento\Review\Observer\CatalogBlockProductCollectionBeforeToHtmlObserver" shared="false" />
</event>
</config>