9 use Magento\Catalog\Model\ProductFactory;
10 use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
11 use Magento\CatalogRule\Api\Data\RuleExtensionInterface;
17 use Magento\CatalogRule\Model\Rule\Action\CollectionFactory as RuleCollectionFactory;
18 use Magento\CatalogRule\Model\Rule\Condition\CombineFactory;
166 private $conditionsToCollectionApplier;
171 private $websitesMap;
176 private $ruleResourceModel;
213 CollectionFactory $productCollectionFactory,
215 CombineFactory $combineFactory,
216 RuleCollectionFactory $actionCollectionFactory,
220 Data $catalogRuleData,
226 array $relatedCacheTypes = [],
234 $this->_productCollectionFactory = $productCollectionFactory;
236 $this->_combineFactory = $combineFactory;
237 $this->_actionCollectionFactory = $actionCollectionFactory;
239 $this->_resourceIterator = $resourceIterator;
240 $this->_customerSession = $customerSession;
241 $this->_catalogRuleData = $catalogRuleData;
242 $this->_cacheTypesList = $cacheTypesList;
243 $this->_relatedCacheTypes = $relatedCacheTypes;
245 $this->_ruleProductProcessor = $ruleProductProcessor;
248 $this->conditionsToCollectionApplier = $conditionsToCollectionApplier
272 parent::_construct();
273 $this->
_init(RuleResourceModel::class);
284 return $this->_combineFactory->create();
294 return $this->_actionCollectionFactory->create();
304 if (!$this->hasCustomerGroupIds()) {
305 $customerGroupIds = $this->ruleResourceModel->getCustomerGroupIds($this->
getId());
306 $this->
setData(
'customer_group_ids', (array)$customerGroupIds);
308 return $this->
_getData(
'customer_group_ids');
341 public function getMatchingProductIds()
343 if ($this->_productIds ===
null) {
344 $this->_productIds = [];
345 $this->setCollectedAttributes([]);
351 if ($this->_productsFilter) {
356 if ($this->canPreMapProducts()) {
361 $this->_resourceIterator->walk(
363 [[$this,
'callbackValidateProduct']],
365 'attributes' => $this->getCollectedAttributes(),
366 'product' => $this->_productFactory->create()
380 private function canPreMapProducts()
385 if (!$conditions || !$conditions->getConditions()) {
407 $product->setStoreId($defaultStoreId);
420 if ($this->websitesMap ===
null) {
421 $this->websitesMap = [];
422 $websites = $this->_storeManager->getWebsites();
425 if (
$website->getDefaultStore() ===
null) {
428 $this->websitesMap[
$website->getId()] =
$website->getDefaultStore()->getId();
432 return $this->websitesMap;
440 $result = parent::validateData($dataObject);
445 $action = $dataObject->
getData(
'simple_action');
446 $discount = $dataObject->
getData(
'discount_amount');
466 if ($discount < 0 || $discount > 100) {
467 $result[] =
__(
'Percentage discount should be between 0 and 100.');
473 $result[] =
__(
'Discount value should be 0 or greater.');
496 if (
$product->hasCustomerGroupId()) {
497 $customerGroupId =
$product->getCustomerGroupId();
499 $customerGroupId = $this->_customerSession->getCustomerGroupId();
501 $dateTs = $this->_localeDate->scopeTimeStamp(
$storeId);
502 $cacheKey = date(
'Y-m-d', $dateTs) .
"|{$websiteId}|{$customerGroupId}|{$productId}|{$price}";
504 if (!array_key_exists($cacheKey, self::$_priceRulesData)) {
509 $priceRules = $priceRules ? $priceRules :
$price;
514 $priceRules = $this->_catalogRuleData->calcPriceRule(
517 $priceRules ? $priceRules :
$price 524 return self::$_priceRulesData[$cacheKey] = $priceRules;
526 self::$_priceRulesData[$cacheKey] =
null;
529 return self::$_priceRulesData[$cacheKey];
545 return $this->ruleResourceModel->getRulesFromProduct($dateTs,
$websiteId, $customerGroupId,
$productId);
578 if (count($this->_relatedCacheTypes)) {
579 $this->_cacheTypesList->invalidate($this->_relatedCacheTypes);
591 if ($this->
isObjectNew() && !$this->_ruleProductProcessor->isIndexerScheduled()) {
594 $this->ruleResourceModel->addCommitCallback([$this,
'reindex']);
597 $this->_ruleProductProcessor->getIndexer()->invalidate();
599 return parent::afterSave();
609 $productIds = $this->_productIds ? array_keys(array_filter($this->_productIds,
function (array
$data) {
610 return array_filter(
$data);
612 $this->_ruleProductProcessor->reindexList(
$productIds);
622 $this->_ruleProductProcessor->getIndexer()->invalidate();
623 return parent::afterDelete();
635 unset($arrayDiff[
'name']);
636 unset($arrayDiff[
'description']);
637 if (empty($arrayDiff)) {
655 if (array_key_exists($key,
$array2)) {
682 return $this->
getData(self::RULE_ID);
698 return $this->
getData(self::NAME);
714 return $this->
getData(self::DESCRIPTION);
730 return $this->
getData(self::IS_ACTIVE);
738 return $this->
setData(self::IS_ACTIVE, $isActive);
746 return $this->getRuleConditionConverter()->arrayToDataModel($this->
getConditions()->asArray());
756 ->loadArray($this->getRuleConditionConverter()->dataModelToArray($condition));
765 return $this->
getData(self::STOP_RULES_PROCESSING);
773 return $this->
setData(self::STOP_RULES_PROCESSING, $isStopProcessing);
781 return $this->
getData(self::SORT_ORDER);
789 return $this->
setData(self::SORT_ORDER, $sortOrder);
797 return $this->
getData(self::SIMPLE_ACTION);
805 return $this->
setData(self::SIMPLE_ACTION, $action);
813 return $this->
getData(self::DISCOUNT_AMOUNT);
829 return $this->
getData(
'from_date');
837 return $this->
getData(
'to_date');
865 private function getRuleConditionConverter()
867 if (
null === $this->ruleConditionConverter) {
869 ->get(Converter::class);
const DATETIME_PHP_FORMAT
setDescription($description)
_getExtensionAttributes()
getData($key='', $index=null)
validateData(DataObject $dataObject)
_setExtensionAttributes(\Magento\Framework\Api\ExtensionAttributesInterface $extensionAttributes)
setRuleCondition($condition)
getConditionsFieldSetId($formName='')
setStopRulesProcessing($isStopProcessing)
$_productCollectionFactory
callbackValidateProduct($args)
setProductsFilter($productIds)
setDiscountAmount($amount)
$_actionCollectionFactory
getData($key='', $index=null)
__construct(Context $context, Registry $registry, FormFactory $formFactory, TimezoneInterface $localeDate, CollectionFactory $productCollectionFactory, StoreManagerInterface $storeManager, CombineFactory $combineFactory, RuleCollectionFactory $actionCollectionFactory, ProductFactory $productFactory, Iterator $resourceIterator, Session $customerSession, Data $catalogRuleData, TypeListInterface $cacheTypesList, DateTime $dateTime, RuleProductProcessor $ruleProductProcessor, AbstractResource $resource=null, AbstractDb $resourceCollection=null, array $relatedCacheTypes=[], array $data=[], ExtensionAttributesFactory $extensionFactory=null, AttributeValueFactory $customAttributeFactory=null, Json $serializer=null, RuleResourceModel $ruleResourceModel=null, ConditionsToCollectionApplier $conditionsToCollectionApplier=null)
setData($key, $value=null)
validateDiscount($action, $discount)
setExtensionAttributes(RuleExtensionInterface $extensionAttributes)
calcProductPriceRule(Product $product, $price)
dataDiff($array1, $array2)
_getRulesFromProduct($dateTs, $websiteId, $customerGroupId, $productId)
if(!isset($_GET['name'])) $name