123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\SalesRule\Model\Rule\Condition;
- /**
- * Product rule condition data model
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- class Product extends \Magento\Rule\Model\Condition\Product\AbstractProduct
- {
- /**
- * Add special attributes
- *
- * @param array $attributes
- * @return void
- */
- protected function _addSpecialAttributes(array &$attributes)
- {
- parent::_addSpecialAttributes($attributes);
- $attributes['quote_item_qty'] = __('Quantity in cart');
- $attributes['quote_item_price'] = __('Price in cart');
- $attributes['quote_item_row_total'] = __('Row total in cart');
- $attributes['parent::category_ids'] = __('Category (Parent only)');
- $attributes['children::category_ids'] = __('Category (Children Only)');
- }
- /**
- * Retrieve attribute
- *
- * @return string
- */
- public function getAttribute(): string
- {
- $attribute = $this->getData('attribute');
- if (strpos($attribute, '::') !== false) {
- list(, $attribute) = explode('::', $attribute);
- }
- return $attribute;
- }
- /**
- * @inheritdoc
- */
- public function getAttributeName()
- {
- $attribute = $this->getAttribute();
- if ($this->getAttributeScope()) {
- $attribute = $this->getAttributeScope() . '::' . $attribute;
- }
- return $this->getAttributeOption($attribute);
- }
- /**
- * @inheritdoc
- */
- public function loadAttributeOptions()
- {
- $productAttributes = $this->_productResource->loadAllAttributes()->getAttributesByCode();
- $attributes = [];
- foreach ($productAttributes as $attribute) {
- /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
- if (!$attribute->isAllowedForRuleCondition()
- || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty)
- ) {
- continue;
- }
- $frontLabel = $attribute->getFrontendLabel();
- $attributes[$attribute->getAttributeCode()] = $frontLabel;
- $attributes['parent::' . $attribute->getAttributeCode()] = $frontLabel . __('(Parent Only)');
- $attributes['children::' . $attribute->getAttributeCode()] = $frontLabel . __('(Children Only)');
- }
- $this->_addSpecialAttributes($attributes);
- asort($attributes);
- $this->setAttributeOption($attributes);
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function getAttributeElementHtml()
- {
- $html = parent::getAttributeElementHtml() .
- $this->getAttributeScopeElement()->getHtml();
- return $html;
- }
- /**
- * Retrieve form element for scope element
- *
- * @return \Magento\Framework\Data\Form\Element\AbstractElement
- */
- private function getAttributeScopeElement(): \Magento\Framework\Data\Form\Element\AbstractElement
- {
- return $this->getForm()->addField(
- $this->getPrefix() . '__' . $this->getId() . '__attribute_scope',
- 'hidden',
- [
- 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][attribute_scope]',
- 'value' => $this->getAttributeScope(),
- 'no_span' => true,
- 'class' => 'hidden',
- 'data-form-part' => $this->getFormName(),
- ]
- );
- }
- /**
- * Set attribute value
- *
- * @param string $value
- * @return void
- */
- public function setAttribute(string $value)
- {
- if (strpos($value, '::') !== false) {
- list($scope, $attribute) = explode('::', $value);
- $this->setData('attribute_scope', $scope);
- $this->setData('attribute', $attribute);
- } else {
- $this->setData('attribute', $value);
- }
- }
- /**
- * @inheritdoc
- */
- public function loadArray($arr)
- {
- parent::loadArray($arr);
- $this->setAttributeScope($arr['attribute_scope'] ?? null);
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function asArray(array $arrAttributes = [])
- {
- $out = parent::asArray($arrAttributes);
- $out['attribute_scope'] = $this->getAttributeScope();
- return $out;
- }
- /**
- * Validate Product Rule Condition
- *
- * @param \Magento\Framework\Model\AbstractModel $model
- *
- * @return bool
- * @throws \Magento\Framework\Exception\NoSuchEntityException
- */
- public function validate(\Magento\Framework\Model\AbstractModel $model)
- {
- //@todo reimplement this method when is fixed MAGETWO-5713
- /** @var \Magento\Catalog\Model\Product $product */
- $product = $model->getProduct();
- if (!$product instanceof \Magento\Catalog\Model\Product) {
- $product = $this->productRepository->getById($model->getProductId());
- }
- $product->setQuoteItemQty(
- $model->getQty()
- )->setQuoteItemPrice(
- $model->getPrice() // possible bug: need to use $model->getBasePrice()
- )->setQuoteItemRowTotal(
- $model->getBaseRowTotal()
- );
- $attrCode = $this->getAttribute();
- if ($attrCode === 'category_ids') {
- return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
- }
- if ($attrCode === 'quote_item_price') {
- $numericOperations = $this->getDefaultOperatorInputByType()['numeric'];
- if (in_array($this->getOperator(), $numericOperations)) {
- $this->setData('value', $this->getFormattedPrice($this->getValue()));
- }
- }
- return parent::validate($product);
- }
- /**
- * Retrieve value element chooser URL
- *
- * @return string
- */
- public function getValueElementChooserUrl()
- {
- $url = false;
- switch ($this->getAttribute()) {
- case 'sku':
- case 'category_ids':
- $url = 'sales_rule/promo_widget/chooser/attribute/' . $this->getAttribute();
- if ($this->getJsFormObject()) {
- $url .= '/form/' . $this->getJsFormObject();
- }
- break;
- default:
- break;
- }
- return $url !== false ? $this->_backendData->getUrl($url) : '';
- }
- /**
- * Get locale-based formatted price.
- *
- * @param string $value
- * @return float|null
- */
- private function getFormattedPrice($value)
- {
- $value = preg_replace('/[^0-9^\^.,-]/m', '', $value);
- /**
- * If the comma is the third symbol in the number, we consider it to be a decimal separator
- */
- $separatorComa = strpos($value, ',');
- $separatorDot = strpos($value, '.');
- if ($separatorComa !== false && $separatorDot === false && preg_match('/,\d{3}$/m', $value) === 1) {
- $value .= '.00';
- }
- return $this->_localeFormat->getNumber($value);
- }
- }
|