Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Product.php
Go to the documentation of this file.
1 <?php
8 
15 {
22  protected function _addSpecialAttributes(array &$attributes)
23  {
24  parent::_addSpecialAttributes($attributes);
25  $attributes['quote_item_qty'] = __('Quantity in cart');
26  $attributes['quote_item_price'] = __('Price in cart');
27  $attributes['quote_item_row_total'] = __('Row total in cart');
28 
29  $attributes['parent::category_ids'] = __('Category (Parent only)');
30  $attributes['children::category_ids'] = __('Category (Children Only)');
31  }
32 
38  public function getAttribute()
39  {
40  $attribute = $this->getData('attribute');
41  if (strpos($attribute, '::') !== false) {
42  list (, $attribute) = explode('::', $attribute);
43  }
44  return $attribute;
45  }
46 
50  public function getAttributeName()
51  {
52  $attribute = $this->getAttribute();
53  if ($this->getAttributeScope()) {
54  $attribute = $this->getAttributeScope() . '::' . $attribute;
55  }
56  return $this->getAttributeOption($attribute);
57  }
58 
62  public function loadAttributeOptions()
63  {
64  $productAttributes = $this->_productResource->loadAllAttributes()->getAttributesByCode();
65 
66  $attributes = [];
67  foreach ($productAttributes as $attribute) {
68  /* @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
69  if (!$attribute->isAllowedForRuleCondition()
70  || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty)
71  ) {
72  continue;
73  }
74  $frontLabel = $attribute->getFrontendLabel();
75  $attributes[$attribute->getAttributeCode()] = $frontLabel;
76  $attributes['parent::' . $attribute->getAttributeCode()] = $frontLabel . __('(Parent Only)');
77  $attributes['children::' . $attribute->getAttributeCode()] = $frontLabel . __('(Children Only)');
78  }
79 
81 
82  asort($attributes);
83  $this->setAttributeOption($attributes);
84 
85  return $this;
86  }
87 
91  public function getAttributeElementHtml()
92  {
93  $html = parent::getAttributeElementHtml() .
94  $this->getAttributeScopeElement()->getHtml();
95  return $html;
96  }
97 
103  private function getAttributeScopeElement()
104  {
105  return $this->getForm()->addField(
106  $this->getPrefix() . '__' . $this->getId() . '__attribute_scope',
107  'hidden',
108  [
109  'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][attribute_scope]',
110  'value' => $this->getAttributeScope(),
111  'no_span' => true,
112  'class' => 'hidden',
113  'data-form-part' => $this->getFormName()
114  ]
115  );
116  }
117 
123  public function setAttribute($value)
124  {
125  if (strpos($value, '::') !== false) {
126  list($scope, $attribute) = explode('::', $value);
127  $this->setData('attribute_scope', $scope);
128  $this->setData('attribute', $attribute);
129  } else {
130  $this->setData('attribute', $value);
131  }
132  }
133 
137  public function loadArray($arr)
138  {
139  parent::loadArray($arr);
140  $this->setAttributeScope(isset($arr['attribute_scope']) ? $arr['attribute_scope'] : null);
141  return $this;
142  }
143 
147  public function asArray(array $arrAttributes = [])
148  {
149  $out = parent::asArray($arrAttributes);
150  $out['attribute_scope'] = $this->getAttributeScope();
151  return $out;
152  }
153 
160  public function validate(\Magento\Framework\Model\AbstractModel $model)
161  {
162  //@todo reimplement this method when is fixed MAGETWO-5713
164  $product = $model->getProduct();
165  if (!$product instanceof \Magento\Catalog\Model\Product) {
166  $product = $this->productRepository->getById($model->getProductId());
167  }
168 
169  $product->setQuoteItemQty(
170  $model->getQty()
171  )->setQuoteItemPrice(
172  $model->getPrice() // possible bug: need to use $model->getBasePrice()
173  )->setQuoteItemRowTotal(
174  $model->getBaseRowTotal()
175  );
176 
177  $attrCode = $this->getAttribute();
178 
179  if ($attrCode === 'category_ids') {
180  return $this->validateAttribute($this->_getAvailableInCategories($product->getId()));
181  }
182 
183  if ($attrCode === 'quote_item_price') {
184  $numericOperations = $this->getDefaultOperatorInputByType()['numeric'];
185  if (in_array($this->getOperator(), $numericOperations)) {
186  $this->setData('value', $this->getFormattedPrice($this->getValue()));
187  }
188  }
189 
190  return parent::validate($product);
191  }
192 
198  public function getValueElementChooserUrl()
199  {
200  $url = false;
201  switch ($this->getAttribute()) {
202  case 'sku':
203  case 'category_ids':
204  $url = 'sales_rule/promo_widget/chooser/attribute/' . $this->getAttribute();
205  if ($this->getJsFormObject()) {
206  $url .= '/form/' . $this->getJsFormObject();
207  }
208  break;
209  default:
210  break;
211  }
212  return $url !== false ? $this->_backendData->getUrl($url) : '';
213  }
214 
221  private function getFormattedPrice($value)
222  {
223  $value = preg_replace('/[^0-9^\^.,-]/m', '', $value);
224 
228  $separatorComa = strpos($value, ',');
229  $separatorDot = strpos($value, '.');
230  if ($separatorComa !== false && $separatorDot === false && preg_match('/,\d{3}$/m', $value) === 1) {
231  $value .= '.00';
232  }
233  return $this->_localeFormat->getNumber($value);
234  }
235 }
getData($key='', $index=null)
Definition: DataObject.php:119
__()
Definition: __.php:13
validate(\Magento\Framework\Model\AbstractModel $model)
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13
setData($key, $value=null)
Definition: DataObject.php:72