Skip to content

Commit

Permalink
Fixed price groups, fixed scandipwa/scandipwa#887
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredsgenkins committed Aug 27, 2020
1 parent 3b38e0d commit dacf941
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/Model/Context/AddSearchCriteriaToContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ class AddSearchCriteriaToContext implements ContextParametersProcessorInterface
public function execute(
ContextParametersInterface $contextParameters
) : ContextParametersInterface {

$contextParameters->addExtensionAttribute('search_criteria', null);

return $contextParameters;
}
}
2 changes: 1 addition & 1 deletion src/Model/Resolver/Product/PriceRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\CatalogGraphQl\Model\Resolver\Product\Price\Discount;
use Magento\CatalogGraphQl\Model\Resolver\Product\Price\ProviderPool as PriceProviderPool;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -68,7 +69,6 @@ public function resolve(

/** @var Product $product */
$product = $value['model'];
// literally single line got removed here

$requestedFields = $info->getFieldSelection(10);
$returnArray = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
class PriceProcessor implements CollectionProcessorInterface
{
const PRICE_FIELD = 'price_range';
const FLAG_CUSTOMER_GROUP_PRICE_ADDED = 'customer_group_price_data_added';

/**
* {@inheritdoc}
Expand All @@ -29,15 +28,9 @@ public function process(
SearchCriteriaInterface $searchCriteria,
array $attributeNames
): Collection {
$isPriceDataAdded = $collection->getFlag(self::FLAG_CUSTOMER_GROUP_PRICE_ADDED);

// add tax percent, no-matter what
$collection->addTaxPercents();

if ($isPriceDataAdded) {
return $collection;
}

if (in_array(self::PRICE_FIELD, $attributeNames, true)) {
/** @var $collection Collection */
$collection->addPriceData();
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Resolver/Products/Query/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\Query;

use Exception;
use Magento\CatalogGraphQl\DataProvider\Product\SearchCriteriaBuilder;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ProductSearch;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
Expand Down Expand Up @@ -112,7 +113,7 @@ public function __construct(
* @param array $args
* @param ResolveInfo $info
* @return SearchResult
* @throws \Exception
* @throws Exception
*/
public function getResult(
array $args,
Expand Down
24 changes: 1 addition & 23 deletions src/Model/Variant/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,31 +254,9 @@ protected function getChildCollectionMapAndList(): array {

protected function getSearchCriteria(array $childrenIds): SearchCriteriaInterface {
// build a search criteria based on original one and filter of product ids
$searchCriteria = $this->searchCriteriaBuilder
return $this->searchCriteriaBuilder
->addFilter('entity_id', $childrenIds, 'in')
->create();

$isSingleProduct = CriteriaCheck::isSingleProductFilter($this->searchCriteria);

$customFilterGroups = $searchCriteria->getFilterGroups();
$originalFilterGroups = $this->searchCriteria->getFilterGroups();

if (!$isSingleProduct) {
$filterGroups = array_merge($customFilterGroups, $originalFilterGroups);
} else {
// special case for customer group price - it is needed to be added to filter
foreach ($originalFilterGroups as $filterGroup) {
foreach ($filterGroup->getFilters() as $filter) {
if ($filter->getField() === 'customer_group_id') {
$filterGroups = array_merge($customFilterGroups, [$filterGroup]);
}
}
}
}

$searchCriteria->setFilterGroups($filterGroups ?? $customFilterGroups);

return $searchCriteria;
}

/**
Expand Down
68 changes: 68 additions & 0 deletions src/Plugin/Resolver/Products.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* ScandiPWA_CatalogGraphQl
*
* @category ScandiPWA
* @package ScandiPWA_CatalogGraphQl
* @author Alfreds Genkins <[email protected]>
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com)
*/

declare(strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Plugin\Resolver;

use Magento\CatalogGraphQl\Model\Resolver\Products as CoreProducts;
use Magento\Customer\Model\Session;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Argument\SearchCriteria\Builder;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

class Products {
/** @var Builder */
protected $searchCriteriaBuilder;

/** @var Session */
protected $customerSession;

/**
* Products constructor.
* @param Builder $searchCriteriaBuilder
* @param Session $customerSession
*/
public function __construct(
Builder $searchCriteriaBuilder,
Session $customerSession
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->customerSession = $customerSession;
}

public function beforeResolve(
CoreProducts $products,
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
$searchCriteria = $this->searchCriteriaBuilder->build('products', $args);
$context->getExtensionAttributes()->setSearchCriteria($searchCriteria);

foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
foreach ($filterGroup->getFilters() as $filter) {
if ($filter->getField() === 'customer_group_id') {
$this->customerSession->setCustomerGroupId($filter->getValue());
}
}
}

return [
$field,
$context,
$info,
$value,
$args
];
}
}
9 changes: 9 additions & 0 deletions src/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
/>
</type>

<type name="Magento\CatalogGraphQl\Model\Resolver\Products">
<plugin
name="add_search_criteria_to_context"
type="ScandiPWA\CatalogGraphQl\Plugin\Resolver\Products"
sortOrder="1"
disabled="false"
/>
</type>

<type name="Magento\GraphQl\Model\Query\ContextFactory">
<arguments>
<argument name="contextParametersProcessors" xsi:type="array">
Expand Down

0 comments on commit dacf941

Please sign in to comment.