/** * Validate product attribute value for condition * * @param \Magento\Framework\Object $object * @return bool */ public function validate(\Magento\Framework\Object $object) { $attrCode = $this->getAttribute(); if ('category_ids' == $attrCode) { return $this->validateAttribute($object->getAvailableInCategories()); } elseif (!isset($this->_entityAttributeValues[$object->getId()])) { if (!$object->getResource()) { return false; } $attr = $object->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime' && !is_int($this->getValue())) { $this->setValue(strtotime($this->getValue())); $value = strtotime($object->getData($attrCode)); return $this->validateAttribute($value); } if ($attr && $attr->getFrontendInput() == 'multiselect') { $value = $object->getData($attrCode); $value = strlen($value) ? explode(',', $value) : array(); return $this->validateAttribute($value); } return parent::validate($object); } else { $result = false; // any valid value will set it to TRUE // remember old attribute state $oldAttrValue = $object->hasData($attrCode) ? $object->getData($attrCode) : null; foreach ($this->_entityAttributeValues[$object->getId()] as $value) { $attr = $object->getResource()->getAttribute($attrCode); if ($attr && $attr->getBackendType() == 'datetime') { $value = strtotime($value); } elseif ($attr && $attr->getFrontendInput() == 'multiselect') { $value = strlen($value) ? explode(',', $value) : array(); } $object->setData($attrCode, $value); $result |= parent::validate($object); if ($result) { break; } } if (is_null($oldAttrValue)) { $object->unsetData($attrCode); } else { $object->setData($attrCode, $oldAttrValue); } return (bool) $result; } }
/** * Get field by attribute * * @return string */ public function getMappedSqlField() { return $this->getAttribute() == 'category_ids' ? 'e.entity_id' : parent::getMappedSqlField(); }
/** * Handles addition of form name to condition and its conditions. * * @param \Magento\Rule\Model\Condition\AbstractCondition $conditions * @param string $formName * @return void */ private function setConditionFormName(\Magento\Rule\Model\Condition\AbstractCondition $conditions, $formName) { $conditions->setFormName($formName); if ($conditions->getConditions() && is_array($conditions->getConditions())) { foreach ($conditions->getConditions() as $condition) { $this->setConditionFormName($condition, $formName); } } }
/** * Validate Address Rule Condition * * @param \Magento\Framework\Model\AbstractModel $model * @return bool */ public function validate(\Magento\Framework\Model\AbstractModel $model) { $address = $model; if (!$address instanceof \Magento\Quote\Model\Quote\Address) { if ($model->getQuote()->isVirtual()) { $address = $model->getQuote()->getBillingAddress(); } else { $address = $model->getQuote()->getShippingAddress(); } } if ('payment_method' == $this->getAttribute() && !$address->hasPaymentMethod()) { $address->setPaymentMethod($model->getQuote()->getPayment()->getMethod()); } return parent::validate($address); }
/** * Get mapped sql field * * @return string */ public function getMappedSqlField() { if (!$this->isAttributeSetOrCategory()) { $mappedSqlField = $this->getEavAttributeTableAlias() . '.value'; } elseif ($this->getAttribute() == 'category_ids') { $mappedSqlField = 'e.entity_id'; } else { $mappedSqlField = parent::getMappedSqlField(); } return $mappedSqlField; }
/** * @param AbstractCondition $condition * @param string $value * @return string * @throws \Magento\Framework\Exception */ protected function _getMappedSqlCondition(AbstractCondition $condition, $value = '') { $argument = $condition->getMappedSqlField(); if ($argument) { $conditionOperator = $condition->getOperatorForValidate(); if (!isset($this->_conditionOperatorMap[$conditionOperator])) { throw new \Magento\Framework\Exception('Unknown condition operator'); } $sql = str_replace(':field', $this->_connection->getIfNullSql($this->_connection->quoteIdentifier($argument), 0), $this->_conditionOperatorMap[$conditionOperator]); return $this->_expressionFactory->create(['expression' => $value . $this->_connection->quoteInto($sql, $condition->getBindArgumentValue())]); } return ''; }