public function testCollect() { $this->shippingAssignment->expects($this->exactly(3))->method('getShipping')->willReturn($this->shipping); $this->shipping->expects($this->exactly(2))->method('getAddress')->willReturn($this->address); $this->shipping->expects($this->once())->method('getMethod')->willReturn('flatrate'); $this->shippingAssignment->expects($this->atLeastOnce())->method('getItems')->willReturn([$this->cartItem]); $this->freeShipping->expects($this->once())->method('isFreeShipping')->with($this->quote, [$this->cartItem])->willReturn(true); $this->address->expects($this->once())->method('setFreeShipping')->with(true); $this->total->expects($this->atLeastOnce())->method('setTotalAmount'); $this->total->expects($this->atLeastOnce())->method('setBaseTotalAmount'); $this->cartItem->expects($this->atLeastOnce())->method('getProduct')->willReturnSelf(); $this->cartItem->expects($this->atLeastOnce())->method('isVirtual')->willReturn(false); $this->cartItem->expects($this->once())->method('getParentItem')->willReturn(false); $this->cartItem->expects($this->once())->method('getHasChildren')->willReturn(false); $this->cartItem->expects($this->once())->method('getWeight')->willReturn(2); $this->cartItem->expects($this->atLeastOnce())->method('getQty')->willReturn(2); $this->address->expects($this->once())->method('getFreeShipping')->willReturn(true); $this->cartItem->expects($this->once())->method('setRowWeight')->with(0); $this->address->expects($this->once())->method('setItemQty')->with(2); $this->address->expects($this->atLeastOnce())->method('setWeight'); $this->address->expects($this->atLeastOnce())->method('setFreeMethodWeight'); $this->address->expects($this->once())->method('collectShippingRates'); $this->address->expects($this->once())->method('getAllShippingRates')->willReturn([$this->rate]); $this->rate->expects($this->once())->method('getCode')->willReturn('flatrate'); $this->quote->expects($this->once())->method('getStore')->willReturn($this->store); $this->rate->expects($this->atLeastOnce())->method('getPrice')->willReturn(5); $this->priceCurrency->expects($this->once())->method('convert')->with(5, $this->store)->willReturn(5); $this->total->expects($this->once())->method('setShippingAmount')->with(5); $this->rate->expects($this->once())->method('getCarrierTitle')->willReturn('Carrier title'); $this->rate->expects($this->once())->method('getMethodTitle')->willReturn('Method title'); $this->address->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title'); $this->address->expects($this->once())->method('getShippingDescription')->willReturn('Carrier title - Method title'); $this->total->expects($this->once())->method('setShippingDescription')->with('Carrier title - Method title'); $this->shippingModel->collect($this->quote, $this->shippingAssignment, $this->total); }
protected function setUp() { $this->markTestIncomplete(); $this->messageManager = $this->getMockForAbstractClass('Magento\\Framework\\Message\\ManagerInterface'); $this->config = $this->getMock('Magento\\Paypal\\Model\\Config', [], [], '', false); $this->request = $this->getMock('Magento\\Framework\\App\\Request\\Http', [], [], '', false); $this->quote = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false); $this->quote->expects($this->any())->method('hasItems')->will($this->returnValue(true)); $this->redirect = $this->getMockForAbstractClass('Magento\\Framework\\App\\Response\\RedirectInterface'); $this->response = $this->getMock('Magento\\Framework\\App\\Response\\Http', [], [], '', false); $this->customerData = $this->getMock('Magento\\Customer\\Api\\Data\\CustomerInterface', [], [], '', false); $this->checkout = $this->getMock('Magento\\Paypal\\Model\\Express\\Checkout', [], [], '', false); $this->customerSession = $this->getMock('Magento\\Customer\\Model\\Session', [], [], '', false); $this->customerSession->expects($this->any())->method('getCustomerDataObject')->will($this->returnValue($this->customerData)); $this->checkoutSession = $this->getMock('Magento\\Checkout\\Model\\Session', [], [], '', false); $this->checkoutFactory = $this->getMock('Magento\\Paypal\\Model\\Express\\Checkout\\Factory', [], [], '', false); $this->checkoutFactory->expects($this->any())->method('create')->will($this->returnValue($this->checkout)); $this->checkoutSession->expects($this->any())->method('getQuote')->will($this->returnValue($this->quote)); $this->session = $this->getMock('Magento\\Framework\\Session\\Generic', [], [], '', false); $objectManager = $this->getMock('Magento\\Framework\\ObjectManagerInterface'); $this->objectManagerCallback = function ($className) { if ($className == 'Magento\\Paypal\\Model\\Config') { return $this->config; } return $this->getMock($className, [], [], '', false); }; $objectManager->expects($this->any())->method('get')->will($this->returnCallback(function ($className) { return call_user_func($this->objectManagerCallback, $className); })); $objectManager->expects($this->any())->method('create')->will($this->returnCallback(function ($className) { return call_user_func($this->objectManagerCallback, $className); })); $helper = new ObjectManagerHelper($this); $this->model = $helper->getObject('\\Magento\\\\Paypal\\Controller\\Express\\' . $this->name, ['messageManager' => $this->messageManager, 'response' => $this->response, 'redirect' => $this->redirect, 'request' => $this->request, 'customerSession' => $this->customerSession, 'checkoutSession' => $this->checkoutSession, 'checkoutFactory' => $this->checkoutFactory, 'paypalSession' => $this->session, 'objectManager' => $objectManager]); }
/** * @param \Magento\Quote\Model\Quote $quote * @param array $details * @return $this * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function populateQuoteAddress($quote, $details) { // import shipping address $exportedShippingAddress = isset($details['shippingAddress']) ? $details['shippingAddress'] : null; if (!$quote->getIsVirtual()) { $shippingAddress = $quote->getShippingAddress(); if ($exportedShippingAddress) { $this->importAddressData($shippingAddress, $exportedShippingAddress); } // PayPal doesn't provide detailed shipping info: prefix, suffix $shippingAddress->setLastname($details['lastName']); $shippingAddress->setFirstname($details['firstName']); $shippingAddress->setEmail($details['email']); $shippingAddress->setCollectShippingRates(true); } $exportedBillingAddress = isset($details['billingAddress']) ? $details['billingAddress'] : null; $billingAddress = $quote->getBillingAddress(); if ($exportedBillingAddress) { $this->importBillingAddressData($billingAddress, $exportedBillingAddress); $billingAddress->setFirstname($details['firstName']); $billingAddress->setLastname($details['lastName']); $billingAddress->setEmail($details['email']); } elseif ($billingAddress->getEmail() == null) { $this->importAddressData($billingAddress, $exportedShippingAddress); $billingAddress->setFirstname($details['firstName']); $billingAddress->setLastname($details['lastName']); $billingAddress->setEmail($details['email']); } return $this; }
/** * {@inheritDoc} */ public function isFreeShipping(\Magento\Quote\Model\Quote $quote, $items) { /** @var \Magento\Quote\Api\Data\CartItemInterface[] $items */ if (!count($items)) { return false; } $addressFreeShipping = true; $store = $this->storeManager->getStore($quote->getStoreId()); $this->calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode()); /** @var \Magento\Quote\Api\Data\CartItemInterface $item */ foreach ($items as $item) { if ($item->getNoDiscount()) { $addressFreeShipping = false; $item->setFreeShipping(false); continue; } /** Child item discount we calculate for parent */ if ($item->getParentItemId()) { continue; } $this->calculator->processFreeShipping($item); $itemFreeShipping = (bool) $item->getFreeShipping(); $addressFreeShipping = $addressFreeShipping && $itemFreeShipping; /** Parent free shipping we apply to all children*/ $this->applyToChildren($item, $itemFreeShipping); } return $addressFreeShipping; }
public function aroundAddItem(\Magento\Quote\Model\Quote $subject, \Closure $proceed, \Magento\Quote\Model\Quote\Item $item) { $product = $item->getProduct(); $attribute = $product->getResource()->getAttribute('enable_subscription'); $value = null; if ($attribute) { $value = $attribute->getFrontend()->getValue($product); } $flag = 0; foreach ($subject->getAllVisibleItems() as $item) { $itemAttrValue = null; $itemProduct = $item->getProduct(); $itemAttr = $itemProduct->getResource()->getAttribute('enable_subscription'); if ($itemAttr) { if ($itemAttr->getFrontend()->getValue($itemProduct)) { $flag = 1; } } } if ($value && $subject->hasItems() || $flag) { throw new \Magento\Framework\Exception\LocalizedException(__('Nominal item can be purchased standalone only.')); } $proceed($item); return $subject; }
/** * Push quote contents to given tracker * * @param \Magento\Quote\Model\Quote $quote * @param \Henhed\Piwik\Model\Tracker $tracker * @return \Henhed\Piwik\Helper\Tracker */ public function addQuote(Quote $quote, TrackerModel $tracker) { foreach ($quote->getAllVisibleItems() as $item) { $this->addQuoteItem($item, $tracker); } $this->addQuoteTotal($quote, $tracker); return $this; }
/** * If module is enabled, don't run collect totals for shipping * * Tax calculation for shipping is handled in this class * @see \ClassyLlama\AvaTax\Model\Tax\Sales\Total\Quote\Tax::collect() * Since this extension doesn't support applying discounts *after* tax, we don't need to run a separate collect * process. * * @param \Magento\Tax\Model\Sales\Total\Quote\Shipping $subject * @param \Closure $proceed * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment * @param \Magento\Quote\Model\Quote\Address\Total $total * @return mixed */ public function aroundCollect(\Magento\Tax\Model\Sales\Total\Quote\Shipping $subject, \Closure $proceed, \Magento\Quote\Model\Quote $quote, \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment, \Magento\Quote\Model\Quote\Address\Total $total) { $storeId = $quote->getStoreId(); // If quote is virtual, getShipping will return billing address, so no need to check if quote is virtual $address = $shippingAssignment->getShipping()->getAddress(); if (!$this->config->isModuleEnabled($storeId) || $this->config->getTaxMode($storeId) == Config::TAX_MODE_NO_ESTIMATE_OR_SUBMIT || !$this->config->isAddressTaxable($address, $storeId)) { return $proceed($quote, $shippingAssignment, $total); } }
public function testSuggestItemsQty() { $data = [[], ['qty' => -2], ['qty' => 3], ['qty' => 3.5], ['qty' => 5], ['qty' => 4]]; $this->quoteMock->expects($this->any())->method('getItemById')->will($this->returnValueMap([[2, $this->prepareQuoteItemMock(2)], [3, $this->prepareQuoteItemMock(3)], [4, $this->prepareQuoteItemMock(4)], [5, $this->prepareQuoteItemMock(5)]])); $this->stockState->expects($this->at(0))->method('suggestQty')->will($this->returnValue(3.0)); $this->stockState->expects($this->at(1))->method('suggestQty')->will($this->returnValue(3.5)); $this->checkoutSessionMock->expects($this->any())->method('getQuote')->will($this->returnValue($this->quoteMock)); $this->assertSame([[], ['qty' => -2], ['qty' => 3.0, 'before_suggest_qty' => 3.0], ['qty' => 3.5, 'before_suggest_qty' => 3.5], ['qty' => 5], ['qty' => 4]], $this->cart->suggestItemsQty($data)); }
/** * Check whether payment method is applicable to quote * * @param PaymentMethodChecksInterface $paymentMethod * @param \Magento\Quote\Model\Quote $quote * @return bool */ public function isApplicable(PaymentMethodChecksInterface $paymentMethod, Quote $quote) { $total = $quote->getBaseGrandTotal(); $minTotal = $paymentMethod->getConfigData(self::MIN_ORDER_TOTAL); $maxTotal = $paymentMethod->getConfigData(self::MAX_ORDER_TOTAL); if (!empty($minTotal) && $total < $minTotal || !empty($maxTotal) && $total > $maxTotal) { return false; } return true; }
/** * Gets \Magento\Quote\Model\Quote\Item from \Magento\Quote\Model\Quote by product id * * @param \Magento\Quote\Model\Quote $quote * @param mixed $productId * @return \Magento\Quote\Model\Quote\Item|null */ private function _getQuoteItemIdByProductId(\Magento\Quote\Model\Quote $quote, $productId) { /** @var $quoteItems \Magento\Quote\Model\Quote\Item[] */ $quoteItems = $quote->getAllItems(); foreach ($quoteItems as $quoteItem) { if ($productId == $quoteItem->getProductId()) { return $quoteItem; } } return null; }
public function testInitWithNonDecimalQty() { $quoteItemMock = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Item', ['getStockId', 'getIsQtyDecimal', '__wakeup'], [], '', false); $this->productMock->expects($this->once())->method('getId')->will($this->returnSelf()); $this->productMock->expects($this->never())->method('setIsQtyDecimal'); $this->productMock->expects($this->once())->method('setCartQty')->will($this->returnSelf()); $this->configMock->expects($this->exactly(2))->method('getQty')->will($this->returnValue(10)); $this->configMock->expects($this->once())->method('setQty')->will($this->returnSelf()); $this->quoteMock->expects($this->once())->method('addProduct')->will($this->returnValue($quoteItemMock)); $this->assertInstanceOf('Magento\\Quote\\Model\\Quote\\Item', $this->model->init($this->quoteMock, $this->productMock, $this->configMock)); }
/** * @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]); } }
/** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Model\Quote\Address\Total $total * @return array */ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total) { $enabled = $this->helperData->isModuleEnabled(); $minimumOrderAmount = $this->helperData->getMinimumOrderAmount(); $subtotal = $quote->getSubtotal(); $fee = $quote->getFee(); if ($enabled && $minimumOrderAmount <= $subtotal && $fee) { return ['code' => 'fee', 'title' => 'Custom Fee', 'value' => $fee]; } else { return array(); } }
/** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Quote\Model\Quote\Address\Total $total * @return array */ public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total) { $enabled = $this->helperData->isModuleEnabled(); $minimumOrderAmount = $this->helperData->getMinimumOrderAmount(); $subtotal = $quote->getSubtotal(); $fee = $quote->getFee(); $result = []; if ($enabled && $minimumOrderAmount <= $subtotal && $fee) { $result = ['code' => 'fee', 'title' => $this->helperData->getFeeLabel(), 'value' => $fee]; } return $result; }
/** * Make sure addresses will be saved without validation errors * * @param Quote $quote * @return void */ protected function disabledQuoteAddressValidation(Quote $quote) { $billingAddress = $quote->getBillingAddress(); $billingAddress->setShouldIgnoreValidation(true); if (!$quote->getIsVirtual()) { $shippingAddress = $quote->getShippingAddress(); $shippingAddress->setShouldIgnoreValidation(true); if (!$billingAddress->getEmail()) { $billingAddress->setSameAsBilling(1); } } }
protected function setUp() { $objectManager = new \Magento\TestFramework\Helper\ObjectManager($this); $this->priceCurrency = $this->getMockBuilder('Magento\\Framework\\Pricing\\PriceCurrencyInterface')->getMock(); $this->store = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $this->quote = $this->getMockBuilder('Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['getStore', '__wakeup', 'getCustomerTaxClassId'])->getMock(); $this->quote->expects($this->any())->method('getStore')->will($this->returnValue($this->store)); $checkoutSession = $this->getMockBuilder('\\Magento\\Checkout\\Model\\Session')->disableOriginalConstructor()->setMethods(['getQuote', '__wakeup'])->getMock(); $checkoutSession->expects($this->any())->method('getQuote')->will($this->returnValue($this->quote)); $this->taxHelper = $this->getMockBuilder('\\Magento\\Tax\\Helper\\Data')->disableOriginalConstructor()->setMethods(['getShippingPrice', 'displayShippingPriceIncludingTax', 'displayShippingBothPrices'])->getMock(); $this->priceObj = $objectManager->getObject('Magento\\Tax\\Block\\Checkout\\Shipping\\Price', ['checkoutSession' => $checkoutSession, 'taxHelper' => $this->taxHelper, 'priceCurrency' => $this->priceCurrency]); }
/** * @param \Magento\Quote\Model\Quote $quote * @param \Magento\Catalog\Model\Product $product * @param \Magento\Framework\DataObject $config * @return \Magento\Quote\Model\Quote\Item|string */ public function init(\Magento\Quote\Model\Quote $quote, \Magento\Catalog\Model\Product $product, \Magento\Framework\DataObject $config) { $stockItem = $this->stockRegistry->getStockItem($product->getId(), $quote->getStore()->getWebsiteId()); if ($stockItem->getIsQtyDecimal()) { $product->setIsQtyDecimal(1); } else { $config->setQty((int) $config->getQty()); } $product->setCartQty($config->getQty()); $item = $quote->addProduct($product, $config, \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL); return $item; }
/** * @param \Magento\Quote\Model\Quote $quote * @return \Magento\Payment\Model\MethodInterface[] */ public function getAvailableMethods(\Magento\Quote\Model\Quote $quote = null) { $store = $quote ? $quote->getStoreId() : null; $methods = []; $specification = $this->methodSpecificationFactory->create([AbstractMethod::CHECK_ZERO_TOTAL]); foreach ($this->paymentHelper->getStoreMethods($store, $quote) as $method) { if ($this->_canUseMethod($method, $quote) && $specification->isApplicable($method, $quote)) { $method->setInfoInstance($quote->getPayment()); $methods[] = $method; } } return $methods; }
public function prepare() { $this->contextMock = $this->getMock('Magento\\Framework\\View\\Element\\Template\\Context', [], [], '', false); $this->checkoutSessionMock = $this->getMock('Magento\\Checkout\\Model\\Session', [], [], '', false); $this->orderFactoryMock = $this->getMock('Magento\\Sales\\Model\\OrderFactory', ['getQuote'], [], '', false); $this->hssHelperMock = $this->getMock('Magento\\Paypal\\Helper\\Hss', [], [], '', false); $this->paymentDataMock = $this->getMock('Magento\\Payment\\Helper\\Data', [], [], '', false); $this->quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', ['getPayment', '__wakeup'], [], '', false); $this->paymentMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Payment', [], [], '', false); $this->checkoutSessionMock->expects($this->any())->method('getQuote')->will($this->returnValue($this->quoteMock)); $this->quoteMock->expects($this->any())->method('getPayment')->will($this->returnValue($this->paymentMock)); $this->hssHelperMock->expects($this->any())->method('getHssMethods')->will($this->returnValue([])); }
/** * @dataProvider totalsDataProvider */ public function testGetTotals($isVirtual) { $expected = 'expected'; $this->quoteMock->expects($this->at(0))->method('setTotalsCollectedFlag')->with(false); $this->quoteMock->expects($this->at(1))->method('collectTotals'); $this->quoteMock->expects($this->once())->method('isVirtual')->willreturn($isVirtual); if ($isVirtual) { $this->billingAddressMock->expects($this->once())->method('getTotals')->willReturn($expected); } else { $this->shippingAddressMock->expects($this->once())->method('getTotals')->willReturn($expected); } $this->assertEquals($expected, $this->totals->getTotals()); }
/** * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php */ public function testSetMethodWithoutShippingAddress() { $this->quote->load('test_order_with_simple_product_without_address', 'reserved_order_id'); $serviceInfo = $this->getServiceInfo(); $requestData = ['cartId' => $this->quote->getId(), 'carrierCode' => 'flatrate', 'methodCode' => 'flatrate']; try { $this->_webApiCall($serviceInfo, $requestData); } catch (\SoapFault $e) { $message = $e->getMessage(); } catch (\Exception $e) { $message = json_decode($e->getMessage())->message; } $this->assertEquals('Shipping address is not set', $message); }
public function setUp() { $objectManager = new ObjectManager($this); $this->taxConfig = $this->getMockBuilder('\\Magento\\Tax\\Model\\Config')->disableOriginalConstructor()->setMethods(['getShippingTaxClass', 'shippingPriceIncludesTax'])->getMock(); $this->store = $this->getMockBuilder('\\Magento\\Store\\Model\\Store')->disableOriginalConstructor()->setMethods(['__wakeup'])->getMock(); $this->quote = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->setMethods(['__wakeup', 'getStore'])->getMock(); $this->quote->expects($this->any())->method('getStore')->will($this->returnValue($this->store)); $this->address = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote\\Address')->disableOriginalConstructor()->setMethods(['__wakeup', 'getQuote', 'getShippingDiscountAmount', 'getBaseShippingDiscountAmount'])->getMock(); $this->address->expects($this->any())->method('getQuote')->will($this->returnValue($this->quote)); $methods = ['setType', 'setCode', 'setQuantity', 'setUnitPrice', 'setDiscountAmount', 'setTaxClassKey', 'setTaxIncluded', 'create']; $this->quoteDetailsItemBuilderMock = $this->getMock('Magento\\Tax\\Api\\Data\\QuoteDetailsItemDataBuilder', $methods, [], '', false); $classMethods = ['setType', 'setValue', 'create']; $this->taxClassKeyBuilderMock = $this->getMock('Magento\\Tax\\Api\\Data\\TaxClassKeyDataBuilder', $classMethods, [], '', false); $this->commonTaxCollector = $objectManager->getObject('Magento\\Tax\\Model\\Sales\\Total\\Quote\\CommonTaxCollector', ['taxConfig' => $this->taxConfig, 'quoteDetailsItemBuilder' => $this->quoteDetailsItemBuilderMock, 'taxClassKeyBuilder' => $this->taxClassKeyBuilderMock]); }
protected function setUp() { $this->loggerMock = $this->getMock('Psr\\Log\\LoggerInterface'); $this->quoteMock = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false); $this->orderMock = $this->getMock('Magento\\Sales\\Model\\Order', [], [], '', false); $this->paymentMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Payment', [], [], '', false); $this->orderSenderMock = $this->getMock('Magento\\Sales\\Model\\Order\\Email\\Sender\\OrderSender', [], [], '', false); $eventMock = $this->getMockBuilder('Magento\\Framework\\Event')->disableOriginalConstructor()->setMethods(['getQuote', 'getOrder'])->getMock(); $this->observerMock = $this->getMock('Magento\\Framework\\Event\\Observer', ['getEvent'], [], '', false); $this->observerMock->expects($this->any())->method('getEvent')->willReturn($eventMock); $eventMock->expects($this->once())->method('getQuote')->willReturn($this->quoteMock); $eventMock->expects($this->once())->method('getOrder')->willReturn($this->orderMock); $this->quoteMock->expects($this->once())->method('getPayment')->willReturn($this->paymentMock); $this->model = new \Magento\Quote\Observer\Webapi\SubmitObserver($this->loggerMock, $this->orderSenderMock); }
/** * @param array $giftMessages * @param \Magento\Quote\Model\Quote $quote * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function add($giftMessages, $quote) { if (!is_array($giftMessages)) { return $this; } // types are 'quote', 'quote_item', etc foreach ($giftMessages as $type => $giftMessageEntities) { foreach ($giftMessageEntities as $entityId => $message) { $giftMessage = $this->messageFactory->create(); switch ($type) { case 'quote': $entity = $quote; break; case 'quote_item': $entity = $quote->getItemById($entityId); break; case 'quote_address': $entity = $quote->getAddressById($entityId); break; case 'quote_address_item': $entity = $quote->getAddressById($message['address'])->getItemById($entityId); break; default: $entity = $quote; break; } if ($entity->getGiftMessageId()) { $giftMessage->load($entity->getGiftMessageId()); } if (trim($message['message']) == '') { if ($giftMessage->getId()) { try { $giftMessage->delete(); $entity->setGiftMessageId(0)->save(); } catch (\Exception $e) { } } continue; } try { $giftMessage->setSender($message['from'])->setRecipient($message['to'])->setMessage($message['message'])->setCustomerId($quote->getCustomerId())->save(); $entity->setGiftMessageId($giftMessage->getId())->save(); } catch (\Exception $e) { } } } return $this; }
/** * Retrieve payment method and assign additional template values * * @return $this * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function _beforeToHtml() { $methodInstance = $this->_quote->getPayment()->getMethodInstance(); $this->setPaymentMethodTitle($methodInstance->getTitle()); $this->setShippingRateRequired(true); if ($this->_quote->getIsVirtual()) { $this->setShippingRateRequired(false); } else { // prepare shipping rates $this->_address = $this->_quote->getShippingAddress(); $groups = $this->_address->getGroupedAllShippingRates(); if ($groups && $this->_address) { $this->setShippingRateGroups($groups); // determine current selected code & name foreach ($groups as $code => $rates) { foreach ($rates as $rate) { if ($this->_address->getShippingMethod() == $rate->getCode()) { $this->_currentShippingRate = $rate; break 2; } } } } $canEditShippingAddress = $this->_quote->getMayEditShippingAddress() && $this->_quote->getPayment()->getAdditionalInformation(\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_BUTTON) == 1; // misc shipping parameters $this->setShippingMethodSubmitUrl($this->getUrl("{$this->_controllerPath}/saveShippingMethod"))->setCanEditShippingAddress($canEditShippingAddress)->setCanEditShippingMethod($this->_quote->getMayEditShippingMethod()); } $this->setEditUrl($this->getUrl("{$this->_controllerPath}/edit"))->setPlaceOrderUrl($this->getUrl("{$this->_controllerPath}/placeOrder")); return parent::_beforeToHtml(); }
/** * Test for execute method * * @return void */ public function testExecuteWithSuccessOrderSave() { $testData = $this->getExecuteWithSuccessOrderSaveTestData(); $redirectMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Redirect')->disableOriginalConstructor()->getMock(); $paymentMock = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Payment')->disableOriginalConstructor()->getMock(); $checkoutMock = $this->getMockBuilder('Magento\\Checkout\\Model\\Session')->disableOriginalConstructor()->setMethods(['getRedirectUrl'])->getMock(); $resultJsonMock = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Json')->disableOriginalConstructor()->getMock(); $redirectMock->expects($this->never())->method('setPath')->with('*/*/')->willReturn('redirect'); $this->formKeyValidatorMock->expects($this->once())->method('validate')->with($this->requestMock)->willReturn(true); $this->resultRedirectFactoryMock->expects($this->never())->method('create')->willReturn($redirectMock); $this->objectManagerMock->expects($this->atLeastOnce())->method('get')->willReturnMap($testData['objectManager.get']); // call _expireAjax method $this->expireAjaxFlowHasItemsFalse(); $this->requestMock->expects($this->atLeastOnce())->method('getPost')->willReturnMap($testData['request.getPost']); $this->agreementsValidatorMock->expects($this->once())->method('isValid')->with($testData['agreementsValidator.isValid'])->willReturn(true); $this->quoteMock->expects($this->atLeastOnce())->method('getPayment')->willReturn($paymentMock); $paymentMock->expects($this->once())->method('setQuote')->with($this->quoteMock); $paymentMock->expects($this->once())->method('importData')->with($testData['payment.importData']); $this->onepageMock->expects($this->once())->method('saveOrder'); $this->onepageMock->expects($this->once())->method('getCheckout')->willReturn($checkoutMock); $checkoutMock->expects($this->once())->method('getRedirectUrl')->willReturn(null); $this->eventManagerMock->expects($this->once())->method('dispatch')->withConsecutive($this->equalTo('checkout_controller_onepage_saveOrder'), $this->countOf(2)); $this->resultJsonFactoryMock->expects($this->once())->method('create')->willReturn($resultJsonMock); $resultJsonMock->expects($this->once())->method('setData')->with($testData['resultJson.setData'])->willReturnSelf(); $this->assertEquals($resultJsonMock, $this->controller->execute()); }
/** * Retrieve quote model object * * @return \Magento\Quote\Model\Quote */ public function getQuote() { if ($this->_quote === null) { $this->_quote = $this->quoteFactory->create(); if ($this->getStoreId()) { if (!$this->getQuoteId()) { $this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId()); $this->_quote->setIsActive(false); $this->_quote->setStoreId($this->getStoreId()); $this->quoteRepository->save($this->_quote); $this->setQuoteId($this->_quote->getId()); $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); } else { $this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]); $this->_quote->setStoreId($this->getStoreId()); } if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) { $customer = $this->customerRepository->getById($this->getCustomerId()); $this->_quote->assignCustomer($customer); $this->quoteRepository->save($this->_quote); } } $this->_quote->setIgnoreOldQty(true); $this->_quote->setIsSuperMode(true); } return $this->_quote; }
/** * Execute operation * * @param string $shippingMethod * @param Quote $quote * @return void * @throws \InvalidArgumentException */ public function execute($shippingMethod, Quote $quote) { if (empty($shippingMethod)) { throw new \InvalidArgumentException('The "shippingMethod" field does not exists.'); } if (!$quote->getIsVirtual()) { $shippingAddress = $quote->getShippingAddress(); if ($shippingMethod !== $shippingAddress->getShippingMethod()) { $this->disabledQuoteAddressValidation($quote); $shippingAddress->setShippingMethod($shippingMethod); $shippingAddress->setCollectShippingRates(true); $quote->collectTotals(); $this->quoteRepository->save($quote); } } }
/** * @param array $productTypes * @param int $expected * @dataProvider dataProviderForTestBeforeSaveIsVirtualQuote */ public function testBeforeSaveIsVirtualQuote(array $productTypes, $expected) { $storeId = 1; $currencyMock = $this->getMockBuilder('Magento\\Directory\\Model\\Currency')->disableOriginalConstructor()->getMock(); $currencyMock->expects($this->any())->method('getCode')->will($this->returnValue('test_code')); $currencyMock->expects($this->any())->method('getRate')->will($this->returnValue('test_rate')); $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock(); $storeMock->expects($this->once())->method('getBaseCurrency')->will($this->returnValue($currencyMock)); $storeMock->expects($this->once())->method('getCurrentCurrency')->will($this->returnValue($currencyMock)); $this->storeManagerMock->expects($this->any())->method('getStore')->with($storeId)->will($this->returnValue($storeMock)); $this->quote->setStoreId($storeId); $collectionMock = $this->getMock('Magento\\Quote\\Model\\Resource\\Quote\\Item\\Collection', [], [], '', false); $items = []; foreach ($productTypes as $type) { $productMock = $this->getMock('\\Magento\\Catalog\\Model\\Product', [], [], '', false); $productMock->expects($this->any())->method('getIsVirtual')->willReturn($type); $itemMock = $this->getMock('Magento\\Quote\\Model\\Quote\\Item', ['isDeleted', 'getParentItemId', 'getProduct'], [], '', false); $itemMock->expects($this->any())->method('isDeleted')->willReturn(false); $itemMock->expects($this->any())->method('getParentItemId')->willReturn(false); $itemMock->expects($this->any())->method('getProduct')->willReturn($productMock); $items[] = $itemMock; } $iterator = new \ArrayIterator($items); $collectionMock->expects($this->any())->method('getIterator')->will($this->returnValue($iterator)); $this->quoteItemCollectionFactoryMock->expects($this->once())->method('create')->will($this->returnValue($collectionMock)); $this->quote->beforeSave(); $this->assertEquals($expected, $this->quote->getDataByKey(CartInterface::KEY_IS_VIRTUAL)); }
public function testDelete() { $this->quoteMock->expects($this->once())->method('delete'); $this->quoteMock->expects($this->exactly(1))->method('getId')->willReturn(1); $this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2); $this->model->delete($this->quoteMock); }