Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Subtotal.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class Subtotal extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
13 {
19  protected $quoteValidator = null;
20 
25  {
26  $this->quoteValidator = $quoteValidator;
27  }
28 
37  public function collect(
38  \Magento\Quote\Model\Quote $quote,
39  \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
41  ) {
42  parent::collect($quote, $shippingAssignment, $total);
43  $baseVirtualAmount = $virtualAmount = 0;
44 
45  $address = $shippingAssignment->getShipping()->getAddress();
46  $address->setTotalQty(0);
50  $items = $shippingAssignment->getItems();
51  foreach ($items as $item) {
52  if ($this->_initItem($address, $item) && $item->getQty() > 0) {
56  if ($item->getProduct()->isVirtual()) {
57  $virtualAmount += $item->getRowTotal();
58  $baseVirtualAmount += $item->getBaseRowTotal();
59  }
60  } else {
61  $this->_removeItem($address, $item);
62  }
63  }
64 
65  $total->setBaseVirtualAmount($baseVirtualAmount);
66  $total->setVirtualAmount($virtualAmount);
67 
71  $this->quoteValidator->validateQuoteAmount($quote, $total->getSubtotal());
72  $this->quoteValidator->validateQuoteAmount($quote, $total->getBaseSubtotal());
73  $address->setSubtotal($total->getSubtotal());
74  $address->setBaseSubtotal($total->getBaseSubtotal());
75  return $this;
76  }
77 
85  protected function _initItem($address, $item)
86  {
87  if ($item instanceof AddressItem) {
88  $quoteItem = $item->getAddress()->getQuote()->getItemById($item->getQuoteItemId());
89  } else {
90  $quoteItem = $item;
91  }
92  $product = $quoteItem->getProduct();
93  $product->setCustomerGroupId($quoteItem->getQuote()->getCustomerGroupId());
94 
98  if ($item->getQuote()->getIsSuperMode()) {
99  if (!$product) {
100  return false;
101  }
102  } else {
103  if (!$product || !$product->isVisibleInCatalog()) {
104  return false;
105  }
106  }
107 
108  $quoteItem->setConvertedPrice(null);
109  $originalPrice = $product->getPrice();
110  if ($quoteItem->getParentItem() && $quoteItem->isChildrenCalculated()) {
111  $finalPrice = $quoteItem->getParentItem()->getProduct()->getPriceModel()->getChildFinalPrice(
112  $quoteItem->getParentItem()->getProduct(),
113  $quoteItem->getParentItem()->getQty(),
114  $product,
115  $quoteItem->getQty()
116  );
117  $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
118  } elseif (!$quoteItem->getParentItem()) {
119  $finalPrice = $product->getFinalPrice($quoteItem->getQty());
120  $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
121  $this->_addAmount($item->getRowTotal());
122  $this->_addBaseAmount($item->getBaseRowTotal());
123  $address->setTotalQty($address->getTotalQty() + $item->getQty());
124  }
125  return true;
126  }
127 
136  protected function _calculateRowTotal($item, $finalPrice, $originalPrice)
137  {
138  if (!$originalPrice) {
139  $originalPrice = $finalPrice;
140  }
141  $item->setPrice($finalPrice)->setBaseOriginalPrice($originalPrice);
142  $item->calcRowTotal();
143  return $this;
144  }
145 
153  protected function _removeItem($address, $item)
154  {
155  if ($item instanceof Item) {
156  $address->removeItem($item->getId());
157  if ($address->getQuote()) {
158  $address->getQuote()->removeItem($item->getId());
159  }
160  } elseif ($item instanceof AddressItem) {
161  $address->removeItem($item->getId());
162  if ($address->getQuote()) {
163  $address->getQuote()->removeItem($item->getQuoteItemId());
164  }
165  }
166  return $this;
167  }
168 
177  public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
178  {
179  return [
180  'code' => $this->getCode(),
181  'title' => $this->getLabel(),
182  'value' => $total->getSubtotal()
183  ];
184  }
185 
191  public function getLabel()
192  {
193  return __('Subtotal');
194  }
195 }
_calculateRowTotal($item, $finalPrice, $originalPrice)
Definition: Subtotal.php:136
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: Subtotal.php:37
fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
Definition: Subtotal.php:177
$quote
__()
Definition: __.php:13
$address
Definition: customer.php:38
$quoteItem
Definition: quote.php:38
__construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
Definition: Subtotal.php:24
$items