-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop loading products all over again each time we request attributes. (…
…#24) * Reworked attribbutes and rating fields as they were hyper ineeficent each loading data on its own. * Fixed PHPDOC * Fixed entries getting lost in schema * Reworked to use countinue * Fied CR
- Loading branch information
1 parent
3750a6f
commit 9d298d4
Showing
4 changed files
with
136 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/Model/Resolver/Products/DataProvider/Product/CollectionProcessor/AttributeProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface; | ||
use Magento\Framework\Api\SearchCriteriaInterface; | ||
use Magento\Store\Model\StoreManagerInterface; | ||
|
||
/** | ||
* Adds passed in attributes to product collection results | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
class AttributeProcessor implements CollectionProcessorInterface | ||
{ | ||
const ATTRIBUTES_FIELD = 'attributes'; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
protected $collectionFactory; | ||
|
||
/** | ||
* @var StoreManagerInterface | ||
*/ | ||
protected $storeManager; | ||
|
||
/** | ||
* FilterableAttributeList constructor | ||
* | ||
* @param CollectionFactory $collectionFactory | ||
* @param StoreManagerInterface $storeManager | ||
*/ | ||
public function __construct( | ||
CollectionFactory $collectionFactory, | ||
StoreManagerInterface $storeManager | ||
) { | ||
$this->collectionFactory = $collectionFactory; | ||
$this->storeManager = $storeManager; | ||
} | ||
|
||
protected function getAttributesVisibleOnFrontend() { | ||
$collection = $this->collectionFactory->create(); | ||
$collection->setItemObjectClass(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class) | ||
->addStoreLabel($this->storeManager->getStore()->getId()) | ||
->setOrder('position', 'ASC'); | ||
|
||
// Add filter by storefront visibility | ||
$collection->addFieldToFilter('additional_table.is_visible_on_front', ['gt' => 0]); | ||
return $collection->load(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process( | ||
Collection $collection, | ||
SearchCriteriaInterface $searchCriteria, | ||
array $attributeNames | ||
): Collection { | ||
foreach ($attributeNames as $name) { | ||
if ($name !== self::ATTRIBUTES_FIELD) { | ||
$collection->addAttributeToSelect($name); | ||
continue; | ||
} | ||
|
||
$attributesVisibleOnFront = $this->getAttributesVisibleOnFrontend(); | ||
|
||
$attributeCodes = array_map(function($attr) { | ||
return $attr->getAttributeCode(); | ||
}, $attributesVisibleOnFront->getItems()); | ||
|
||
$collection->addAttributeToSelect($attributeCodes); | ||
} | ||
|
||
return $collection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters