/** * 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; } }
/** * 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); }