Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function getAddress($cartId)
 {
     $storeId = $this->storeManager->getStore()->getId();
     /** @var  \Magento\Sales\Model\Quote\Address $address */
     $address = $this->quoteLoader->load($cartId, $storeId)->getBillingAddress();
     return $this->addressConverter->convertModelToDataObject($address);
 }
Esempio n. 2
0
 /**
  * {@inheritdoc}
  */
 public function setAddress($cartId, $addressData)
 {
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     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);
     $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();
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function set(\Magento\Checkout\Service\V1\Data\Cart\PaymentMethod $method, $cartId)
 {
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     $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();
 }
Esempio n. 4
0
 /**
  * {@inheritdoc}
  */
 public function setMethod($cartId, $carrierCode, $methodCode)
 {
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     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;
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function get($cartId)
 {
     $storeId = $this->storeManager->getStore()->getId();
     /** @var  \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $storeId);
     $data = [Coupon::COUPON_CODE => $quote->getCouponCode()];
     $output = $this->couponBuilder->populateWithArray($data)->create();
     return $output;
 }
Esempio n. 6
0
 /**
  * {@inheritdoc}
  */
 public function getAddress($cartId)
 {
     $storeId = $this->storeManager->getStore()->getId();
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $storeId);
     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);
 }
Esempio n. 7
0
 /**
  * {@inheritdoc}
  */
 public function getList($cartId)
 {
     $output = [];
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     foreach ($this->methodList->getAvailableMethods($quote) as $method) {
         $output[] = $this->paymentMethodConverter->toDataObject($method);
     }
     return $output;
 }
Esempio n. 8
0
 /**
  * {@inheritdoc}
  */
 public function setAddress($cartId, $addressData)
 {
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId());
     /** @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();
 }
Esempio n. 9
0
 /**
  * {@inheritdoc}
  */
 public function delete($cartId)
 {
     $storeId = $this->storeManager->getStore()->getId();
     /** @var  \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $storeId);
     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;
 }
Esempio n. 10
0
 /**
  * {@inheritdoc}
  */
 public function getList($cartId)
 {
     $output = [];
     $storeId = $this->storeManager->getStore()->getId();
     /** @var \Magento\Sales\Model\Quote $quote */
     $quote = $this->quoteLoader->load($cartId, $storeId);
     // 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;
 }