Пример #1
1
 /**
  * Initialize coupon
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function execute()
 {
     $couponCode = $this->getRequest()->getParam('remove') == 1 ? '' : trim($this->getRequest()->getParam('coupon_code'));
     $cartQuote = $this->cart->getQuote();
     $oldCouponCode = $cartQuote->getCouponCode();
     $codeLength = strlen($couponCode);
     if (!$codeLength && !strlen($oldCouponCode)) {
         return $this->_goBack();
     }
     try {
         $isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
         $itemsCount = $cartQuote->getItemsCount();
         if ($itemsCount) {
             $cartQuote->getShippingAddress()->setCollectShippingRates(true);
             $cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
             $this->quoteRepository->save($cartQuote);
         }
         if ($codeLength) {
             $escaper = $this->_objectManager->get('Magento\\Framework\\Escaper');
             if (!$itemsCount) {
                 if ($isCodeLengthValid) {
                     $coupon = $this->couponFactory->create();
                     $coupon->load($couponCode, 'code');
                     if ($coupon->getId()) {
                         $this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
                         $this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
                     } else {
                         $this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
                     }
                 } else {
                     $this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
                 }
             } else {
                 if ($isCodeLengthValid && $couponCode == $cartQuote->getCouponCode()) {
                     $this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
                 } else {
                     $this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
                     $this->cart->save();
                 }
             }
         } else {
             $this->messageManager->addSuccess(__('You canceled the coupon code.'));
         }
     } catch (\Magento\Framework\Exception\LocalizedException $e) {
         $this->messageManager->addError($e->getMessage());
     } catch (\Exception $e) {
         $this->messageManager->addError(__('We cannot apply the coupon code.'));
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
     }
     return $this->_goBack();
 }
 /**
  * {@inheritDoc}
  *
  * @param int $cartId The cart ID.
  * @return Totals Quote totals data.
  */
 public function get($cartId)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     if ($quote->isVirtual()) {
         $addressTotalsData = $quote->getBillingAddress()->getData();
         $addressTotals = $quote->getBillingAddress()->getTotals();
     } else {
         $addressTotalsData = $quote->getShippingAddress()->getData();
         $addressTotals = $quote->getShippingAddress()->getTotals();
     }
     /** @var \Magento\Quote\Api\Data\TotalsInterface $quoteTotals */
     $quoteTotals = $this->totalsFactory->create();
     $this->dataObjectHelper->populateWithArray($quoteTotals, $addressTotalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface');
     $items = [];
     foreach ($quote->getAllVisibleItems() as $index => $item) {
         $items[$index] = $this->itemConverter->modelToDataObject($item);
     }
     $calculatedTotals = $this->totalsConverter->process($addressTotals);
     $quoteTotals->setTotalSegments($calculatedTotals);
     $amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
     $amount = $amount > 0 ? $amount : 0;
     $quoteTotals->setCouponCode($this->couponService->get($cartId));
     $quoteTotals->setGrandTotal($amount);
     $quoteTotals->setItems($items);
     $quoteTotals->setItemsQty($quote->getItemsQty());
     $quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
     $quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
     return $quoteTotals;
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function placeOrder($cartId, $agreements = null, PaymentInterface $paymentMethod = null)
 {
     /** @var $quoteIdMask QuoteIdMask */
     $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
     $this->cartRepository->get($quoteIdMask->getQuoteId())->setCheckoutMethod(CartManagementInterface::METHOD_GUEST);
     return $this->quoteManagement->placeOrder($quoteIdMask->getQuoteId(), $agreements, $paymentMethod);
 }
Пример #4
0
 /**
  * Update quote data
  *
  * @param Quote $quote
  * @param array $details
  * @return void
  */
 private function updateQuote(Quote $quote, array $details)
 {
     $quote->setMayEditShippingAddress(false);
     $quote->setMayEditShippingMethod(true);
     $this->updateQuoteAddress($quote, $details);
     $this->disabledQuoteAddressValidation($quote);
     $quote->collectTotals();
     $this->quoteRepository->save($quote);
 }
Пример #5
0
 /**
  * Saves payment information in quote
  *
  * @param Object $response
  * @return void
  */
 public function savePaymentInQuote($response)
 {
     $quote = $this->quoteRepository->get($this->sessionTransparent->getQuoteId());
     /** @var InfoInterface $payment */
     $payment = $this->paymentManagement->get($quote->getId());
     $payment->setAdditionalInformation('pnref', $response->getPnref());
     $this->errorHandler->handle($payment, $response);
     $this->paymentManagement->set($quote->getId(), $payment);
 }
Пример #6
0
 /**
  * Fetch coupon info
  *
  * Controller Action
  */
 public function execute()
 {
     $total = $this->getRequest()->getParam('cost');
     $quote = $this->_checkoutSession->getQuote();
     //save value to DiscountCoupon collect
     $this->_registry->register('mercadopago_total_amount', $total);
     $this->quoteRepository->save($quote->collectTotals());
     return;
 }
Пример #7
0
 /**
  * Initialize shipping information
  *
  * @return \Magento\Framework\Controller\Result\Redirect
  */
 public function execute()
 {
     $country = (string) $this->getRequest()->getParam('country_id');
     $postcode = (string) $this->getRequest()->getParam('estimate_postcode');
     $city = (string) $this->getRequest()->getParam('estimate_city');
     $regionId = (string) $this->getRequest()->getParam('region_id');
     $region = (string) $this->getRequest()->getParam('region');
     $this->cart->getQuote()->getShippingAddress()->setCountryId($country)->setCity($city)->setPostcode($postcode)->setRegionId($regionId)->setRegion($region)->setCollectShippingRates(true);
     $this->quoteRepository->save($this->cart->getQuote());
     $this->cart->save();
     return $this->_goBack();
 }
 /**
  * {@inheritDoc}
  */
 public function savePaymentInformation($cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null)
 {
     if ($billingAddress) {
         $billingAddress->setEmail($email);
         $this->billingAddressManagement->assign($cartId, $billingAddress);
     } else {
         $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id');
         $this->cartRepository->getActive($quoteIdMask->getQuoteId())->getBillingAddress()->setEmail($email);
     }
     $this->paymentMethodManagement->set($cartId, $paymentMethod);
     return true;
 }
 /**
  * Return shipping options items for shipping address from request
  *
  * @return void
  */
 public function execute()
 {
     try {
         $quoteId = $this->getRequest()->getParam('quote_id');
         $this->_quote = $this->quoteRepository->get($quoteId);
         $this->_initCheckout();
         $response = $this->_checkout->getShippingOptionsCallbackResponse($this->getRequest()->getParams());
         $this->getResponse()->setBody($response);
     } catch (\Exception $e) {
         $this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
     }
 }
 public function testExecute()
 {
     $quoteMock = $this->getQuoteMock();
     $quoteMock->expects(self::exactly(2))->method('getIsVirtual')->willReturn(false);
     $quoteMock->expects(self::exactly(2))->method('getShippingAddress')->willReturn($this->shippingAddressMock);
     $this->shippingAddressMock->expects(self::once())->method('getShippingMethod')->willReturn(self::TEST_SHIPPING_METHOD . '-bad');
     $this->disabledQuoteAddressValidationStep($quoteMock);
     $this->shippingAddressMock->expects(self::once())->method('setShippingMethod')->willReturn(self::TEST_SHIPPING_METHOD);
     $this->shippingAddressMock->expects(self::once())->method('setCollectShippingRates')->willReturn(true);
     $quoteMock->expects(self::once())->method('collectTotals');
     $this->quoteRepositoryMock->expects(self::once())->method('save')->with($quoteMock);
     $this->shippingMethodUpdater->execute(self::TEST_SHIPPING_METHOD, $quoteMock);
 }
 /**
  * {@inheritDoc}
  */
 public function calculate($cartId, \Magento\Checkout\Api\Data\TotalsInformationInterface $addressInformation)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->cartRepository->get($cartId);
     $this->validateQuote($quote);
     if ($quote->getIsVirtual()) {
         $quote->setBillingAddress($addressInformation->getAddress());
     } else {
         $quote->setShippingAddress($addressInformation->getAddress());
         $quote->getShippingAddress()->setCollectShippingRates(true)->setShippingMethod($addressInformation->getShippingCarrierCode() . '_' . $addressInformation->getShippingMethodCode());
     }
     $quote->collectTotals();
     return $this->cartTotalRepository->get($cartId);
 }
 protected function recordOrder($order)
 {
     $customOrderId = null;
     //https://github.com/magento/magento2/issues/4233
     $quoteId = $order->getQuoteId();
     $quote = $this->quoteRepository->get($quoteId);
     $shippingAddress = $quote->getShippingAddress();
     $carrierType = $shippingAddress->getCarrierType();
     //  $order->setCarrierType($carrierType);
     $order->setDestinationType($shippingAddress->getDestinationType());
     $order->setValidationStatus($shippingAddress->getValidationStatus());
     $this->carrierGroupHelper->saveOrderDetail($order, $shippingAddress);
     $this->carrierGroupHelper->recordOrderItems($order);
     $this->packageHelper->saveOrderPackages($order, $shippingAddress);
     if (strstr($order->getShippingMethod(), 'shqshared_')) {
         $orderDetailArray = $this->carrierGroupHelper->loadOrderDetailByOrderId($order->getId());
         //SHQ16- Review for splits
         foreach ($orderDetailArray as $orderDetail) {
             $original = $orderDetail->getCarrierType();
             $carrierTypeArray = explode('_', $orderDetail->getCarrierType());
             if (is_array($carrierTypeArray)) {
                 $orderDetail->setCarrierType($carrierTypeArray[1]);
                 //SHQ16-1026
                 $currentShipDescription = $order->getShippingDescription();
                 $shipDescriptionArray = explode('-', $currentShipDescription);
                 $cgArray = $this->shipperDataHelper->decodeShippingDetails($orderDetail->getCarrierGroupDetail());
                 foreach ($cgArray as $key => $cgDetail) {
                     if (isset($cgDetail['carrierType']) && $cgDetail['carrierType'] == $original) {
                         $cgDetail['carrierType'] = $carrierTypeArray[1];
                     }
                     if (is_array($shipDescriptionArray) && isset($cgDetail['carrierTitle'])) {
                         $shipDescriptionArray[0] = $cgDetail['carrierTitle'] . ' ';
                         $newShipDescription = implode('-', $shipDescriptionArray);
                         $order->setShippingDescription($newShipDescription);
                     }
                     $cgArray[$key] = $cgDetail;
                 }
                 $encoded = $this->shipperDataHelper->encode($cgArray);
                 $orderDetail->setCarrierGroupDetail($encoded);
                 $orderDetail->save();
             }
             $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Rates displayed as single carrier', 'Resetting carrier type on order to be ' . $carrierTypeArray[1]);
         }
     }
     if ($this->shipperDataHelper->useDefaultCarrierCodes()) {
         $order->setShippingMethod($this->getDefaultCarrierShipMethod($order, $shippingAddress));
     }
     $order->save();
 }
Пример #13
0
 /**
  * @param \Magento\Checkout\Controller\Cart $subject
  * @param \Magento\Framework\App\RequestInterface $request
  * @return void
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function beforeDispatch(\Magento\Checkout\Controller\Cart $subject, \Magento\Framework\App\RequestInterface $request)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->checkoutSession->getQuote();
     // Clear shipping addresses and item assignments after Multishipping flow
     if ($quote->isMultipleShippingAddresses()) {
         foreach ($quote->getAllShippingAddresses() as $address) {
             $quote->removeAddress($address->getId());
         }
         $quote->getShippingAddress();
         $quote->setIsMultiShipping(false);
         $quote->collectTotals();
         $this->cartRepository->save($quote);
     }
 }
 public function testSavePaymentInQuote()
 {
     $quoteId = 1;
     $response = new DataObject();
     $payment = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Payment')->disableOriginalConstructor()->getMock();
     $payment->expects($this->once())->method('setAdditionalInformation')->with('pnref');
     $this->errorHandlerMock->expects($this->once())->method('handle')->with($payment, $response);
     $quote = $this->getMock('Magento\\Quote\\Api\\Data\\CartInterface', [], [], '', false);
     $quote->expects($this->exactly(2))->method('getId')->willReturn($quoteId);
     $this->sessionTransparent->expects($this->once())->method('getQuoteId')->willReturn($quoteId);
     $this->quoteRepository->expects($this->once())->method('get')->willReturn($quote);
     $this->paymentMethodManagementInterface->expects($this->once())->method('get')->willReturn($payment);
     $this->paymentMethodManagementInterface->expects($this->once())->method('set');
     $this->model->savePaymentInQuote($response);
 }
Пример #15
0
 /**
  * Retrieve quote model object
  *
  * @return \Magento\Quote\Model\Quote
  */
 public function getQuote()
 {
     if ($this->_quote === null) {
         $this->_quote = $this->quoteFactory->create();
         if ($this->getStoreId()) {
             if (!$this->getQuoteId()) {
                 $this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
                 $this->_quote->setIsActive(false);
                 $this->_quote->setStoreId($this->getStoreId());
                 $this->quoteRepository->save($this->_quote);
                 $this->setQuoteId($this->_quote->getId());
                 $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
             } else {
                 $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
                 $this->_quote->setStoreId($this->getStoreId());
             }
             if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
                 $customer = $this->customerRepository->getById($this->getCustomerId());
                 $this->_quote->assignCustomer($customer);
                 $this->quoteRepository->save($this->_quote);
             }
         }
         $this->_quote->setIgnoreOldQty(true);
         $this->_quote->setIsSuperMode(true);
     }
     return $this->_quote;
 }
 /**
  * {@inheritDoc}
  */
 public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
 {
     $address = $addressInformation->getShippingAddress();
     $billingAddress = $addressInformation->getBillingAddress();
     $carrierCode = $addressInformation->getShippingCarrierCode();
     $methodCode = $addressInformation->getShippingMethodCode();
     if (!$address->getCountryId()) {
         throw new StateException(__('Shipping address is not set'));
     }
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
     $this->validateQuote($quote);
     $quote->setIsMultiShipping(false);
     if ($billingAddress) {
         $quote->setBillingAddress($billingAddress);
     }
     try {
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save shipping information. Please check input data.'));
     }
     $shippingAddress = $quote->getShippingAddress();
     if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
         throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
     }
     /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
     $paymentDetails = $this->paymentDetailsFactory->create();
     $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
     $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
     return $paymentDetails;
 }
 /**
  * @param int $direction
  * @param string $expectedDirection
  * @dataProvider getListSuccessDataProvider
  */
 public function testGetListSuccess($direction, $expectedDirection)
 {
     $searchResult = $this->getMock('\\Magento\\Quote\\Api\\Data\\CartSearchResultsInterface', [], [], '', false);
     $searchCriteriaMock = $this->getMock('\\Magento\\Framework\\Api\\SearchCriteria', [], [], '', false);
     $cartMock = $this->getMock('Magento\\Payment\\Model\\Cart', [], [], '', false);
     $filterMock = $this->getMock('\\Magento\\Framework\\Api\\Filter', [], [], '', false);
     $pageSize = 10;
     $this->searchResultsDataFactory->expects($this->once())->method('create')->will($this->returnValue($searchResult));
     $searchResult->expects($this->once())->method('setSearchCriteria');
     $filterGroupMock = $this->getMock('\\Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false);
     $searchCriteriaMock->expects($this->any())->method('getFilterGroups')->will($this->returnValue([$filterGroupMock]));
     //addFilterGroupToCollection() checks
     $filterGroupMock->expects($this->any())->method('getFilters')->will($this->returnValue([$filterMock]));
     $filterMock->expects($this->once())->method('getField')->will($this->returnValue('store_id'));
     $filterMock->expects($this->any())->method('getConditionType')->will($this->returnValue('eq'));
     $filterMock->expects($this->once())->method('getValue')->will($this->returnValue('filter_value'));
     //back in getList()
     $this->quoteCollectionMock->expects($this->once())->method('getSize')->willReturn($pageSize);
     $searchResult->expects($this->once())->method('setTotalCount')->with($pageSize);
     $sortOrderMock = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->setMethods(['getField', 'getDirection'])->disableOriginalConstructor()->getMock();
     //foreach cycle
     $searchCriteriaMock->expects($this->once())->method('getSortOrders')->will($this->returnValue([$sortOrderMock]));
     $sortOrderMock->expects($this->once())->method('getField')->will($this->returnValue('id'));
     $sortOrderMock->expects($this->once())->method('getDirection')->will($this->returnValue($direction));
     $this->quoteCollectionMock->expects($this->once())->method('addOrder')->with('id', $expectedDirection);
     $searchCriteriaMock->expects($this->once())->method('getCurrentPage')->will($this->returnValue(1));
     $searchCriteriaMock->expects($this->once())->method('getPageSize')->will($this->returnValue(10));
     $this->quoteCollectionMock->expects($this->once())->method('setCurPage')->with(1);
     $this->quoteCollectionMock->expects($this->once())->method('setPageSize')->with(10);
     $this->extensionAttributesJoinProcessorMock->expects($this->once())->method('process')->with($this->isInstanceOf('\\Magento\\Quote\\Model\\ResourceModel\\Quote\\Collection'));
     $this->quoteCollectionMock->expects($this->once())->method('getItems')->willReturn([$cartMock]);
     $searchResult->expects($this->once())->method('setItems')->with([$cartMock]);
     $this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock));
 }
Пример #18
0
 /**
  * @param $quoteId
  *
  * @return Quote
  */
 public function reOrder($quoteId)
 {
     $quote = $this->_quoteRepository->get($quoteId);
     $quote->setIsActive(1)->setReservedOrderId(null);
     $this->_quoteRepository->save($quote);
     return $quote;
 }
 /**
  * Execute operation
  *
  * @param string $shippingMethod
  * @param Quote $quote
  * @return void
  * @throws \InvalidArgumentException
  */
 public function execute($shippingMethod, Quote $quote)
 {
     if (empty($shippingMethod)) {
         throw new \InvalidArgumentException('The "shippingMethod" field does not exists.');
     }
     if (!$quote->getIsVirtual()) {
         $shippingAddress = $quote->getShippingAddress();
         if ($shippingMethod !== $shippingAddress->getShippingMethod()) {
             $this->disabledQuoteAddressValidation($quote);
             $shippingAddress->setShippingMethod($shippingMethod);
             $shippingAddress->setCollectShippingRates(true);
             $quote->collectTotals();
             $this->quoteRepository->save($quote);
         }
     }
 }
Пример #20
0
 public function testGetCartForCustomer()
 {
     $customerId = 100;
     $cartMock = $this->getMock('\\Magento\\Quote\\Model\\Quote', [], [], '', false);
     $this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer')->with($customerId)->willReturn($cartMock);
     $this->assertEquals($cartMock, $this->model->getCartForCustomer($customerId));
 }
Пример #21
0
 /**
  * {@inheritDoc}
  */
 public function getPaymentMethods($quoteId, $country = null)
 {
     // get quote from quoteId
     $quote = $this->_quoteRepository->getActive($quoteId);
     $store = $quote->getStore();
     $paymentMethods = $this->_addHppMethodsToConfig($store, $country);
     return $paymentMethods;
 }
 /**
  * {@inheritDoc}
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
 {
     $address = $addressInformation->getShippingAddress();
     $carrierCode = $addressInformation->getShippingCarrierCode();
     $methodCode = $addressInformation->getShippingMethodCode();
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     $this->validateQuote($quote);
     $quote->setIsMultiShipping(false);
     $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
     $sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
     $customerAddressId = $address->getCustomerAddressId();
     $this->addressValidator->validate($address);
     $quote->setShippingAddress($address);
     $address = $quote->getShippingAddress();
     if ($customerAddressId) {
         $addressData = $this->addressRepository->getById($customerAddressId);
         $address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
     }
     $billingAddress = $addressInformation->getBillingAddress();
     if ($billingAddress) {
         $quote->setBillingAddress($billingAddress);
     }
     $address->setSaveInAddressBook($saveInAddressBook);
     $address->setSameAsBilling($sameAsBilling);
     $address->setCollectShippingRates(true);
     if (!$address->getCountryId()) {
         throw new StateException(__('Shipping address is not set'));
     }
     $address->setShippingMethod($carrierCode . '_' . $methodCode);
     try {
         $this->totalsCollector->collectAddressTotals($quote, $address);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save address. Please check input data.'));
     }
     if (!$address->getShippingRateByCode($address->getShippingMethod())) {
         throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
     }
     if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
         throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId()));
     }
     try {
         $address->save();
         $quote->collectTotals();
         $this->quoteRepository->save($quote);
     } catch (\Exception $e) {
         $this->logger->critical($e);
         throw new InputException(__('Unable to save shipping information. Please check input data.'));
     }
     /** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
     $paymentDetails = $this->paymentDetailsFactory->create();
     $paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
     $paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
     return $paymentDetails;
 }
 /**
  * {@inheritDoc}
  */
 public function get($cartId)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     if ($quote->isVirtual()) {
         throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.'));
     }
     /** @var \Magento\Quote\Model\Quote\Address $address */
     return $quote->getShippingAddress();
 }
Пример #24
0
 /**
  * Fetch coupon info
  *
  * Controller Action
  */
 public function execute()
 {
     $coupon_id = $this->getRequest()->getParam('id');
     if (!empty($coupon_id)) {
         $response = $this->coreModel->validCoupon($coupon_id);
     } else {
         $response = $this->getArrayErrorResponse();
     }
     if ($response['status'] != 200 && $response['status'] != 201) {
         $response = $this->getArrayErrorResponse();
     }
     //save value to DiscountCoupon collect
     $this->_registry->register('mercadopago_discount_amount', (double) $response['response']['coupon_amount']);
     $quote = $this->_checkoutSession->getQuote();
     $this->quoteRepository->save($quote->collectTotals());
     $jsonData = json_encode($response);
     $this->getResponse()->setHeader('Content-type', 'application/json');
     $this->getResponse()->setBody($jsonData);
 }
Пример #25
0
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @return void
  */
 public function collect(\Magento\Quote\Model\Quote $quote)
 {
     if ($this->customerSession->isLoggedIn()) {
         $customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
         if ($defaultShipping = $customer->getDefaultShipping()) {
             $address = $this->addressRepository->getById($defaultShipping);
             if ($address) {
                 /** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */
                 $estimatedAddress = $this->estimatedAddressFactory->create();
                 $estimatedAddress->setCountryId($address->getCountryId());
                 $estimatedAddress->setPostcode($address->getPostcode());
                 $estimatedAddress->setRegion((string) $address->getRegion()->getRegion());
                 $estimatedAddress->setRegionId($address->getRegionId());
                 $this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress);
                 $this->quoteRepository->save($quote);
             }
         }
     }
 }
Пример #26
0
 /**
  * {@inheritdoc}
  */
 public function remove($cartId)
 {
     /** @var  \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     if (!$quote->getItemsCount()) {
         throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
     }
     $quote->getShippingAddress()->setCollectShippingRates(true);
     try {
         $quote->setCouponCode('');
         $this->quoteRepository->save($quote->collectTotals());
     } catch (\Exception $e) {
         throw new CouldNotDeleteException(__('Could not delete coupon code'));
     }
     if ($quote->getCouponCode() != '') {
         throw new CouldNotDeleteException(__('Could not delete coupon code'));
     }
     return true;
 }
 /**
  * @param \PHPUnit_Framework_MockObject_MockObject $quoteMock
  * @param array $details
  */
 private function updateQuoteStep(\PHPUnit_Framework_MockObject_MockObject $quoteMock, array $details)
 {
     $quoteMock->expects(self::once())->method('setMayEditShippingAddress')->with(false);
     $quoteMock->expects(self::once())->method('setMayEditShippingMethod')->with(true);
     $quoteMock->expects(self::exactly(2))->method('getShippingAddress')->willReturn($this->shippingAddressMock);
     $quoteMock->expects(self::exactly(2))->method('getBillingAddress')->willReturn($this->billingAddressMock);
     $this->updateQuoteAddressStep($quoteMock, $details);
     $this->disabledQuoteAddressValidationStep();
     $quoteMock->expects(self::once())->method('collectTotals');
     $this->quoteRepositoryMock->expects(self::once())->method('save')->with($quoteMock);
 }
Пример #28
0
 public function testSaveCheckoutMethod()
 {
     $this->assertEquals(['error' => -1, 'message' => 'Invalid data'], $this->onepage->saveCheckoutMethod(null));
     /** @var \Magento\Quote\Model\Quote|\PHPUnit_Framework_MockObject_MockObject $quoteMock */
     $quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', ['setCheckoutMethod', '__wakeup'], [], '', false);
     $quoteMock->expects($this->once())->method('setCheckoutMethod')->with('someMethod')->will($this->returnSelf());
     $this->quoteRepositoryMock->expects($this->once())->method('save')->with($quoteMock);
     $this->checkoutSessionMock->expects($this->once())->method('setStepData')->with('billing', 'allow', true);
     $this->onepage->setQuote($quoteMock);
     $this->assertEquals([], $this->onepage->saveCheckoutMethod('someMethod'));
 }
Пример #29
0
 /**
  * Loads customer, quote and quote item by request params
  *
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _initData()
 {
     $this->_customerId = (int) $this->getRequest()->getParam('customer_id');
     if (!$this->_customerId) {
         throw new \Magento\Framework\Exception\LocalizedException(__('No customer ID defined.'));
     }
     $quoteItemId = (int) $this->getRequest()->getParam('id');
     $websiteId = (int) $this->getRequest()->getParam('website_id');
     try {
         $this->_quote = $this->quoteRepository->getForCustomer($this->_customerId);
     } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
         $this->_quote = $this->quoteFactory->create();
     }
     $this->_quote->setWebsite($this->_objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getWebsite($websiteId));
     $this->_quoteItem = $this->_quote->getItemById($quoteItemId);
     if (!$this->_quoteItem) {
         throw new LocalizedException(__('Please correct the quote items and try again.'));
     }
     return $this;
 }
 /**
  * {@inheritDoc}
  */
 public function estimateByAddressId($cartId, $addressId)
 {
     /** @var \Magento\Quote\Model\Quote $quote */
     $quote = $this->quoteRepository->getActive($cartId);
     // no methods applicable for empty carts or carts with virtual products
     if ($quote->isVirtual() || 0 == $quote->getItemsCount()) {
         return [];
     }
     $address = $this->addressRepository->getById($addressId);
     return $this->getEstimatedRates($quote, $address->getCountryId(), $address->getPostcode(), $address->getRegionId(), $address->getRegion());
 }