-
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.
Move 2.x version commits to correct branch
- Loading branch information
Showing
7 changed files
with
393 additions
and
5 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
...Resolver/Products/SearchCriteria/CollectionProcessor/FilterProcessor/ConditionsFilter.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,129 @@ | ||
<?php | ||
|
||
namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor; | ||
|
||
use Magento\CatalogWidget\Model\Rule; | ||
use Magento\Catalog\Model\Product\Attribute\Source\Status; | ||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\Framework\Api\Filter; | ||
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface; | ||
use Magento\Framework\Data\Collection\AbstractDb; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Rule\Model\Condition\Sql\Builder; | ||
use Magento\Widget\Helper\Conditions; | ||
|
||
class ConditionsFilter implements CustomFilterInterface | ||
{ | ||
/** | ||
* @var Conditions | ||
*/ | ||
protected $conditionsHelper; | ||
|
||
/** | ||
* @var Rule | ||
*/ | ||
protected $rule; | ||
|
||
/** | ||
* @var Builder | ||
*/ | ||
protected $sqlBuilder; | ||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* ConditionsFilter constructor. | ||
* @param Conditions $conditionsHelper | ||
* @param Rule $rule | ||
* @param Builder $sqlBuilder | ||
*/ | ||
public function __construct( | ||
Conditions $conditionsHelper, | ||
Rule $rule, | ||
Builder $sqlBuilder, | ||
CollectionFactory $collectionFactory | ||
) { | ||
$this->conditionsHelper = $conditionsHelper; | ||
$this->rule = $rule; | ||
$this->sqlBuilder = $sqlBuilder; | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* Get conditions | ||
* | ||
* @return \Magento\Rule\Model\Condition\Combine | ||
*/ | ||
protected function getConditions($conditions) | ||
{ | ||
$conditions = $this->conditionsHelper->decode($conditions); | ||
|
||
foreach ($conditions as $key => $condition) { | ||
if (!empty($condition['attribute']) | ||
&& in_array($condition['attribute'], ['special_from_date', 'special_to_date']) | ||
) { | ||
$conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value'])); | ||
} | ||
} | ||
|
||
$this->rule->loadPost(['conditions' => $conditions]); | ||
|
||
return $this->rule->getConditions(); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function apply(Filter $filter, AbstractDb $collection) | ||
{ | ||
$conditionType = $filter->getConditionType(); | ||
$rawFilterField = $filter->getField(); | ||
|
||
if ($conditionType !== 'eq') { | ||
throw new LocalizedException(__($rawFilterField . " only supports 'eq' condition type.")); | ||
} | ||
|
||
$conditions = base64_decode($filter->getValue()); | ||
$conditions = $this->getConditions($conditions); | ||
|
||
$simpleSelect = clone $collection; | ||
$conditions->collectValidatedAttributes($simpleSelect); | ||
$this->sqlBuilder->attachConditionToCollection($simpleSelect, $conditions); | ||
|
||
$simpleSelect->addFieldToFilter('status', Status::STATUS_ENABLED); | ||
$simpleSelect->getSelect() | ||
->reset(\Zend_Db_Select::COLUMNS) | ||
->columns(['e.entity_id']); | ||
|
||
$configurableProductCollection = $this->collectionFactory->create(); | ||
$select = $configurableProductCollection->getConnection() | ||
->select() | ||
->distinct() | ||
->from(['l' => 'catalog_product_super_link'], 'l.product_id') | ||
->join( | ||
['k' => $configurableProductCollection->getTable('catalog_product_entity')], | ||
`k.entity_id = l.parent_id` | ||
) | ||
->where($configurableProductCollection->getConnection()->prepareSqlCondition( | ||
'l.product_id', | ||
['in' => $simpleSelect->getSelect()] | ||
)) | ||
->reset(\Zend_Db_Select::COLUMNS) | ||
->columns(['l.parent_id']); | ||
|
||
$unionCollection = $this->collectionFactory->create(); | ||
$unionSelect = $unionCollection->getConnection() | ||
->select() | ||
->union([$simpleSelect->getSelect(), $select]); | ||
|
||
$collection->getSelect() | ||
->where($collection->getConnection()->prepareSqlCondition( | ||
'e.entity_id', | ||
['in' => $unionSelect] | ||
)); | ||
|
||
return true; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace ScandiPWA\CatalogGraphQl\Model\Rule\Condition; | ||
|
||
use Magento\CatalogWidget\Model\Rule\Condition\Product as ProductCondition; | ||
use Magento\Catalog\Model\ResourceModel\Eav\Attribute; | ||
use Magento\Catalog\Model\ResourceModel\Product\Collection; | ||
|
||
/** | ||
* Class Product | ||
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) | ||
*/ | ||
class Product extends ProductCondition | ||
{ | ||
/** | ||
* @param Attribute $attribute | ||
* @param Collection $collection | ||
* @return $this | ||
*/ | ||
protected function addGlobalAttribute( | ||
Attribute $attribute, | ||
Collection $collection | ||
) { | ||
switch ($attribute->getBackendType()) { | ||
case 'decimal': | ||
case 'datetime': | ||
case 'int': | ||
$alias = 'at_' . $attribute->getAttributeCode(); | ||
$collection->addAttributeToSelect($attribute->getAttributeCode(), 'left'); | ||
break; | ||
default: | ||
$alias = 'at_' . sha1($this->getId()) . $attribute->getAttributeCode(); | ||
|
||
$connection = $this->_productResource->getConnection(); | ||
$storeId = $connection->getIfNullSql($alias . '.store_id', $this->storeManager->getStore()->getId()); | ||
$linkField = $attribute->getEntity()->getLinkField(); | ||
|
||
$collection->getSelect()->join( | ||
[$alias => $collection->getTable('catalog_product_entity_varchar')], | ||
"($alias.$linkField = e.$linkField) AND ($alias.store_id = $storeId)" . | ||
" AND ($alias.attribute_id = {$attribute->getId()})", | ||
[] | ||
); | ||
} | ||
|
||
$this->joinedAttributes[$attribute->getAttributeCode()] = $alias . '.value'; | ||
|
||
return $this; | ||
} | ||
} |
Oops, something went wrong.