/** * Set new customer group to all his quotes * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var CustomerData $customerDataObject */ $customerDataObject = $observer->getEvent()->getCustomerDataObject(); /** @var CustomerData $origCustomerDataObject */ $origCustomerDataObject = $observer->getEvent()->getOrigCustomerDataObject(); if ($customerDataObject->getGroupId() !== $origCustomerDataObject->getGroupId()) { /** * It is needed to process customer's quotes for all websites * if customer accounts are shared between all of them */ /** @var $websites \Magento\Store\Model\Website[] */ $websites = $this->_config->isWebsiteScope() ? array($this->_storeManager->getWebsite($customerDataObject->getWebsiteId())) : $this->_storeManager->getWebsites(); foreach ($websites as $website) { $quote = $this->_quoteFactory->create(); $quote->setWebsite($website); $quote->loadByCustomer($customerDataObject->getId()); if ($quote->getId()) { $quote->setCustomerGroupId($customerDataObject->getGroupId()); $quote->collectTotals(); $quote->save(); } } } }
/** * Load quote with different methods * * @param string $loadMethod * @param string $loadField * @param int $identifier * @return Quote * @throws \Magento\Framework\Exception\NoSuchEntityException */ protected function loadQuote($loadMethod, $loadField, $identifier) { $quote = $this->quoteFactory->create(); $quote->setStoreId($this->storeManager->getStore()->getId())->{$loadMethod}($identifier); if (!$quote->getId() || !$quote->getIsActive()) { throw NoSuchEntityException::singleField($loadField, $identifier); } return $quote; }
/** * Get cart by id * * @param int $cartId * @return Quote * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function get($cartId) { $quote = $this->quoteFactory->create(); $quote->setStoreId($this->storeManager->getStore()->getId())->load($cartId); if (!$quote->getId() || !$quote->getIsActive()) { throw NoSuchEntityException::singleField('cartId', $cartId); } return $quote; }
/** * Return current active quote for persistent customer * * @return \Magento\Sales\Model\Quote */ protected function _getQuote() { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->_quoteFactory->create(); $quote->loadByCustomer($this->_persistentSession->getSession()->getCustomerId()); return $quote; }
/** * {@inheritdoc} */ protected function _prepareLayout() { $this->setId('customerViewAccordion'); $this->addItem('lastOrders', array('title' => __('Recent Orders'), 'ajax' => true, 'content_url' => $this->getUrl('customer/*/lastOrders', array('_current' => true)))); $customerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); $customer = $this->getCustomer($customerId); $websiteIds = $this->_shareConfig->getSharedWebsiteIds($customer->getWebsiteId()); // add shopping cart block of each website foreach ($websiteIds as $websiteId) { $website = $this->_storeManager->getWebsite($websiteId); // count cart items $cartItemsCount = $this->_quoteFactory->create()->setWebsite($website)->loadByCustomer($customerId)->getItemsCollection(false)->addFieldToFilter('parent_item_id', array('null' => true))->getSize(); // prepare title for cart $title = __('Shopping Cart - %1 item(s)', $cartItemsCount); if (count($websiteIds) > 1) { $title = __('Shopping Cart of %1 - %2 item(s)', $website->getName(), $cartItemsCount); } // add cart ajax accordion $this->addItem('shopingCart' . $websiteId, array('title' => $title, 'ajax' => true, 'content_url' => $this->getUrl('customer/*/viewCart', array('_current' => true, 'website_id' => $websiteId)))); } // count wishlist items $wishlistCount = $this->_itemsFactory->create()->addCustomerIdFilter($customerId)->addStoreData()->getSize(); // add wishlist ajax accordion $this->addItem('wishlist', array('title' => __('Wishlist - %1 item(s)', $wishlistCount), 'ajax' => true, 'content_url' => $this->getUrl('customer/*/viewWishlist', array('_current' => true)))); }
/** * Load quote data into $data * * @param object $data * @return $this */ public function addQuoteData($data) { $quoteId = $data->getQuoteId(); if (intval($quoteId) <= 0) { return $this; } $data->setQuoteData($this->_quoteFactory->create()->load($quoteId)); return $this; }
/** * Get the quote of the cart * * @return \Magento\Sales\Model\Quote */ protected function getQuote() { if (null === $this->quote) { $customerId = $this->getCustomerId(); $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds(); $this->quote = $this->_quoteFactory->create()->setSharedStoreIds($storeIds)->loadByCustomer($customerId); } return $this->quote; }
/** * Converting order object to quote object * * @param \Magento\Sales\Model\Order $order * @param null|\Magento\Sales\Model\Quote $quote * @return \Magento\Sales\Model\Quote */ public function toQuote(\Magento\Sales\Model\Order $order, $quote = null) { if (!$quote instanceof \Magento\Sales\Model\Quote) { $quote = $this->_quoteFactory->create(); } $quote->setStoreId($order->getStoreId())->setOrderId($order->getId()); $this->_objectCopyService->copyFieldsetToTarget('sales_convert_order', 'to_quote', $order, $quote); $this->_eventManager->dispatch('sales_convert_order_to_quote', array('order' => $order, 'quote' => $quote)); return $quote; }
/** * Get quote * * @return \Magento\Sales\Model\Quote */ protected function getQuote() { if (null == $this->quote) { $storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds(); $this->quote = $this->_quoteFactory->create()->setSharedStoreIds($storeIds); $currentCustomerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID); if (!empty($currentCustomerId)) { $this->quote->loadByCustomer($currentCustomerId); } } return $this->quote; }
/** * {@inheritdoc} */ public function assignCustomer($cartId, $customerId) { $storeId = $this->storeManager->getStore()->getId(); $quote = $this->quoteFactory->create()->load($cartId); if ($quote->getId() != $cartId || $quote->getStoreId() != $storeId) { throw new NoSuchEntityException('There is no cart with provided ID.'); } $customer = $this->customerRegistry->retrieve($customerId); if (!in_array($storeId, $customer->getSharedStoreIds())) { throw new StateException('Cannot assign customer to the given cart. The cart belongs to different store.'); } if ($quote->getCustomerId()) { throw new StateException('Cannot assign customer to the given cart. The cart is not anonymous.'); } $currentCustomerQuote = $this->quoteFactory->create()->loadByCustomer($customer); if ($currentCustomerQuote->getId()) { throw new StateException('Cannot assign customer to the given cart. Customer already has active cart.'); } $quote->setCustomer($customer); $quote->setCustomerIsGuest(0); $quote->save(); return true; }
/** * Restore last active quote * * @return bool True if quote restored successfully, false otherwise */ public function restoreQuote() { /** @var \Magento\Sales\Model\Order $order */ $order = $this->getLastRealOrder(); if ($order->getId()) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->_quoteFactory->create()->load($order->getQuoteId()); if ($quote->getId()) { $quote->setIsActive(1)->setReservedOrderId(null)->save(); $this->replaceQuote($quote)->unsLastRealOrderId(); $this->_eventManager->dispatch('restore_quote', array('order' => $order, 'quote' => $quote)); return true; } } return false; }
/** * Add cart info to collection * * @return $this */ public function addCartInfo() { foreach ($this->getItems() as $item) { $quote = $this->_quoteFactory->create()->loadByCustomer($item->getId()); if ($quote instanceof \Magento\Sales\Model\Quote) { $totals = $quote->getTotals(); $item->setTotal($totals['subtotal']->getValue()); $quoteItems = $this->_quoteItemFactory->create()->setQuoteFilter($quote->getId()); $quoteItems->load(); $item->setItems($quoteItems->count()); } else { $item->remove(); } } return $this; }
/** * Operate with order using information from Authorize.net. * Authorize order or authorize and capture it. * * @param \Magento\Sales\Model\Order $order * @return void * @throws \Magento\Framework\Model\Exception * @throws \Exception */ protected function _authOrder(\Magento\Sales\Model\Order $order) { try { $this->checkResponseCode(); $this->checkTransId(); } catch (\Exception $e) { //decline the order (in case of wrong response code) but don't return money to customer. $message = $e->getMessage(); $this->_declineOrder($order, $message, false); throw $e; } $response = $this->getResponse(); //create transaction. need for void if amount will not match. $payment = $order->getPayment(); $this->_fillPaymentByResponse($payment); $payment->addTransaction(\Magento\Sales\Model\Order\Payment\Transaction::TYPE_AUTH); // Set transaction approval message $message = __('Amount of %1 approved by payment gateway. Transaction ID: "%2".', $order->getBaseCurrency()->formatTxt($payment->getBaseAmountAuthorized()), $response->getXTransId()); $orderState = \Magento\Sales\Model\Order::STATE_PROCESSING; $orderStatus = $this->getConfigData('order_status'); if (!$orderStatus || $order->getIsVirtual()) { $orderStatus = $order->getConfig()->getStateDefaultStatus($orderState); } $order->setState($orderState, $orderStatus ? $orderStatus : true, $message, false)->save(); //match amounts. should be equals for authorization. //decline the order if amount does not match. if (!$this->_matchAmount($payment->getBaseAmountAuthorized())) { $message = __('Something went wrong: the paid amount doesn\'t match the order amount. Please correct this and try again.'); $this->_declineOrder($order, $message, true); throw new \Magento\Framework\Model\Exception($message); } //capture order using AIM if needed $this->_captureOrder($order); try { if (!$response->hasOrderSendConfirmation() || $response->getOrderSendConfirmation()) { $this->orderSender->send($order); } $this->_quoteFactory->create()->load($order->getQuoteId())->setIsActive(false)->save(); } catch (\Exception $e) { // do not cancel order if we couldn't send email } }
/** * 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; }
/** * Operate with order using information from silent post * * @param \Magento\Sales\Model\Order $order * @return void * @throws \Magento\Framework\Model\Exception */ protected function _processOrder(\Magento\Sales\Model\Order $order) { $response = $this->getResponse(); $payment = $order->getPayment(); $payment->setTransactionId($response->getPnref())->setIsTransactionClosed(0); $canSendNewOrderEmail = true; if ($response->getResult() == self::RESPONSE_CODE_FRAUDSERVICE_FILTER || $response->getResult() == self::RESPONSE_CODE_DECLINED_BY_FILTER) { $canSendNewOrderEmail = false; $fraudMessage = $this->_getFraudMessage() ? $response->getFraudMessage() : $response->getRespmsg(); $payment->setIsTransactionPending(true)->setIsFraudDetected(true)->setAdditionalInformation('paypal_fraud_filters', $fraudMessage); } if ($response->getAvsdata() && strstr(substr($response->getAvsdata(), 0, 2), 'N')) { $payment->setAdditionalInformation('paypal_avs_code', substr($response->getAvsdata(), 0, 2)); } if ($response->getCvv2match() && $response->getCvv2match() != 'Y') { $payment->setAdditionalInformation('paypal_cvv2_match', $response->getCvv2match()); } switch ($response->getType()) { case self::TRXTYPE_AUTH_ONLY: $payment->registerAuthorizationNotification($payment->getBaseAmountAuthorized()); break; case self::TRXTYPE_SALE: $payment->registerCaptureNotification($payment->getBaseAmountAuthorized()); break; default: break; } $order->save(); try { if ($canSendNewOrderEmail) { $this->orderSender->send($order); } $this->_quoteFactory->create()->load($order->getQuoteId())->setIsActive(false)->save(); } catch (\Exception $e) { throw new \Magento\Framework\Model\Exception(__('We cannot send the new order email.')); } }
/** * @return \Magento\Sales\Model\Quote */ protected function _createQuote() { return $this->_quoteFactory->create(); }