Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Utility Class Reference

Public Member Functions

 __construct (\Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory $usageFactory, \Magento\SalesRule\Model\CouponFactory $couponFactory, \Magento\SalesRule\Model\Rule\CustomerFactory $customerFactory, \Magento\Framework\DataObjectFactory $objectFactory, PriceCurrencyInterface $priceCurrency)
 
 minFix (\Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData, \Magento\Quote\Model\Quote\Item\AbstractItem $item, $qty)
 
 deltaRoundingFix (\Magento\SalesRule\Model\Rule\Action\Discount\Data $discountData, \Magento\Quote\Model\Quote\Item\AbstractItem $item)
 
 getItemPrice ($item)
 
 getItemBasePrice ($item)
 
 getItemQty ($item, $rule)
 
 mergeIds ($a1, $a2, $asString=true)
 
 resetRoundingDeltas ()
 

Protected Attributes

 $_roundingDeltas = []
 
 $_baseRoundingDeltas = []
 
 $usageFactory
 
 $couponFactory
 
 $customerFactory
 
 $objectFactory
 
 $priceCurrency
 

Detailed Description

Definition at line 16 of file Utility.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory  $usageFactory,
\Magento\SalesRule\Model\CouponFactory  $couponFactory,
\Magento\SalesRule\Model\Rule\CustomerFactory  $customerFactory,
\Magento\Framework\DataObjectFactory  $objectFactory,
PriceCurrencyInterface  $priceCurrency 
)
Parameters
\Magento\SalesRule\Model\ResourceModel\Coupon\UsageFactory$usageFactory
CouponFactory$couponFactory
Rule\CustomerFactory$customerFactory
\Magento\Framework\DataObjectFactory$objectFactory
PriceCurrencyInterface$priceCurrency

Definition at line 60 of file Utility.php.

66  {
67  $this->couponFactory = $couponFactory;
68  $this->customerFactory = $customerFactory;
69  $this->usageFactory = $usageFactory;
70  $this->objectFactory = $objectFactory;
71  $this->priceCurrency = $priceCurrency;
72  }

Member Function Documentation

◆ deltaRoundingFix()

Process "delta" rounding

Parameters
\Magento\SalesRule\Model\Rule\Action\Discount\Data$discountData
\Magento\Quote\Model\Quote\Item\AbstractItem$item
Returns
$this

When we have 100% discount check if totals will not be negative

Definition at line 186 of file Utility.php.

189  {
190  $discountAmount = $discountData->getAmount();
191  $baseDiscountAmount = $discountData->getBaseAmount();
192  $rowTotalInclTax = $item->getRowTotalInclTax();
193  $baseRowTotalInclTax = $item->getBaseRowTotalInclTax();
194 
195  //TODO Seems \Magento\Quote\Model\Quote\Item\AbstractItem::getDiscountPercent() returns float value
196  //that can not be used as array index
197  $percentKey = $item->getDiscountPercent();
198  if ($percentKey) {
199  $delta = isset($this->_roundingDeltas[$percentKey]) ? $this->_roundingDeltas[$percentKey] : 0;
200  $baseDelta = isset($this->_baseRoundingDeltas[$percentKey]) ? $this->_baseRoundingDeltas[$percentKey] : 0;
201 
202  $discountAmount += $delta;
203  $baseDiscountAmount += $baseDelta;
204 
205  $this->_roundingDeltas[$percentKey] = $discountAmount - $this->priceCurrency->round($discountAmount);
206  $this->_baseRoundingDeltas[$percentKey] = $baseDiscountAmount
207  - $this->priceCurrency->round($baseDiscountAmount);
208  }
209 
214  if ($percentKey == 100) {
215  $discountDelta = $rowTotalInclTax - $discountAmount;
216  $baseDiscountDelta = $baseRowTotalInclTax - $baseDiscountAmount;
217 
218  if ($discountDelta < 0) {
219  $discountAmount += $discountDelta;
220  }
221 
222  if ($baseDiscountDelta < 0) {
223  $baseDiscountAmount += $baseDiscountDelta;
224  }
225  }
226 
227  $discountData->setAmount($this->priceCurrency->round($discountAmount));
228  $discountData->setBaseAmount($this->priceCurrency->round($baseDiscountAmount));
229 
230  return $this;
231  }

◆ getItemBasePrice()

getItemBasePrice (   $item)

Return item base price

Parameters
\Magento\Quote\Model\Quote\Item\AbstractItem$item
Returns
float

Definition at line 252 of file Utility.php.

253  {
254  $price = $item->getDiscountCalculationPrice();
255  return $price !== null ? $item->getBaseDiscountCalculationPrice() : $item->getBaseCalculationPrice();
256  }
$price

◆ getItemPrice()

getItemPrice (   $item)

Return item price

Parameters
\Magento\Quote\Model\Quote\Item\AbstractItem$item
Returns
float

Definition at line 239 of file Utility.php.

240  {
241  $price = $item->getDiscountCalculationPrice();
242  $calcPrice = $item->getCalculationPrice();
243  return $price === null ? $calcPrice : $price;
244  }
$price

◆ getItemQty()

getItemQty (   $item,
  $rule 
)

Return discount item qty

Parameters
\Magento\Quote\Model\Quote\Item\AbstractItem$item
\Magento\SalesRule\Model\Rule$rule
Returns
int

Definition at line 265 of file Utility.php.

266  {
267  $qty = $item->getTotalQty();
268  $discountQty = $rule->getDiscountQty();
269  return $discountQty ? min($qty, $discountQty) : $qty;
270  }

◆ mergeIds()

mergeIds (   $a1,
  $a2,
  $asString = true 
)

Merge two sets of ids

Parameters
array | string$a1
array | string$a2
bool$asString
Returns
array|string

Definition at line 280 of file Utility.php.

281  {
282  if (!is_array($a1)) {
283  $a1 = empty($a1) ? [] : explode(',', $a1);
284  }
285  if (!is_array($a2)) {
286  $a2 = empty($a2) ? [] : explode(',', $a2);
287  }
288  $a = array_unique(array_merge($a1, $a2));
289  if ($asString) {
290  $a = implode(',', $a);
291  }
292  return $a;
293  }

◆ minFix()

Parameters
\Magento\SalesRule\Model\Rule\Action\Discount\Data$discountData
\Magento\Quote\Model\Quote\Item\AbstractItem$item
float$qty
Returns
void

Definition at line 161 of file Utility.php.

165  {
166  $itemPrice = $this->getItemPrice($item);
167  $baseItemPrice = $this->getItemBasePrice($item);
168 
169  $itemDiscountAmount = $item->getDiscountAmount();
170  $itemBaseDiscountAmount = $item->getBaseDiscountAmount();
171 
172  $discountAmount = min($itemDiscountAmount + $discountData->getAmount(), $itemPrice * $qty);
173  $baseDiscountAmount = min($itemBaseDiscountAmount + $discountData->getBaseAmount(), $baseItemPrice * $qty);
174 
175  $discountData->setAmount($discountAmount);
176  $discountData->setBaseAmount($baseDiscountAmount);
177  }

◆ resetRoundingDeltas()

resetRoundingDeltas ( )
Returns
void

Definition at line 298 of file Utility.php.

299  {
300  $this->_roundingDeltas = [];
301  $this->_baseRoundingDeltas = [];
302  }

Field Documentation

◆ $_baseRoundingDeltas

$_baseRoundingDeltas = []
protected

Definition at line 26 of file Utility.php.

◆ $_roundingDeltas

$_roundingDeltas = []
protected

Definition at line 21 of file Utility.php.

◆ $couponFactory

$couponFactory
protected

Definition at line 36 of file Utility.php.

◆ $customerFactory

$customerFactory
protected

Definition at line 41 of file Utility.php.

◆ $objectFactory

$objectFactory
protected

Definition at line 46 of file Utility.php.

◆ $priceCurrency

$priceCurrency
protected

Definition at line 51 of file Utility.php.

◆ $usageFactory

$usageFactory
protected

Definition at line 31 of file Utility.php.


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