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

Public Member Functions

 getAmounts ()
 
 hasNegativeItemAmount ()
 
- Public Member Functions inherited from Cart
 __construct (\Magento\Payment\Model\Cart\SalesModel\Factory $salesModelFactory, \Magento\Framework\Event\ManagerInterface $eventManager, $salesModel)
 
 getSalesModel ()
 
 addTax ($taxAmount)
 
 setTax ($taxAmount)
 
 getTax ()
 
 addDiscount ($discountAmount)
 
 setDiscount ($discountAmount)
 
 getDiscount ()
 
 addShipping ($shippingAmount)
 
 setShipping ($shippingAmount)
 
 getShipping ()
 
 addSubtotal ($subtotalAmount)
 
 getSubtotal ()
 
 addCustomItem ($name, $qty, $amount, $identifier=null)
 
 getAllItems ()
 
 getAmounts ()
 
 setTransferShippingAsItem ()
 
 setTransferDiscountAsItem ()
 

Protected Member Functions

 _calculateCustomItemsSubtotal ()
 
 _validate ()
 
 _importItemsFromSalesModel ()
 
 _applyDiscountTaxCompensationWorkaround (\Magento\Payment\Model\Cart\SalesModel\SalesModelInterface $salesEntity)
 
- Protected Member Functions inherited from Cart
 _collectItemsAndAmounts ()
 
 _importItemsFromSalesModel ()
 
 _calculateCustomItemsSubtotal ()
 
 _setTransferFlag ($flagType, $value)
 
 _setAmount ($amountType, $amount)
 
 _addAmount ($amountType, $amount)
 
 _getAmount ($amountType)
 
 _createItemFromData ($name, $qty, $amount, $identifier=null)
 
 _resetAmounts ()
 

Protected Attributes

 $_areAmountsValid = false
 
- Protected Attributes inherited from Cart
 $_salesModel
 
 $_eventManager
 
 $_amounts
 
 $_customItems = []
 
 $_salesModelItems = []
 
 $_transferFlags = []
 
 $_itemsCollectingRequired = true
 

Additional Inherited Members

- Data Fields inherited from Cart
const AMOUNT_TAX = 'tax'
 
const AMOUNT_SHIPPING = 'shipping'
 
const AMOUNT_DISCOUNT = 'discount'
 
const AMOUNT_SUBTOTAL = 'subtotal'
 

Detailed Description

PayPal-specific model for shopping cart items and totals The main idea is to accommodate all possible totals into PayPal-compatible 4 totals and line items

Definition at line 14 of file Cart.php.

Member Function Documentation

◆ _applyDiscountTaxCompensationWorkaround()

_applyDiscountTaxCompensationWorkaround ( \Magento\Payment\Model\Cart\SalesModel\SalesModelInterface  $salesEntity)
protected

Add "hidden" discount and shipping tax

Go ahead, try to understand ]:->

Tax settings for getting "discount tax":

Test case for getting "hidden shipping tax":

  • Make sure shipping is taxable (set shipping tax class)
  • Catalog Prices = Including Tax
  • Shipping Prices = Including Tax
  • Apply Customer Tax = After Discount
  • Create a cart price rule with % discount applied to the Shipping Amount
  • run shopping cart and estimate shipping
  • go to PayPal
Parameters
\Magento\Payment\Model\Cart\SalesModel\SalesModelInterface$salesEntity
Returns
void

Definition at line 177 of file Cart.php.

179  {
180  $dataContainer = $salesEntity->getTaxContainer();
181  $this->addTax((double)$dataContainer->getBaseDiscountTaxCompensationAmount());
182  $this->addTax((double)$dataContainer->getBaseShippingDiscountTaxCompensationAmount());
183  }
addTax($taxAmount)
Definition: Cart.php:106

◆ _calculateCustomItemsSubtotal()

_calculateCustomItemsSubtotal ( )
protected

Calculate subtotal from custom items

Returns
void

Definition at line 52 of file Cart.php.

53  {
54  parent::_calculateCustomItemsSubtotal();
55  $this->_applyDiscountTaxCompensationWorkaround($this->_salesModel);
56 
57  $this->_validate();
58  }
_applyDiscountTaxCompensationWorkaround(\Magento\Payment\Model\Cart\SalesModel\SalesModelInterface $salesEntity)
Definition: Cart.php:177

◆ _importItemsFromSalesModel()

_importItemsFromSalesModel ( )
protected

Import items from sales model with workarounds for PayPal

Returns
void

Definition at line 113 of file Cart.php.

114  {
115  $this->_salesModelItems = [];
116 
117  foreach ($this->_salesModel->getAllItems() as $item) {
118  if ($item->getParentItem()) {
119  continue;
120  }
121 
122  $amount = $item->getPrice();
123  $qty = $item->getQty();
124 
125  $subAggregatedLabel = '';
126 
127  // workaround in case if item subtotal precision is not compatible with PayPal (.2)
128  if ($amount - round($amount, 2)) {
129  $amount = $amount * $qty;
130  $subAggregatedLabel = ' x' . $qty;
131  $qty = 1;
132  }
133 
134  // aggregate item price if item qty * price does not match row total
135  $itemBaseRowTotal = $item->getOriginalItem()->getBaseRowTotal();
136  if ($amount * $qty != $itemBaseRowTotal) {
137  $amount = (double)$itemBaseRowTotal;
138  $subAggregatedLabel = ' x' . $qty;
139  $qty = 1;
140  }
141 
142  $this->_salesModelItems[] = $this->_createItemFromData(
143  $item->getName() . $subAggregatedLabel,
144  $qty,
145  $amount
146  );
147  }
148 
149  $this->addSubtotal($this->_salesModel->getBaseSubtotal());
150  $this->addTax($this->_salesModel->getBaseTaxAmount());
151  $this->addShipping($this->_salesModel->getBaseShippingAmount());
152  $this->addDiscount(abs($this->_salesModel->getBaseDiscountAmount()));
153  }
addShipping($shippingAmount)
Definition: Cart.php:176
$amount
Definition: order.php:14
_createItemFromData($name, $qty, $amount, $identifier=null)
Definition: Cart.php:414
addDiscount($discountAmount)
Definition: Cart.php:141
addTax($taxAmount)
Definition: Cart.php:106
addSubtotal($subtotalAmount)
Definition: Cart.php:211

◆ _validate()

_validate ( )
protected

Check the line items and totals according to PayPal business logic limitations

Returns
void

numbers are intentionally converted to strings because of possible comparison error see http://php.net/float

Definition at line 65 of file Cart.php.

66  {
67  $areItemsValid = false;
68  $this->_areAmountsValid = false;
69 
70  $referenceAmount = $this->_salesModel->getDataUsingMethod('base_grand_total');
71 
72  $itemsSubtotal = 0;
73  foreach ($this->getAllItems() as $i) {
74  $itemsSubtotal = $itemsSubtotal + $i->getQty() * $i->getAmount();
75  }
76 
77  $sum = $itemsSubtotal + $this->getTax();
78 
79  if (empty($this->_transferFlags[self::AMOUNT_SHIPPING])) {
80  $sum += $this->getShipping();
81  }
82 
83  if (empty($this->_transferFlags[self::AMOUNT_DISCOUNT])) {
84  $sum -= $this->getDiscount();
85  // PayPal requires to have discount less than items subtotal
86  $this->_areAmountsValid = round($this->getDiscount(), 4) < round($itemsSubtotal, 4);
87  } else {
88  $this->_areAmountsValid = $itemsSubtotal > 0.00001;
89  }
90 
95  // match sum of all the items and totals to the reference amount
96  if (sprintf('%.4F', $sum) == sprintf('%.4F', $referenceAmount)) {
97  $areItemsValid = true;
98  }
99 
100  $areItemsValid = $areItemsValid && $this->_areAmountsValid;
101 
102  if (!$areItemsValid) {
103  $this->_salesModelItems = [];
104  $this->_customItems = [];
105  }
106  }
$i
Definition: gallery.phtml:31

◆ getAmounts()

getAmounts ( )

Get shipping, tax, subtotal and discount amounts all together

Returns
array

Definition at line 26 of file Cart.php.

27  {
28  $this->_collectItemsAndAmounts();
29 
30  if (!$this->_areAmountsValid) {
31  $subtotal = $this->getSubtotal() + $this->getTax();
32 
33  if (empty($this->_transferFlags[self::AMOUNT_SHIPPING])) {
34  $subtotal += $this->getShipping();
35  }
36 
37  if (empty($this->_transferFlags[self::AMOUNT_DISCOUNT])) {
38  $subtotal -= $this->getDiscount();
39  }
40 
41  return [self::AMOUNT_SUBTOTAL => $subtotal];
42  }
43 
44  return $this->_amounts;
45  }

◆ hasNegativeItemAmount()

hasNegativeItemAmount ( )

Check whether any item has negative amount

Returns
bool

Definition at line 190 of file Cart.php.

191  {
192  foreach ($this->_customItems as $item) {
193  if ($item->getAmount() < 0) {
194  return true;
195  }
196  }
197  return false;
198  }

Field Documentation

◆ $_areAmountsValid

$_areAmountsValid = false
protected

Definition at line 19 of file Cart.php.


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