Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
AbstractGroupPrice Class Reference
Inheritance diagram for AbstractGroupPrice:
Price AbstractBackend BackendInterface Tierprice

Public Member Functions

 __construct (\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Data $catalogData, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Locale\FormatInterface $localeFormat, \Magento\Catalog\Model\Product\Type $catalogProductType, GroupManagementInterface $groupManagement, ScopeOverriddenValue $scopeOverriddenValue=null)
 
 validate ($object)
 
 preparePriceData (array $priceData, $productTypeId, $websiteId)
 
 afterLoad ($object)
 
 setPriceData ($object, $priceData)
 
 afterSave ($object)
 
 getAffectedFields ($object)
 
 getResource ()
 
- Public Member Functions inherited from Price
 __construct (\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Helper\Data $catalogData, \Magento\Framework\App\Config\ScopeConfigInterface $config, \Magento\Framework\Locale\FormatInterface $localeFormat, ScopeOverriddenValue $scopeOverriddenValue=null)
 
 setAttribute ($attribute)
 
 setScope ($attribute)
 
 validate ($object)
 
- Public Member Functions inherited from AbstractBackend
 setAttribute ($attribute)
 
 getAttribute ()
 
 getType ()
 
 isStatic ()
 
 getTable ()
 
 getEntityIdField ()
 
 setValueId ($valueId)
 
 setEntityValueId ($entity, $valueId)
 
 getValueId ()
 
 getEntityValueId ($entity)
 
 getDefaultValue ()
 
 validate ($object)
 
 afterLoad ($object)
 
 beforeSave ($object)
 
 afterSave ($object)
 
 beforeDelete ($object)
 
 afterDelete ($object)
 
 getAffectedFields ($object)
 
 isScalar ()
 

Protected Member Functions

 _getDuplicateErrorMessage ()
 
 _getWebsiteCurrencyRates ()
 
 _getResource ()
 
 _getAdditionalUniqueFields ($objectArray)
 
 getAdditionalFields ($objectArray)
 
 _isPriceFixed ($priceObject)
 
 validatePrice (array $priceRow)
 
 updateValues (array $valuesToUpdate, array $oldValues)
 
- Protected Member Functions inherited from Price
 isPositiveOrZero ($value)
 

Protected Attributes

 $metadataPool
 
 $_rates
 
 $_catalogProductType
 
 $_groupManagement
 
- Protected Attributes inherited from Price
 $_helper
 
 $_storeManager
 
 $_currencyFactory
 
 $_config
 
 $localeFormat
 
- Protected Attributes inherited from AbstractBackend
 $_attribute
 
 $_valueId
 
 $_valueIds = []
 
 $_table
 
 $_entityIdField
 
 $_defaultValue = null
 

Detailed Description

Catalog product abstract group price backend attribute model

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 19 of file AbstractGroupPrice.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Directory\Model\CurrencyFactory  $currencyFactory,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Catalog\Helper\Data  $catalogData,
\Magento\Framework\App\Config\ScopeConfigInterface  $config,
\Magento\Framework\Locale\FormatInterface  $localeFormat,
\Magento\Catalog\Model\Product\Type  $catalogProductType,
GroupManagementInterface  $groupManagement,
ScopeOverriddenValue  $scopeOverriddenValue = null 
)
Parameters
\Magento\Directory\Model\CurrencyFactory$currencyFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Catalog\Helper\Data$catalogData
\Magento\Framework\App\Config\ScopeConfigInterface$config
\Magento\Framework\Locale\FormatInterface$localeFormat
\Magento\Catalog\Model\Product\Type$catalogProductType
GroupManagementInterface$groupManagement
ScopeOverriddenValue | null$scopeOverriddenValue

Definition at line 63 of file AbstractGroupPrice.php.

72  {
73  $this->_catalogProductType = $catalogProductType;
74  $this->_groupManagement = $groupManagement;
75  parent::__construct(
76  $currencyFactory,
78  $catalogData,
79  $config,
81  $scopeOverriddenValue
82  );
83  }
$config
Definition: fraud_order.php:17
$storeManager

Member Function Documentation

◆ _getAdditionalUniqueFields()

_getAdditionalUniqueFields (   $objectArray)
protected

Get additional unique fields

Parameters
array$objectArray
Returns
array @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 135 of file AbstractGroupPrice.php.

136  {
137  return [];
138  }

◆ _getDuplicateErrorMessage()

_getDuplicateErrorMessage ( )
abstractprotected

Error message when duplicates

@abstract

Returns
string

◆ _getResource()

_getResource ( )
abstractprotected

Retrieve resource instance

Returns
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\Tierprice

◆ _getWebsiteCurrencyRates()

_getWebsiteCurrencyRates ( )
protected

Retrieve websites currency rates and base currency codes

Returns
array

Definition at line 90 of file AbstractGroupPrice.php.

91  {
92  if ($this->_rates === null) {
93  $this->_rates = [];
94  $baseCurrency = $this->_config->getValue(
95  \Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE,
96  'default'
97  );
98  foreach ($this->_storeManager->getWebsites() as $website) {
99  /* @var $website \Magento\Store\Model\Website */
100  if ($website->getBaseCurrencyCode() != $baseCurrency) {
101  $rate = $this->_currencyFactory->create()->load(
102  $baseCurrency
103  )->getRate(
104  $website->getBaseCurrencyCode()
105  );
106  if (!$rate) {
107  $rate = 1;
108  }
109  $this->_rates[$website->getId()] = [
110  'code' => $website->getBaseCurrencyCode(),
111  'rate' => $rate,
112  ];
113  } else {
114  $this->_rates[$website->getId()] = ['code' => $baseCurrency, 'rate' => 1];
115  }
116  }
117  }
118  return $this->_rates;
119  }

◆ _isPriceFixed()

_isPriceFixed (   $priceObject)
protected

Whether group price value fixed or percent of original price

Parameters
\Magento\Catalog\Model\Product\Type\Price$priceObject
Returns
bool

Definition at line 158 of file AbstractGroupPrice.php.

159  {
160  return $priceObject->isGroupPriceFixed();
161  }

◆ afterLoad()

afterLoad (   $object)

Assign group prices to product data

Parameters
\Magento\Catalog\Model\Product$object
Returns
$this

Implements BackendInterface.

Definition at line 305 of file AbstractGroupPrice.php.

306  {
307  $data = $this->_getResource()->loadPriceData(
308  $object->getData($this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField()),
309  $this->getWebsiteId($object->getStoreId())
310  );
311  $this->setPriceData($object, $data);
312 
313  return $this;
314  }

◆ afterSave()

afterSave (   $object)

After Save Attribute manipulation

Parameters
\Magento\Catalog\Model\Product$object
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements BackendInterface.

Definition at line 384 of file AbstractGroupPrice.php.

385  {
386  return $this;
387  }

◆ getAdditionalFields()

getAdditionalFields (   $objectArray)
protected

Get additional fields

Parameters
array$objectArray
Returns
array @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 147 of file AbstractGroupPrice.php.

148  {
149  return [];
150  }

◆ getAffectedFields()

getAffectedFields (   $object)

Retrieve data for update attribute

Parameters
\Magento\Catalog\Model\Product$object
Returns
array

Definition at line 420 of file AbstractGroupPrice.php.

421  {
422  $data = [];
423  $prices = (array)$object->getData($this->getAttribute()->getName());
424  $tableName = $this->_getResource()->getMainTable();
425  foreach ($prices as $value) {
426  $data[$tableName][] = [
427  'attribute_id' => $this->getAttribute()->getAttributeId(),
428  'entity_id' => $object->getId(),
429  'value_id' => $value['price_id'],
430  ];
431  }
432 
433  return $data;
434  }
$tableName
Definition: trigger.php:13
$value
Definition: gender.phtml:16

◆ getResource()

getResource ( )

Get resource model instance

Returns
\Magento\Catalog\Model\ResourceModel\Product\Attribute\Backend\GroupPrice\AbstractGroupPrice

Definition at line 441 of file AbstractGroupPrice.php.

◆ preparePriceData()

preparePriceData ( array  $priceData,
  $productTypeId,
  $websiteId 
)

Prepare group prices data for website

Parameters
array$priceData
string$productTypeId
int$websiteId
Returns
array

Definition at line 273 of file AbstractGroupPrice.php.

274  {
275  $rates = $this->_getWebsiteCurrencyRates();
276  $data = [];
277  $price = $this->_catalogProductType->priceFactory($productTypeId);
278  foreach ($priceData as $v) {
279  if (!array_filter($v)) {
280  continue;
281  }
282  $key = implode('-', array_merge([$v['cust_group']], $this->_getAdditionalUniqueFields($v)));
283  if ($v['website_id'] == $websiteId) {
284  $data[$key] = $v;
285  $data[$key]['website_price'] = $v['price'];
286  } elseif ($v['website_id'] == 0 && !isset($data[$key])) {
287  $data[$key] = $v;
288  $data[$key]['website_id'] = $websiteId;
289  if ($this->_isPriceFixed($price)) {
290  $data[$key]['price'] = $v['price'] * $rates[$websiteId]['rate'];
291  $data[$key]['website_price'] = $v['price'] * $rates[$websiteId]['rate'];
292  }
293  }
294  }
295 
296  return $data;
297  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$price
$rates
Definition: tax.phtml:35

◆ setPriceData()

setPriceData (   $object,
  $priceData 
)

Set price data.

Parameters
\Magento\Catalog\Model\Product$object
array$priceData

Definition at line 339 of file AbstractGroupPrice.php.

340  {
341  $priceData = $this->modifyPriceData($object, $priceData);
342  $websiteId = $this->getWebsiteId($object->getStoreId());
343  if (!$object->getData('_edit_mode') && $websiteId) {
344  $priceData = $this->preparePriceData($priceData, $object->getTypeId(), $websiteId);
345  }
346 
347  $object->setData($this->getAttribute()->getName(), $priceData);
348  $object->setOrigData($this->getAttribute()->getName(), $priceData);
349 
350  $valueChangedKey = $this->getAttribute()->getName() . '_changed';
351  $object->setOrigData($valueChangedKey, 0);
352  $object->setData($valueChangedKey, 0);
353  }

◆ updateValues()

updateValues ( array  $valuesToUpdate,
array  $oldValues 
)
protected

Update values.

Parameters
array$valuesToUpdate
array$oldValues
Returns
boolean

Definition at line 396 of file AbstractGroupPrice.php.

397  {
398  $isChanged = false;
399  foreach ($valuesToUpdate as $key => $value) {
400  if ($oldValues[$key]['price'] != $value['value']) {
401  $price = new \Magento\Framework\DataObject(
402  [
403  'value_id' => $oldValues[$key]['price_id'],
404  'value' => $value['value']
405  ]
406  );
407  $this->_getResource()->savePriceData($price);
408  $isChanged = true;
409  }
410  }
411  return $isChanged;
412  }
$price
$value
Definition: gender.phtml:16

◆ validate()

validate (   $object)

Validate group price data

Parameters
\Magento\Catalog\Model\Product$object
Exceptions

Definition at line 172 of file AbstractGroupPrice.php.

173  {
174  $attribute = $this->getAttribute();
175  $priceRows = $object->getData($attribute->getName());
176  $priceRows = array_filter((array)$priceRows);
177 
178  if (empty($priceRows)) {
179  return true;
180  }
181 
182  // validate per website
183  $duplicates = [];
184  foreach ($priceRows as $priceRow) {
185  if (!empty($priceRow['delete'])) {
186  continue;
187  }
188  $compare = implode(
189  '-',
190  array_merge(
191  [$priceRow['website_id'], $priceRow['cust_group']],
192  $this->_getAdditionalUniqueFields($priceRow)
193  )
194  );
195  if (isset($duplicates[$compare])) {
196  throw new \Magento\Framework\Exception\LocalizedException(__($this->_getDuplicateErrorMessage()));
197  }
198 
199  $this->validatePrice($priceRow);
200 
201  $duplicates[$compare] = true;
202  }
203 
204  // if attribute scope is website and edit in store view scope
205  // add global group prices for duplicates find
206  if (!$attribute->isScopeGlobal() && $object->getStoreId()) {
207  $origPrices = $object->getOrigData($attribute->getName());
208  if ($origPrices) {
209  foreach ($origPrices as $price) {
210  if ($price['website_id'] == 0) {
211  $compare = implode(
212  '-',
213  array_merge(
214  [$price['website_id'], $price['cust_group']],
215  $this->_getAdditionalUniqueFields($price)
216  )
217  );
218  $duplicates[$compare] = true;
219  }
220  }
221  }
222  }
223 
224  // validate currency
225  $baseCurrency = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE, 'default');
226  $rates = $this->_getWebsiteCurrencyRates();
227  foreach ($priceRows as $priceRow) {
228  if (!empty($priceRow['delete'])) {
229  continue;
230  }
231  if ($priceRow['website_id'] == 0) {
232  continue;
233  }
234 
235  $globalCompare = implode(
236  '-',
237  array_merge([0, $priceRow['cust_group']], $this->_getAdditionalUniqueFields($priceRow))
238  );
239  $websiteCurrency = $rates[$priceRow['website_id']]['code'];
240 
241  if ($baseCurrency == $websiteCurrency && isset($duplicates[$globalCompare])) {
242  throw new \Magento\Framework\Exception\LocalizedException(__($this->_getDuplicateErrorMessage()));
243  }
244  }
245 
246  return true;
247  }
__()
Definition: __.php:13
$price
$rates
Definition: tax.phtml:35

◆ validatePrice()

validatePrice ( array  $priceRow)
protected

Validate price.

Parameters
array$priceRow
Returns
void
Exceptions

Definition at line 256 of file AbstractGroupPrice.php.

257  {
258  if (!isset($priceRow['price']) || !$this->isPositiveOrZero($priceRow['price'])) {
259  throw new \Magento\Framework\Exception\LocalizedException(
260  __('Group price must be a number greater than 0.')
261  );
262  }
263  }
__()
Definition: __.php:13

Field Documentation

◆ $_catalogProductType

$_catalogProductType
protected

Definition at line 46 of file AbstractGroupPrice.php.

◆ $_groupManagement

$_groupManagement
protected

Definition at line 51 of file AbstractGroupPrice.php.

◆ $_rates

$_rates
protected

Definition at line 31 of file AbstractGroupPrice.php.

◆ $metadataPool

$metadataPool
protected

Definition at line 24 of file AbstractGroupPrice.php.


The documentation for this class was generated from the following file: