Ejemplo n.º 1
0
 /**
  * Collect address subtotal
  *
  * @param \Magento\Quote\Model\Quote $quote
  * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
  * @param Address\Total $total
  * @return $this
  */
 public function collect(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total)
 {
     parent::collect($quote, $shippingAssignment, $total);
     $total->setTotalQty(0);
     $baseVirtualAmount = $virtualAmount = 0;
     $address = $shippingAssignment->getShipping()->getAddress();
     /**
      * Process address items
      */
     $items = $shippingAssignment->getItems();
     foreach ($items as $item) {
         if ($this->_initItem($address, $item) && $item->getQty() > 0) {
             /**
              * Separately calculate subtotal only for virtual products
              */
             if ($item->getProduct()->isVirtual()) {
                 $virtualAmount += $item->getRowTotal();
                 $baseVirtualAmount += $item->getBaseRowTotal();
             }
         } else {
             $this->_removeItem($address, $item);
         }
     }
     $total->setBaseVirtualAmount($baseVirtualAmount);
     $total->setVirtualAmount($virtualAmount);
     /**
      * Initialize grand totals
      */
     $this->quoteValidator->validateQuoteAmount($quote, $total->getSubtotal());
     $this->quoteValidator->validateQuoteAmount($quote, $total->getBaseSubtotal());
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Collect address subtotal
  *
  * @param Address $address
  * @return $this
  */
 public function collect(Address $address)
 {
     parent::collect($address);
     $address->setTotalQty(0);
     $baseVirtualAmount = $virtualAmount = 0;
     /**
      * Process address items
      */
     $items = $this->_getAddressItems($address);
     foreach ($items as $item) {
         if ($this->_initItem($address, $item) && $item->getQty() > 0) {
             /**
              * Separately calculate subtotal only for virtual products
              */
             if ($item->getProduct()->isVirtual()) {
                 $virtualAmount += $item->getRowTotal();
                 $baseVirtualAmount += $item->getBaseRowTotal();
             }
         } else {
             $this->_removeItem($address, $item);
         }
     }
     $address->setBaseVirtualAmount($baseVirtualAmount);
     $address->setVirtualAmount($virtualAmount);
     /**
      * Initialize grand totals
      */
     $this->quoteValidator->validateQuoteAmount($address->getQuote(), $address->getSubtotal());
     $this->quoteValidator->validateQuoteAmount($address->getQuote(), $address->getBaseSubtotal());
     return $this;
 }
 public function testCheckQuoteAmountAmountGreaterThanAvailable()
 {
     $this->quoteMock->expects($this->once())->method('getHasError')->will($this->returnValue(false));
     $this->quoteMock->expects($this->once())->method('setHasError')->with(true);
     $this->quoteMock->expects($this->once())->method('addMessage')->with(__('This item price or quantity is not valid for checkout.'));
     $this->assertSame($this->quoteValidator, $this->quoteValidator->validateQuoteAmount($this->quoteMock, QuoteValidator::MAXIMUM_AVAILABLE_NUMBER + 1));
 }
Ejemplo n.º 4
0
 public function testSubmit()
 {
     $orderData = [];
     $isGuest = true;
     $isVirtual = false;
     $customerId = 1;
     $quoteId = 1;
     $quoteItem = $this->getMock('Magento\\Quote\\Model\\Quote\\Item', [], [], '', false);
     $billingAddress = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false);
     $shippingAddress = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false);
     $payment = $this->getMock('Magento\\Quote\\Model\\Quote\\Payment', [], [], '', false);
     $baseOrder = $this->getMock('Magento\\Sales\\Api\\Data\\OrderInterface', [], [], '', false);
     $convertedBillingAddress = $this->getMock('Magento\\Sales\\Api\\Data\\OrderAddressInterface', [], [], '', false);
     $convertedShippingAddress = $this->getMock('Magento\\Sales\\Api\\Data\\OrderAddressInterface', [], [], '', false);
     $convertedPayment = $this->getMock('Magento\\Sales\\Api\\Data\\OrderPaymentInterface', [], [], '', false);
     $convertedQuoteItem = $this->getMock('Magento\\Sales\\Api\\Data\\OrderItemInterface', [], [], '', false);
     $addresses = [$convertedShippingAddress, $convertedBillingAddress];
     $payments = [$convertedPayment];
     $quoteItems = [$quoteItem];
     $convertedItems = [$convertedQuoteItem];
     $quote = $this->getQuote($isGuest, $isVirtual, $billingAddress, $payment, $customerId, $quoteId, $quoteItems, $shippingAddress);
     $this->quoteValidator->expects($this->once())->method('validateBeforeSubmit')->with($quote);
     $this->quoteAddressToOrder->expects($this->once())->method('convert')->with($shippingAddress, $orderData)->willReturn($baseOrder);
     $this->quoteAddressToOrderAddress->expects($this->at(0))->method('convert')->with($shippingAddress, ['address_type' => 'shipping', 'email' => '*****@*****.**'])->willReturn($convertedShippingAddress);
     $this->quoteAddressToOrderAddress->expects($this->at(1))->method('convert')->with($billingAddress, ['address_type' => 'billing', 'email' => '*****@*****.**'])->willReturn($convertedBillingAddress);
     $this->quoteItemToOrderItem->expects($this->once())->method('convert')->with($quoteItem, ['parent_item' => null])->willReturn($convertedQuoteItem);
     $this->quotePaymentToOrderPayment->expects($this->once())->method('convert')->with($payment)->willReturn($convertedPayment);
     $order = $this->prepareOrderFactory($baseOrder, $convertedBillingAddress, $addresses, $payments, $convertedItems, $quoteId, $convertedShippingAddress);
     $this->orderManagement->expects($this->once())->method('place')->with($order)->willReturn($order);
     $this->eventManager->expects($this->at(0))->method('dispatch')->with('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
     $this->eventManager->expects($this->at(1))->method('dispatch')->with('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
     $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quote);
     $this->assertEquals($order, $this->model->submit($quote, $orderData));
 }
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @return \Magento\Quote\Model\Quote\Address\Total
  */
 public function collect(\Magento\Quote\Model\Quote $quote)
 {
     /** @var \Magento\Quote\Model\Quote\Address\Total $total */
     $total = $this->totalFactory->create('Magento\\Quote\\Model\\Quote\\Address\\Total');
     $this->eventManager->dispatch('sales_quote_collect_totals_before', ['quote' => $quote]);
     $this->_collectItemsQtys($quote);
     $total->setSubtotal(0);
     $total->setBaseSubtotal(0);
     $total->setSubtotalWithDiscount(0);
     $total->setBaseSubtotalWithDiscount(0);
     $total->setGrandTotal(0);
     $total->setBaseGrandTotal(0);
     /** @var \Magento\Quote\Model\Quote\Address $address */
     foreach ($quote->getAllAddresses() as $address) {
         $addressTotal = $this->collectAddressTotals($quote, $address);
         $total->setShippingAmount($addressTotal->getShippingAmount());
         $total->setBaseShippingAmount($addressTotal->getBaseShippingAmount());
         $total->setShippingDescription($addressTotal->getShippingDescription());
         $total->setSubtotal((double) $total->getSubtotal() + $addressTotal->getSubtotal());
         $total->setBaseSubtotal((double) $total->getBaseSubtotal() + $addressTotal->getBaseSubtotal());
         $total->setSubtotalWithDiscount((double) $total->getSubtotalWithDiscount() + $addressTotal->getSubtotalWithDiscount());
         $total->setBaseSubtotalWithDiscount((double) $total->getBaseSubtotalWithDiscount() + $addressTotal->getBaseSubtotalWithDiscount());
         $total->setGrandTotal((double) $total->getGrandTotal() + $addressTotal->getGrandTotal());
         $total->setBaseGrandTotal((double) $total->getBaseGrandTotal() + $addressTotal->getBaseGrandTotal());
     }
     $this->quoteValidator->validateQuoteAmount($quote, $quote->getGrandTotal());
     $this->quoteValidator->validateQuoteAmount($quote, $quote->getBaseGrandTotal());
     $this->_validateCouponCode($quote);
     $this->eventManager->dispatch('sales_quote_collect_totals_after', ['quote' => $quote]);
     return $total;
 }
 /**
  * @expectedException \Magento\Framework\Exception\LocalizedException
  * @expectedExceptionMessage Please select a valid payment method.
  */
 public function testValidateBeforeSubmitThrowsExceptionIfPaymentMethodIsNotSelected()
 {
     $paymentMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Payment', [], [], '', false);
     $billingAddressMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Address', [], [], '', false);
     $billingAddressMock->expects($this->any())->method('validate')->willReturn(true);
     $this->quoteMock->expects($this->any())->method('getBillingAddress')->willReturn($billingAddressMock);
     $this->quoteMock->expects($this->any())->method('getPayment')->willReturn($paymentMock);
     $this->quoteMock->expects($this->any())->method('isVirtual')->willReturn(true);
     $this->quoteValidator->validateBeforeSubmit($this->quoteMock);
 }
Ejemplo n.º 7
0
 /**
  * Submit quote
  *
  * @param Quote $quote
  * @param array $orderData
  * @return \Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object
  * @throws \Exception
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function submitQuote(QuoteEntity $quote, $orderData = [])
 {
     $order = $this->orderFactory->create();
     $this->quoteValidator->validateBeforeSubmit($quote);
     if (!$quote->getCustomerIsGuest()) {
         if ($quote->getCustomerId()) {
             $this->_prepareCustomerQuote($quote);
         }
         $this->customerManagement->populateCustomerInfo($quote);
     }
     $addresses = [];
     $quote->reserveOrderId();
     if ($quote->isVirtual()) {
         $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData));
     } else {
         $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getShippingAddress(), $orderData));
         $shippingAddress = $this->quoteAddressToOrderAddress->convert($quote->getShippingAddress(), ['address_type' => 'shipping', 'email' => $quote->getCustomerEmail()]);
         $addresses[] = $shippingAddress;
         $order->setShippingAddress($shippingAddress);
         $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod());
     }
     $billingAddress = $this->quoteAddressToOrderAddress->convert($quote->getBillingAddress(), ['address_type' => 'billing', 'email' => $quote->getCustomerEmail()]);
     $addresses[] = $billingAddress;
     $order->setBillingAddress($billingAddress);
     $order->setAddresses($addresses);
     $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment()));
     $order->setItems($this->resolveItems($quote));
     if ($quote->getCustomer()) {
         $order->setCustomerId($quote->getCustomer()->getId());
     }
     $order->setQuoteId($quote->getId());
     $order->setCustomerEmail($quote->getCustomerEmail());
     $order->setCustomerFirstname($quote->getCustomerFirstname());
     $order->setCustomerMiddlename($quote->getCustomerMiddlename());
     $order->setCustomerLastname($quote->getCustomerLastname());
     $this->eventManager->dispatch('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]);
     try {
         $order = $this->orderManagement->place($order);
         $quote->setIsActive(false);
         $this->eventManager->dispatch('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]);
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->eventManager->dispatch('sales_model_service_quote_submit_failure', ['order' => $order, 'quote' => $quote, 'exception' => $e]);
         throw $e;
     }
     return $order;
 }
Ejemplo n.º 8
0
 /**
  * Collect totals
  *
  * @return $this
  */
 public function collectTotals()
 {
     /**
      * Protect double totals collection
      */
     if ($this->getTotalsCollectedFlag()) {
         return $this;
     }
     $this->_eventManager->dispatch($this->_eventPrefix . '_collect_totals_before', [$this->_eventObject => $this]);
     $this->_collectItemsQtys();
     $this->setSubtotal(0);
     $this->setBaseSubtotal(0);
     $this->setSubtotalWithDiscount(0);
     $this->setBaseSubtotalWithDiscount(0);
     $this->setGrandTotal(0);
     $this->setBaseGrandTotal(0);
     foreach ($this->getAllAddresses() as $address) {
         $address->setSubtotal(0);
         $address->setBaseSubtotal(0);
         $address->setGrandTotal(0);
         $address->setBaseGrandTotal(0);
         $address->collectTotals();
         $this->setSubtotal((double) $this->getSubtotal() + $address->getSubtotal());
         $this->setBaseSubtotal((double) $this->getBaseSubtotal() + $address->getBaseSubtotal());
         $this->setSubtotalWithDiscount((double) $this->getSubtotalWithDiscount() + $address->getSubtotalWithDiscount());
         $this->setBaseSubtotalWithDiscount((double) $this->getBaseSubtotalWithDiscount() + $address->getBaseSubtotalWithDiscount());
         $this->setGrandTotal((double) $this->getGrandTotal() + $address->getGrandTotal());
         $this->setBaseGrandTotal((double) $this->getBaseGrandTotal() + $address->getBaseGrandTotal());
     }
     $this->quoteValidator->validateQuoteAmount($this, $this->getGrandTotal());
     $this->quoteValidator->validateQuoteAmount($this, $this->getBaseGrandTotal());
     $this->setData('trigger_recollect', 0);
     $this->_validateCouponCode();
     $this->_eventManager->dispatch($this->_eventPrefix . '_collect_totals_after', [$this->_eventObject => $this]);
     $this->setTotalsCollectedFlag(true);
     return $this;
 }