/** * Retrieve customer cart quote object model * * @return \Magento\Sales\Model\Quote */ public function getCustomerCart() { if (!is_null($this->_cart)) { return $this->_cart; } $this->_cart = $this->_objectManager->create('Magento\\Sales\\Model\\Quote'); $customerId = (int) $this->getSession()->getCustomerId(); if ($customerId) { $this->_cart->setStore($this->getSession()->getStore())->loadByCustomer($customerId); if (!$this->_cart->getId()) { $customerData = $this->_customerAccountService->getCustomer($customerId); $this->_cart->assignCustomer($customerData); $this->_cart->save(); } } return $this->_cart; }
public function testPopulateBeforeSaveData() { /** Preconditions */ $customerId = 1; $customerAddressId = 1; $this->_address->setQuote($this->_quote); $this->assertNotEquals($customerId, $this->_address->getCustomerId(), "Precondition failed: Customer ID was not set."); $this->assertNotEquals(1, $this->_address->getQuoteId(), "Precondition failed: Quote ID was not set."); $this->assertNotEquals($customerAddressId, $this->_address->getCustomerAddressId(), "Precondition failed: Customer address ID was not set."); /** @var \Magento\Customer\Service\V1\Data\AddressBuilder $addressBuilder */ $addressBuilder = Bootstrap::getObjectManager()->create('Magento\\Customer\\Service\\V1\\Data\\AddressBuilder'); $customerAddressData = $addressBuilder->setId($customerAddressId)->create(); $this->_address->setCustomerAddressData($customerAddressData); $this->_address->save(); $this->assertEquals($customerId, $this->_address->getCustomerId()); $this->assertEquals($this->_quote->getId(), $this->_address->getQuoteId()); $this->assertEquals($customerAddressId, $this->_address->getCustomerAddressId()); }
/** * Retrieve quote model object * * @return \Magento\Sales\Model\Quote */ public function getQuote() { if (is_null($this->_quote)) { $this->_quote = $this->_quoteFactory->create(); if ($this->getStoreId() && $this->getQuoteId()) { $this->_quote->setStoreId($this->getStoreId())->load($this->getQuoteId()); } elseif ($this->getStoreId() && $this->hasCustomerId()) { $customerGroupId = $this->_scopeConfig->getValue(self::XML_PATH_DEFAULT_CREATEACCOUNT_GROUP, \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $this->_quote->setStoreId($this->getStoreId())->setCustomerGroupId($customerGroupId)->setIsActive(false)->save(); $this->setQuoteId($this->_quote->getId()); try { $customerData = $this->_customerService->getCustomer($this->getCustomerId()); $this->_quote->assignCustomer($customerData); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { /** Customer does not exist */ } } $this->_quote->setIgnoreOldQty(true); $this->_quote->setIsSuperMode(true); } return $this->_quote; }
/** * Reserve order ID for specified quote and start checkout on PayPal * * @param string $returnUrl * @param string $cancelUrl * @param bool|null $button * @return string * @throws \Magento\Framework\Model\Exception */ public function start($returnUrl, $cancelUrl, $button = null) { $this->_quote->collectTotals(); if (!$this->_quote->getGrandTotal() && !$this->_quote->hasNominalItems()) { throw new \Magento\Framework\Model\Exception(__('PayPal can\'t process orders with a zero balance due. ' . 'To finish your purchase, please go through the standard checkout process.')); } $this->_quote->reserveOrderId()->save(); // prepare API $this->_getApi(); $solutionType = $this->_config->getMerchantCountry() == 'DE' ? \Magento\Paypal\Model\Config::EC_SOLUTION_TYPE_MARK : $this->_config->getConfigValue('solutionType'); $this->_api->setAmount($this->_quote->getBaseGrandTotal())->setCurrencyCode($this->_quote->getBaseCurrencyCode())->setInvNum($this->_quote->getReservedOrderId())->setReturnUrl($returnUrl)->setCancelUrl($cancelUrl)->setSolutionType($solutionType)->setPaymentAction($this->_config->getConfigValue('paymentAction')); if ($this->_giropayUrls) { list($successUrl, $cancelUrl, $pendingUrl) = $this->_giropayUrls; $this->_api->addData(['giropay_cancel_url' => $cancelUrl, 'giropay_success_url' => $successUrl, 'giropay_bank_txn_pending_url' => $pendingUrl]); } if ($this->_isBml) { $this->_api->setFundingSource('BML'); } $this->_setBillingAgreementRequest(); if ($this->_config->getConfigValue('requireBillingAddress') == PaypalConfig::REQUIRE_BILLING_ADDRESS_ALL) { $this->_api->setRequireBillingAddress(1); } // suppress or export shipping address if ($this->_quote->getIsVirtual()) { if ($this->_config->getConfigValue('requireBillingAddress') == PaypalConfig::REQUIRE_BILLING_ADDRESS_VIRTUAL) { $this->_api->setRequireBillingAddress(1); } $this->_api->setSuppressShipping(true); } else { $address = $this->_quote->getShippingAddress(); $isOverridden = 0; if (true === $address->validate()) { $isOverridden = 1; $this->_api->setAddress($address); } $this->_quote->getPayment()->setAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_SHIPPING_OVERRIDDEN, $isOverridden); $this->_quote->getPayment()->save(); } // add line items /** @var $cart \Magento\Payment\Model\Cart */ $cart = $this->_cartFactory->create(array('salesModel' => $this->_quote)); $this->_api->setPaypalCart($cart)->setIsLineItemsEnabled($this->_config->getConfigValue('lineItemsEnabled')); // add shipping options if needed and line items are available $cartItems = $cart->getAllItems(); if ($this->_config->getConfigValue('lineItemsEnabled') && $this->_config->getConfigValue('transferShippingOptions') && !empty($cartItems)) { if (!$this->_quote->getIsVirtual() && !$this->_quote->hasNominalItems()) { $options = $this->_prepareShippingOptions($address, true); if ($options) { $this->_api->setShippingOptionsCallbackUrl($this->_coreUrl->getUrl('*/*/shippingOptionsCallback', ['quote_id' => $this->_quote->getId()]))->setShippingOptions($options); } } } $this->_config->exportExpressCheckoutStyleSettings($this->_api); /* Temporary solution. @TODO: do not pass quote into Nvp model */ $this->_api->setQuote($this->_quote); $this->_api->callSetExpressCheckout(); $token = $this->_api->getToken(); $this->_redirectUrl = $button ? $this->_config->getExpressCheckoutStartUrl($token) : $this->_config->getPayPalBasicStartUrl($token); $payment = $this->_quote->getPayment(); $payment->unsAdditionalInformation(self::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT); // Set flag that we came from Express Checkout button if (!empty($button)) { $payment->setAdditionalInformation(self::PAYMENT_INFO_BUTTON, 1); } elseif ($payment->hasAdditionalInformation(self::PAYMENT_INFO_BUTTON)) { $payment->unsAdditionalInformation(self::PAYMENT_INFO_BUTTON); } $payment->save(); return $token; }
/** * Fetch base quote data and map it to DTO fields * * @param Quote $quote * @return array */ public function map(Quote $quote) { return [Cart::ID => $quote->getId(), Cart::STORE_ID => $quote->getStoreId(), Cart::CREATED_AT => $quote->getCreatedAt(), Cart::UPDATED_AT => $quote->getUpdatedAt(), Cart::CONVERTED_AT => $quote->getConvertedAt(), Cart::IS_ACTIVE => $quote->getIsActive(), Cart::IS_VIRTUAL => $quote->getIsVirtual(), Cart::ITEMS_COUNT => $quote->getItemsCount(), Cart::ITEMS_QUANTITY => $quote->getItemsQty(), Cart::CHECKOUT_METHOD => $quote->getCheckoutMethod(), Cart::RESERVED_ORDER_ID => $quote->getReservedOrderId(), Cart::ORIG_ORDER_ID => $quote->getOrigOrderId()]; }
/** * Declare quote model object * * @param \Magento\Sales\Model\Quote $quote * @return $this */ public function setQuote(\Magento\Sales\Model\Quote $quote) { $this->_quote = $quote; $this->setQuoteId($quote->getId()); return $this; }
/** * @param Quote $quote * @return $this */ public function replaceQuote($quote) { $this->_quote = $quote; $this->setQuoteId($quote->getId()); return $this; }
/** * Fetch base quote data and map it to DTO fields * * @param Quote $quote * @return array */ public function map(Quote $quote) { $this->cartBuilder->populateWithArray([Cart::ID => $quote->getId(), Cart::STORE_ID => $quote->getStoreId(), Cart::CREATED_AT => $quote->getCreatedAt(), Cart::UPDATED_AT => $quote->getUpdatedAt(), Cart::CONVERTED_AT => $quote->getConvertedAt(), Cart::IS_ACTIVE => $quote->getIsActive(), Cart::IS_VIRTUAL => $quote->getIsVirtual(), Cart::ITEMS_COUNT => $quote->getItemsCount(), Cart::ITEMS_QUANTITY => $quote->getItemsQty(), Cart::CHECKOUT_METHOD => $quote->getCheckoutMethod(), Cart::RESERVED_ORDER_ID => $quote->getReservedOrderId(), Cart::ORIG_ORDER_ID => $quote->getOrigOrderId()]); $this->customerBuilder->populateWithArray($this->customerMapper->map($quote)); $this->totalsBuilder->populateWithArray($this->totalsMapper->map($quote)); $items = []; foreach ($quote->getAllItems() as $item) { $items[] = $this->itemTotalsMapper->extractDto($item); } $this->totalsBuilder->setItems($items); $this->cartBuilder->setCustomer($this->customerBuilder->create()); $this->cartBuilder->setTotals($this->totalsBuilder->create()); $this->cartBuilder->setCurrency($this->currencyMapper->extractDto($quote)); return $this->cartBuilder->create(); }
/** * Set Quote object to Collection * * @param \Magento\Sales\Model\Quote $quote * @return $this */ public function setQuote($quote) { $this->_quote = $quote; $quoteId = $quote->getId(); if ($quoteId) { $this->addFieldToFilter('quote_id', $quote->getId()); } else { $this->_totalRecords = 0; $this->_setIsLoaded(true); } return $this; }