/** * {@inheritdoc} */ public function setMethod($cartId, $carrierCode, $methodCode) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if (0 == $quote->getItemsCount()) { throw new InputException('Shipping method is not applicable for empty cart'); } if ($quote->isVirtual()) { throw new NoSuchEntityException('Cart contains virtual product(s) only. Shipping method is not applicable.'); } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new StateException('Shipping address is not set'); } $billingAddress = $quote->getBillingAddress(); if (!$billingAddress->getCountryId()) { throw new StateException('Billing address is not set'); } $shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode); if (!$shippingAddress->requestShippingRates()) { throw new NoSuchEntityException('Carrier with such method not found: ' . $carrierCode . ', ' . $methodCode); } try { $quote->collectTotals()->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Cannot set shipping method. ' . $e->getMessage()); } return true; }
/** * {@inheritdoc} */ public function set(\Magento\Checkout\Service\V1\Data\Cart\PaymentMethod $method, $cartId) { $quote = $this->quoteRepository->get($cartId); $payment = $this->paymentMethodBuilder->build($method, $quote); if ($quote->isVirtual()) { // check if billing address is set if (is_null($quote->getBillingAddress()->getCountryId())) { throw new InvalidTransitionException('Billing address is not set'); } $quote->getBillingAddress()->setPaymentMethod($payment->getMethod()); } else { // check if shipping address is set if (is_null($quote->getShippingAddress()->getCountryId())) { throw new InvalidTransitionException('Shipping address is not set'); } $quote->getShippingAddress()->setPaymentMethod($payment->getMethod()); } if (!$quote->isVirtual() && $quote->getShippingAddress()) { $quote->getShippingAddress()->setCollectShippingRates(true); } if (!$this->zeroTotalValidator->isApplicable($payment->getMethodInstance(), $quote)) { throw new InvalidTransitionException('The requested Payment Method is not available.'); } $quote->setTotalsCollectedFlag(false)->collectTotals()->save(); return $quote->getPayment()->getId(); }
/** * {@inheritdoc} */ public function setAddress($cartId, $addressData) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if ($quote->isVirtual()) { throw new NoSuchEntityException('Cart contains virtual product(s) only. Shipping address is not applicable'); } /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $this->quoteAddressFactory->create(); $this->addressValidator->validate($addressData); if ($addressData->getId()) { $address->load($addressData->getId()); } $address = $this->addressConverter->convertDataObjectToModel($addressData, $address); $address->setSameAsBilling(0); $address->setCollectShippingRates(true); $quote->setShippingAddress($address); $quote->setDataChanges(true); try { $quote->save(); } catch (\Exception $e) { $this->logger->logException($e); throw new InputException('Unable to save address. Please, check input data.'); } return $quote->getShippingAddress()->getId(); }
/** * {@inheritdoc} */ public function get($cartId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); $data = [Coupon::COUPON_CODE => $quote->getCouponCode()]; $output = $this->couponBuilder->populateWithArray($data)->create(); return $output; }
/** * {@inheritdoc} */ public function getList($cartId) { $output = []; /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); foreach ($this->methodList->getAvailableMethods($quote) as $method) { $output[] = $this->paymentMethodConverter->toDataObject($method); } return $output; }
/** * {@inheritdoc} */ public function getList($cartId) { $output = []; /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); /** @var \Magento\Sales\Model\Quote\Item $item */ foreach ($quote->getAllItems() as $item) { $output[] = $this->itemMapper->extractDto($item); } return $output; }
/** * {@inheritdoc} */ public function getAddress($cartId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if ($quote->isVirtual()) { throw new NoSuchEntityException('Cart contains virtual product(s) only. Shipping address is not applicable'); } /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $quote->getShippingAddress(); return $this->addressConverter->convertModelToDataObject($address); }
/** * {@inheritdoc} */ public function getTotals($cartId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); $this->totalsBuilder->populateWithArray($this->totalsMapper->map($quote)); $items = []; foreach ($quote->getAllItems() as $item) { $items[] = $this->itemTotalsMapper->extractDto($item); } $this->totalsBuilder->setItems($items); return $this->totalsBuilder->create(); }
/** * {@inheritdoc} */ public function getItemMessage($cartId, $itemId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if (!($item = $quote->getItemById($itemId))) { throw new NoSuchEntityException('There is no item with provided id in the cart'); } $messageId = $item->getGiftMessageId(); if (!$messageId) { return null; } /** @var \Magento\GiftMessage\Model\Message $model */ $model = $this->messageFactory->create()->load($messageId); return $this->messageMapper->extractDto($model); }
/** * {@inheritdoc} */ public function getTotals($cartId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); $this->totalsBuilder->populateWithArray($this->totalsMapper->map($quote)); $this->totalsBuilder->setItems($this->fetchItemTotalsData($quote)); return $this->totalsBuilder->create(); }
/** * {@inheritdoc} */ public function order($cartId) { $quote = $this->quoteRepository->get($cartId); /** @var \Magento\Sales\Model\Service\Quote $quoteService */ $quoteService = $this->quoteServiceFactory->create(['quote' => $quote]); $order = $quoteService->submitOrderWithDataObject(); return $order->getId(); }
/** * {@inheritdoc} */ public function delete($cartId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if (!$quote->getItemsCount()) { throw new NoSuchEntityException("Cart {$cartId} doesn't contain products"); } $quote->getShippingAddress()->setCollectShippingRates(true); try { $quote->setCouponCode(''); $quote->collectTotals()->save(); } catch (\Exception $e) { throw new CouldNotDeleteException('Could not delete coupon code'); } if ($quote->getCouponCode() != '') { throw new CouldNotDeleteException('Could not delete coupon code'); } return true; }
/** * {@inheritdoc} */ public function setAddress($cartId, $addressData) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $this->quoteAddressFactory->create(); $this->addressValidator->validate($addressData); if ($addressData->getId()) { $address->load($addressData->getId()); } $address = $this->addressConverter->convertDataObjectToModel($addressData, $address); $quote->setBillingAddress($address); $quote->setDataChanges(true); try { $quote->save(); } catch (\Exception $e) { $this->logger->logException($e); throw new InputException('Unable to save address. Please, check input data.'); } return $quote->getBillingAddress()->getId(); }
/** * {@inheritdoc} */ public function setForItem($cartId, \Magento\GiftMessage\Service\V1\Data\Message $giftMessage, $itemId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); if (!($item = $quote->getItemById($itemId))) { throw new NoSuchEntityException("There is no product with provided itemId: {$itemId} in the cart"); } if ($item->getIsVirtual()) { throw new InvalidTransitionException('Gift Messages is not applicable for virtual products'); } $this->setMessage($quote, 'quote_item', $giftMessage, $itemId); return true; }
/** * {@inheritdoc} */ public function removeItem($cartId, $itemId) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); $quoteItem = $quote->getItemById($itemId); if (!$quoteItem) { throw new NoSuchEntityException("Cart {$cartId} doesn't contain item {$itemId}"); } try { $quote->removeItem($itemId); $quote->collectTotals()->save(); } catch (\Exception $e) { throw new CouldNotSaveException('Could not remove item from quote'); } return true; }
/** * {@inheritdoc} */ public function getList($cartId) { $output = []; /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); // no methods applicable for empty carts or carts with virtual products if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new StateException('Shipping address not set.'); } $shippingAddress->collectShippingRates(); $shippingRates = $shippingAddress->getGroupedAllShippingRates(); foreach ($shippingRates as $carrierRates) { foreach ($carrierRates as $rate) { $output[] = $this->converter->modelToDataObject($rate, $quote->getQuoteCurrencyCode()); } } return $output; }
/** * {@inheritdoc} */ public function getAddress($cartId) { /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $this->quoteRepository->get($cartId)->getBillingAddress(); return $this->addressConverter->convertModelToDataObject($address); }
/** * {@inheritdoc} */ public function getCart($cartId) { $quote = $this->quoteRepository->get($cartId); return $this->cartMapper->map($quote); }