|
6 | 6 | namespace Faonni\SmartCategory\Model\Rule\Condition; |
7 | 7 |
|
8 | 8 | use Magento\Framework\Model\AbstractModel; |
| 9 | +use Magento\Framework\Model\ResourceModel\IteratorFactory; |
| 10 | +use Magento\Framework\Locale\FormatInterface; |
| 11 | +use Magento\Backend\Helper\Data as Helper; |
| 12 | +use Magento\Rule\Model\Condition\Context; |
9 | 13 | use Magento\Rule\Model\Condition\Product\AbstractProduct; |
| 14 | +use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection as AttrSetCollection; |
| 15 | +use Magento\Eav\Model\Config; |
10 | 16 | use Magento\Store\Model\Store; |
| 17 | +use Magento\Catalog\Model\ProductFactory; |
| 18 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 19 | +use Magento\Catalog\Model\ResourceModel\Product as ProductResource; |
| 20 | +use Magento\Catalog\Model\ProductCategoryList; |
11 | 21 |
|
12 | 22 | /** |
13 | 23 | * Product condition |
|
16 | 26 | * @method getJsFormObject() |
17 | 27 | * @method getAttributeOption() |
18 | 28 | * @method getRule() |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
19 | 30 | */ |
20 | 31 | class Product extends AbstractProduct |
21 | 32 | { |
| 33 | + /** |
| 34 | + * @var IteratorFactory |
| 35 | + */ |
| 36 | + protected $iteratorFactory; |
| 37 | + |
22 | 38 | /** |
23 | 39 | * Attribute data key that indicates whether it should be used for rules |
24 | 40 | * |
25 | 41 | * @var string |
26 | 42 | */ |
27 | 43 | protected $_isUsedForRuleProperty = 'is_used_for_smart_rules'; |
28 | 44 |
|
| 45 | + /** |
| 46 | + * Initialize condition |
| 47 | + * |
| 48 | + * @param Context $context |
| 49 | + * @param Helper $backendData |
| 50 | + * @param Config $config |
| 51 | + * @param ProductFactory $productFactory |
| 52 | + * @param ProductRepositoryInterface $productRepository |
| 53 | + * @param ProductResource $productResource |
| 54 | + * @param AttrSetCollection $attrSetCollection |
| 55 | + * @param FormatInterface $localeFormat |
| 56 | + * @param IteratorFactory $iteratorFactory |
| 57 | + * @param mixed[] $data |
| 58 | + * @param ProductCategoryList|null $categoryList |
| 59 | + * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + Context $context, |
| 63 | + Helper $backendData, |
| 64 | + Config $config, |
| 65 | + ProductFactory $productFactory, |
| 66 | + ProductRepositoryInterface $productRepository, |
| 67 | + ProductResource $productResource, |
| 68 | + AttrSetCollection $attrSetCollection, |
| 69 | + FormatInterface $localeFormat, |
| 70 | + IteratorFactory $iteratorFactory, |
| 71 | + array $data = [], |
| 72 | + ProductCategoryList $categoryList = null |
| 73 | + ) { |
| 74 | + $this->iteratorFactory = $iteratorFactory; |
| 75 | + |
| 76 | + parent::__construct( |
| 77 | + $context, |
| 78 | + $backendData, |
| 79 | + $config, |
| 80 | + $productFactory, |
| 81 | + $productRepository, |
| 82 | + $productResource, |
| 83 | + $attrSetCollection, |
| 84 | + $localeFormat, |
| 85 | + $data, |
| 86 | + $categoryList |
| 87 | + ); |
| 88 | + } |
| 89 | + |
29 | 90 | /** |
30 | 91 | * Retrieve value element chooser URL |
31 | 92 | * |
@@ -106,14 +167,12 @@ protected function setAttributeValue(AbstractModel $model) |
106 | 167 | return $this; |
107 | 168 | } |
108 | 169 |
|
109 | | - $productValues = $this->_entityAttributeValues[$model->getId()]; |
110 | | - |
| 170 | + $productValues = $this->_entityAttributeValues[$model->getId()]; |
111 | 171 | if (!isset($productValues[$storeId]) && !isset($productValues[$defaultStoreId])) { |
112 | 172 | return $this; |
113 | 173 | } |
114 | 174 |
|
115 | | - $value = isset($productValues[$storeId]) ? $productValues[$storeId] : $productValues[$defaultStoreId]; |
116 | | - |
| 175 | + $value = $productValues[$storeId] ?? $productValues[$defaultStoreId]; |
117 | 176 | $value = $this->prepareDatetimeValue($value, $model); |
118 | 177 | $value = $this->prepareMultiselectValue($value, $model); |
119 | 178 |
|
@@ -157,4 +216,49 @@ protected function prepareMultiselectValue($value, AbstractModel $model) |
157 | 216 | } |
158 | 217 | return $value; |
159 | 218 | } |
| 219 | + |
| 220 | + /** |
| 221 | + * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection |
| 222 | + * |
| 223 | + * @return $this |
| 224 | + * @throws \Magento\Framework\Exception\LocalizedException |
| 225 | + * @SuppressWarnings(PHPMD.ElseExpression) |
| 226 | + */ |
| 227 | + public function collectValidatedAttributes($productCollection) |
| 228 | + { |
| 229 | + $attribute = $this->getAttribute(); |
| 230 | + if ('category_ids' != $attribute) { |
| 231 | + $productCollection->addAttributeToSelect($attribute, 'left'); |
| 232 | + if ($this->getAttributeObject()->isScopeGlobal()) { |
| 233 | + $attributes = $this->getRule()->getCollectedAttributes(); |
| 234 | + $attributes[$attribute] = true; |
| 235 | + $this->getRule()->setCollectedAttributes($attributes); |
| 236 | + } else { |
| 237 | + $select = clone $productCollection->getSelect(); |
| 238 | + $attributeModel = $productCollection->getEntity()->getAttribute($attribute); |
| 239 | + |
| 240 | + $fieldMainTable = $productCollection->getConnection()->getAutoIncrementField($productCollection->getMainTable()); |
| 241 | + $fieldJoinTable = $attributeModel->getEntity()->getLinkField(); |
| 242 | + $select->reset() |
| 243 | + ->from( |
| 244 | + ['cpe' => $productCollection->getMainTable()], |
| 245 | + ['entity_id'] |
| 246 | + )->join( |
| 247 | + ['cpa' => $attributeModel->getBackend()->getTable()], |
| 248 | + 'cpe.' . $fieldMainTable . ' = cpa.' . $fieldJoinTable, |
| 249 | + ['store_id', 'value'] |
| 250 | + )->where('attribute_id = ?', (int)$attributeModel->getId()); |
| 251 | + |
| 252 | + $iterator = $this->iteratorFactory->create(); |
| 253 | + $res = []; |
| 254 | + $iterator->walk((string)$select, [function (array $data) { |
| 255 | + $row = $data['row']; |
| 256 | + $res[$row['entity_id']][$row['store_id']] = $row['value']; |
| 257 | + }], [], $productCollection->getConnection()); |
| 258 | + $this->_entityAttributeValues = $res; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + return $this; |
| 263 | + } |
160 | 264 | } |
0 commit comments