/**
  * Get payment country
  *
  * @param Quote $quote
  * @return int
  */
 public function getCountry(Quote $quote)
 {
     $address = $quote->isVirtual() ? $quote->getBillingAddress() : $quote->getShippingAddress();
     return $address ? $address->getCountry() : $this->directoryHelper->getDefaultCountry();
 }
 /**
  * Validate quote before submit
  *
  * @param Quote $quote
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function validateBeforeSubmit(QuoteEntity $quote)
 {
     if (!$quote->isVirtual()) {
         if ($quote->getShippingAddress()->validate() !== true) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Please check the shipping address information. %1', implode(' ', $quote->getShippingAddress()->validate())));
         }
         $method = $quote->getShippingAddress()->getShippingMethod();
         $rate = $quote->getShippingAddress()->getShippingRateByCode($method);
         if (!$quote->isVirtual() && (!$method || !$rate)) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Please specify a shipping method.'));
         }
     }
     if ($quote->getBillingAddress()->validate() !== true) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Please check the billing address information. %1', implode(' ', $quote->getBillingAddress()->validate())));
     }
     if (!$quote->getPayment()->getMethod()) {
         throw new \Magento\Framework\Exception\LocalizedException(__('Please select a valid payment method.'));
     }
     return $this;
 }
Beispiel #3
0
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @return void
  * @throws InputException
  */
 private function processShippingAssignment($quote)
 {
     // Shipping Assignments processing
     $extensionAttributes = $quote->getExtensionAttributes();
     if (!$quote->isVirtual() && $extensionAttributes && $extensionAttributes->getShippingAssignments()) {
         $shippingAssignments = $extensionAttributes->getShippingAssignments();
         if (count($shippingAssignments) > 1) {
             throw new InputException(__("Only 1 shipping assignment can be set"));
         }
         $this->shippingAssignmentPersister->save($quote, $shippingAssignments[0]);
     }
 }
 /**
  * Validate quote
  *
  * @param \Magento\Quote\Model\Quote $quote
  * @throws InputException
  * @throws NoSuchEntityException
  * @return void
  */
 protected function validateQuote(\Magento\Quote\Model\Quote $quote)
 {
     if ($quote->isVirtual()) {
         throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.'));
     }
     if (0 == $quote->getItemsCount()) {
         throw new InputException(__('Shipping method is not applicable for empty cart'));
     }
 }
Beispiel #5
0
 /**
  * Prepare quote for customer order submit
  *
  * @param Quote $quote
  * @return void
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function _prepareCustomerQuote($quote)
 {
     /** @var Quote $quote */
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     $customer = $this->customerRepository->getById($quote->getCustomerId());
     $hasDefaultBilling = (bool) $customer->getDefaultBilling();
     $hasDefaultShipping = (bool) $customer->getDefaultShipping();
     if ($shipping && !$shipping->getSameAsBilling() && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook())) {
         $shippingAddress = $shipping->exportCustomerAddress();
         if (!$hasDefaultShipping) {
             //Make provided address as default shipping address
             $shippingAddress->setIsDefaultShipping(true);
             $hasDefaultShipping = true;
         }
         $quote->addCustomerAddress($shippingAddress);
         $shipping->setCustomerAddressData($shippingAddress);
     }
     if (!$billing->getCustomerId() || $billing->getSaveInAddressBook()) {
         $billingAddress = $billing->exportCustomerAddress();
         if (!$hasDefaultBilling) {
             //Make provided address as default shipping address
             if (!$hasDefaultShipping) {
                 //Make provided address as default shipping address
                 $billingAddress->setIsDefaultShipping(true);
             }
             $billingAddress->setIsDefaultBilling(true);
         }
         $quote->addCustomerAddress($billingAddress);
         $billing->setCustomerAddressData($billingAddress);
     }
     if ($shipping && !$shipping->getCustomerId() && !$hasDefaultBilling) {
         $shipping->setIsDefaultBilling(true);
     }
 }
Beispiel #6
0
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @param int|null $customerId
  * @return \Magento\Quote\Model\Quote
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function prepareRegisteredCustomerQuote(\Magento\Quote\Model\Quote $quote, $customerId)
 {
     $billing = $quote->getBillingAddress();
     $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress();
     $customer = $this->customerRepository->getById($customerId);
     $isBillingAddressDefaultBilling = !$customer->getDefaultBilling();
     $isBillingAddressDefaultShipping = false;
     $isShippingAddressDefaultShipping = false;
     if ($shipping && !$customer->getDefaultShipping()) {
         $isShippingAddressDefaultShipping = true;
     } elseif (!$customer->getDefaultShipping()) {
         $isBillingAddressDefaultShipping = true;
     }
     if ($shipping && $shipping->getTelephone() && !$shipping->getSameAsBilling() && (!$shipping->getCustomerId() || $shipping->getSaveInAddressBook() || !$customer->getDefaultShipping())) {
         $address = $shipping->exportCustomerAddress();
         $address->setIsDefaultShipping($isShippingAddressDefaultShipping);
         $quote->addCustomerAddress($address);
     }
     if ($billing && $billing->getTelephone() && (!$customer->getDefaultBilling() || $billing->getSaveInAddressBook())) {
         $address = $billing->exportCustomerAddress();
         $address->setIsDefaultBilling($isBillingAddressDefaultBilling);
         $address->setIsDefaultShipping($isBillingAddressDefaultShipping);
         $quote->addCustomerAddress($address);
     }
     return $quote;
 }
 /**
  * @param \Magento\Quote\Model\Quote $quote
  * @return Address\Total
  */
 public function collectQuoteTotals(\Magento\Quote\Model\Quote $quote)
 {
     if ($quote->isVirtual()) {
         return $this->collectAddressTotals($quote, $quote->getBillingAddress());
     }
     return $this->collectAddressTotals($quote, $quote->getShippingAddress());
 }
 /**
  * Get payment country
  *
  * @param Quote $quote
  * @return int
  */
 public function getCountry(Quote $quote)
 {
     return $quote->isVirtual() ? $this->directoryHelper->getDefaultCountry() : $quote->getShippingAddress()->getCountry();
 }