/** * Converts a specified rate model to a shipping method data object. * * @param \Magento\Quote\Model\Quote\Item $item * @return array * @throws \Exception */ public function modelToDataObject($item) { $this->eventManager->dispatch('items_additional_data', ['item' => $item]); $items = $item->toArray(); $items['options'] = $this->getFormattedOptionValue($item); $itemsData = $this->totalsItemFactory->create(); $this->dataObjectHelper->populateWithArray($itemsData, $items, '\\Magento\\Quote\\Api\\Data\\TotalsItemInterface'); return $itemsData; }
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; }
/** * @param int $qty * @return int */ public function getQtyForCheck($qty) { if (!$this->item->getParentItem()) { $increaseQty = $this->item->getQtyToAdd() ? $this->item->getQtyToAdd() : $qty; return $this->quoteItemQtyList->getQty($this->item->getProduct()->getId(), $this->item->getId(), $this->item->getQuoteId(), $increaseQty); } return $this->quoteItemQtyList->getQty($this->item->getProduct()->getId(), $this->item->getId(), $this->item->getQuoteId(), 0); }
/** * Returns options adopted to compare * * @param Item $item * @return array */ public function getOptions(Item $item) { $options = []; foreach ($item->getOptions() as $option) { $options[$option->getCode()] = $this->getOptionValues($option->getValue()); } return $options; }
/** * test for convert method */ public function testConvert() { $this->quoteItemMock->expects($this->exactly(2))->method('getProduct')->willReturn($this->productMock); $this->productMock->expects($this->once())->method('getTypeInstance')->willReturn($this->productTypeMock); $this->productTypeMock->expects($this->once())->method('getOrderOptions')->with($this->productMock)->willReturn(['option']); $this->objectCopyServiceMock->expects($this->at(0))->method('getDataFromFieldset')->with('quote_convert_item', 'to_order_item', $this->quoteItemMock)->willReturn([]); $this->objectCopyServiceMock->expects($this->at(1))->method('getDataFromFieldset')->with('quote_convert_item', 'to_order_item_discount', $this->quoteItemMock)->willReturn([]); $this->orderItemFactoryMock->expects($this->once())->method('create')->willReturn($this->orderItemMock); $this->assertInstanceOf('Magento\\Sales\\Model\\Order\\Item', $this->converter->convert($this->quoteItemMock, [])); }
/** * Adds item ID to giftOptionsCartItem configuration and name * * @param array $jsLayout * @param Item $item * @return array */ public function process($jsLayout, Item $item) { if (isset($jsLayout['components']['giftOptionsCartItem'])) { if (!isset($jsLayout['components']['giftOptionsCartItem']['config'])) { $jsLayout['components']['giftOptionsCartItem']['config'] = []; } $jsLayout['components']['giftOptionsCartItem']['config']['itemId'] = $item->getId(); $jsLayout['components']['giftOptionsCartItem-' . $item->getId()] = $jsLayout['components']['giftOptionsCartItem']; unset($jsLayout['components']['giftOptionsCartItem']); } return $jsLayout; }
/** * Adds stock item qty to $items (creates new entry or increments existing one) * * @param QuoteItem $quoteItem * @param array &$items * @return void */ protected function _addItemToQtyArray(QuoteItem $quoteItem, &$items) { $productId = $quoteItem->getProductId(); if (!$productId) { return; } if (isset($items[$productId])) { $items[$productId] += $quoteItem->getTotalQty(); } else { $items[$productId] = $quoteItem->getTotalQty(); } }
/** * @param $baseRowTotalInclTax * @param $baseRowWeeeTaxInclTax * @param $weeeEnabled * @param $expectedValue * @dataProvider testGetFinalDisplayPriceDataProvider */ public function testGetBaseFinalRowDisplayPriceInclTax($baseRowTotalInclTax, $baseRowWeeeTaxInclTax, $weeeEnabled, $expectedValue) { $this->weeeHelper->expects($this->once())->method('isEnabled')->will($this->returnValue($weeeEnabled)); $this->weeeHelper->expects($this->any())->method('getBaseRowWeeeTaxInclTax')->with($this->item)->will($this->returnValue($baseRowWeeeTaxInclTax)); $this->item->expects($this->once())->method('getBaseRowTotalInclTax')->will($this->returnValue($baseRowTotalInclTax)); $this->assertEquals($expectedValue, $this->renderer->getBaseFinalRowDisplayPriceInclTax()); }
public function testInitTotalsNoItems() { $address = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Address', [], [], '', false); $this->item->expects($this->never())->method('getParentItemId'); $this->model->init($this->model->getWebsiteId(), $this->model->getCustomerGroupId(), $this->model->getCouponCode()); $this->model->initTotals([], $address); }
/** * test compare two items first without options and second with options */ public function testCompareItemWithoutOptionWithCompared() { $this->itemMock->expects($this->any())->method('getProductId')->will($this->returnValue(1)); $this->comparedMock->expects($this->any())->method('getProductId')->will($this->returnValue(1)); $this->comparedMock->expects($this->any())->method('getOptions')->will($this->returnValue([$this->getOptionMock('option-1', 1), $this->getOptionMock('option-2', 'option-value'), $this->getOptionMock('option-3', serialize(['value' => 'value-1', 'qty' => 2]))])); $this->itemMock->expects($this->any())->method('getOptions')->will($this->returnValue([])); $this->assertFalse($this->helper->compare($this->itemMock, $this->comparedMock)); }
/** * Verify that compare ignores empty options. */ public function testCompareWithEmptyValues() { $this->itemMock->expects($this->any())->method('getProductId')->will($this->returnValue(1)); $this->comparedMock->expects($this->any())->method('getProductId')->will($this->returnValue(1)); $this->itemMock->expects($this->once())->method('getOptions')->willReturn([$this->getOptionMock('option-1', serialize(['non-empty-option' => 'test', 'empty_option' => '']))]); $this->comparedMock->expects($this->once())->method('getOptions')->willReturn([$this->getOptionMock('option-1', serialize(['non-empty-option' => 'test']))]); $this->assertTrue($this->helper->compare($this->itemMock, $this->comparedMock)); }
public function testSaveModifiedItem() { $this->entitySnapshotMock->expects($this->exactly(2))->method('isModified')->with($this->quoteItemMock)->willReturn(true); $this->quoteItemMock->expects($this->once())->method('isOptionsSaved')->willReturn(false); $this->quoteItemMock->expects($this->once())->method('saveItemOptions'); $this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock); $this->assertEquals($this->model, $this->model->save($this->quoteItemMock)); }
/** * @param Item|AddressItem $item * @param array $data * @return OrderItemInterface */ public function convert($item, $data = []) { $options = $item->getProductOrderOptions(); if (!$options) { $options = $item->getProduct()->getTypeInstance()->getOrderOptions($item->getProduct()); } $orderItemData = $this->objectCopyService->getDataFromFieldset('quote_convert_item', 'to_order_item', $item); if (!$item->getNoDiscount()) { $data = array_merge($data, $this->objectCopyService->getDataFromFieldset('quote_convert_item', 'to_order_item_discount', $item)); } $orderItem = $this->orderItemFactory->create(); $this->dataObjectHelper->populateWithArray($orderItem, array_merge($orderItemData, $data), '\\Magento\\Sales\\Api\\Data\\OrderItemInterface'); $orderItem->setProductOptions($options); if ($item->getParentItem()) { $orderItem->setQtyOrdered($orderItemData[OrderItemInterface::QTY_ORDERED] * $item->getParentItem()->getQty()); } return $orderItem; }
/** * Initialize item option * * @param \Magento\Quote\Model\Quote\Item\Option $option * @param \Magento\Quote\Model\Quote\Item $quoteItem * @param int $qty * * @return \Magento\Framework\Object * @throws \Magento\Framework\Exception\LocalizedException */ public function initialize(\Magento\Quote\Model\Quote\Item\Option $option, \Magento\Quote\Model\Quote\Item $quoteItem, $qty) { $optionValue = $option->getValue(); $optionQty = $qty * $optionValue; $increaseOptionQty = ($quoteItem->getQtyToAdd() ? $quoteItem->getQtyToAdd() : $qty) * $optionValue; $qtyForCheck = $this->quoteItemQtyList->getQty($option->getProduct()->getId(), $quoteItem->getId(), $quoteItem->getQuoteId(), $increaseOptionQty); $stockItem = $this->getStockItem($option, $quoteItem); $result = $this->stockState->checkQuoteItemQty($option->getProduct()->getId(), $optionQty, $qtyForCheck, $optionValue, $option->getProduct()->getStore()->getWebsiteId()); if ($result->getItemIsQtyDecimal() !== null) { $option->setIsQtyDecimal($result->getItemIsQtyDecimal()); } if ($result->getHasQtyOptionUpdate()) { $option->setHasQtyOptionUpdate(true); $quoteItem->updateQtyOption($option, $result->getOrigQty()); $option->setValue($result->getOrigQty()); /** * if option's qty was updates we also need to update quote item qty */ $quoteItem->setData('qty', intval($qty)); } if ($result->getMessage() !== null) { $option->setMessage($result->getMessage()); $quoteItem->setMessage($result->getMessage()); } if ($result->getItemBackorders() !== null) { $option->setBackorders($result->getItemBackorders()); } $stockItem->unsIsChildItem(); return $result; }
public function testRemoveErrorInfosByParamsAllErrorsRemoved() { $message = "message"; $message2 = "message2"; $this->errorInfos->expects($this->at(0))->method('addItem')->with(null, null, $message); $this->errorInfos->expects($this->at(1))->method('addItem')->with(null, null, $message2); $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message)); $this->assertEquals($this->model, $this->model->addErrorInfo(null, null, $message2)); $this->assertEquals($message . "\n" . $message2, $this->model->getMessage()); $params = []; $removedItems = [['message' => $message], ['message' => $message2]]; $this->errorInfos->expects($this->once())->method('removeItemsByParams')->with($params)->will($this->returnValue($removedItems)); $this->errorInfos->expects($this->once())->method('getItems')->will($this->returnValue([])); $this->assertEquals($this->model, $this->model->removeErrorInfosByParams($params)); $this->assertFalse($this->model->getHasError()); $this->assertEquals('', $this->model->getMessage()); }
public function testGetTotalAmount() { $rowTotal = 100; $taxAmount = 10; $hiddenTaxAmount = 2; $discountAmount = 20; $weeeTaxAppliedRowAmount = 10; $expectedResult = $rowTotal + $taxAmount + $hiddenTaxAmount - $discountAmount + $weeeTaxAppliedRowAmount; $this->itemMock->expects($this->once())->method('getRowTotal')->will($this->returnValue($rowTotal)); $this->itemMock->expects($this->once())->method('getTaxAmount')->will($this->returnValue($taxAmount)); $this->itemMock->expects($this->once())->method('getHiddenTaxAmount')->will($this->returnValue($hiddenTaxAmount)); $this->itemMock->expects($this->once())->method('getDiscountAmount')->will($this->returnValue($discountAmount)); $this->itemMock->expects($this->once())->method('getWeeeTaxAppliedRowAmount')->will($this->returnValue($weeeTaxAppliedRowAmount)); $this->assertEquals($expectedResult, $this->block->getTotalAmount($this->itemMock)); }
/** * Verify that correct fields of item has been set * * @param \PHPUnit_Framework_MockObject_MockObject|\Magento\Quote\Model\Quote\Item $item * @param array $itemData */ public function verifyItem(\Magento\Quote\Model\Quote\Item $item, $itemData) { foreach ($itemData as $key => $value) { $this->assertEquals($value, $item->getData($key), 'item ' . $key . ' is incorrect'); } }
/** * Adding new item to quote * * @param \Magento\Quote\Model\Quote\Item $item * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function addItem(\Magento\Quote\Model\Quote\Item $item) { $item->setQuote($this); if (!$item->getId()) { $this->getItemsCollection()->addItem($item); $this->_eventManager->dispatch('sales_quote_add_item', ['quote_item' => $item]); } return $this; }
/** * Remove item * * @param Address $address * @param AddressItem|Item $item * @return $this */ protected function _removeItem($address, $item) { if ($item instanceof Item) { $address->removeItem($item->getId()); if ($address->getQuote()) { $address->getQuote()->removeItem($item->getId()); } } elseif ($item instanceof AddressItem) { $address->removeItem($item->getId()); if ($address->getQuote()) { $address->getQuote()->removeItem($item->getQuoteItemId()); } } return $this; }
public function getAdditionalData(\Magento\Quote\Model\Quote\Item $quoteItem) { $additionalData = $this->proxyItem->getAdditionalData(); $existAdditionalData = $quoteItem->getAdditionalData(); $existAdditionalData = is_string($existAdditionalData) ? @unserialize($existAdditionalData) : []; return serialize(array_merge((array) $existAdditionalData, $additionalData)); }
/** * Set quote item * * @param \Magento\Quote\Model\Quote\Item $item * @return $this */ public function setItem($item) { $this->setItemId($item->getId()); $this->_item = $item; return $this; }
/** * Add error to Quote Item * * @param \Magento\Quote\Model\Quote\Item $item * @param string $itemError * @param string $quoteError * @param string $errorIndex * @return $this */ protected function _addQuoteItemError(\Magento\Quote\Model\Quote\Item $item, $itemError, $quoteError, $errorIndex = 'error') { $item->setHasError(true); $item->setMessage($itemError); $item->setQuoteMessage($quoteError); $item->setQuoteMessageIndex($errorIndex); return $this; }
/** * {@inheritdoc} */ public function getItemData(Item $item) { $this->item = $item; return \array_merge(['product_type' => $item->getProductType()], $this->doGetItemData()); }
/** * @param \Magento\Quote\Model\Quote\Item $quoteItem * @return $this */ public function importQuoteItem(\Magento\Quote\Model\Quote\Item $quoteItem) { $this->_quote = $quoteItem->getQuote(); $this->setQuoteItem($quoteItem)->setQuoteItemId($quoteItem->getId())->setProductId($quoteItem->getProductId())->setProduct($quoteItem->getProduct())->setSku($quoteItem->getSku())->setName($quoteItem->getName())->setDescription($quoteItem->getDescription())->setWeight($quoteItem->getWeight())->setPrice($quoteItem->getPrice())->setCost($quoteItem->getCost()); if (!$this->hasQty()) { $this->setQty($quoteItem->getQty()); } $this->setQuoteItemImported(true); return $this; }
/** * Get display price for unit price excluding tax. The Weee amount will be added to unit price * depending on Weee display setting * * @param \Magento\Quote\Model\Quote\Item $item * @return float */ private function getUnitDisplayPriceExclTax($item) { $priceExclTax = $item->getCalculationPrice(); if (!$this->weeeHelper->isEnabled($this->getStoreId())) { return $priceExclTax; } if ($this->getIncludeWeeeFlag()) { return $priceExclTax + $item->getWeeeTaxAppliedAmount(); } return $priceExclTax; }
/** * Removes error statuses from quote and item, set by this observer * * @param \Magento\Quote\Model\Quote\Item $item * @param int $code * @return void */ protected function _removeErrorsFromQuoteAndItem($item, $code) { if ($item->getHasError()) { $params = ['origin' => 'cataloginventory', 'code' => $code]; $item->removeErrorInfosByParams($params); } $quote = $item->getQuote(); $quoteItems = $quote->getItemsCollection(); $canRemoveErrorFromQuote = true; foreach ($quoteItems as $quoteItem) { if ($quoteItem->getItemId() == $item->getItemId()) { continue; } $errorInfos = $quoteItem->getErrorInfos(); foreach ($errorInfos as $errorInfo) { if ($errorInfo['code'] == $code) { $canRemoveErrorFromQuote = false; break; } } if (!$canRemoveErrorFromQuote) { break; } } if ($quote->getHasError() && $canRemoveErrorFromQuote) { $params = ['origin' => 'cataloginventory', 'code' => $code]; $quote->removeErrorInfosByParams(null, $params); } }
/** * Set store_id value to quote item * * @param Item $item * @return void */ protected function setItemStoreId(Item $item) { if ($this->appState->getAreaCode() === \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE) { $storeId = $this->storeManager->getStore($this->storeManager->getStore()->getId())->getId(); $item->setStoreId($storeId); } else { $item->setStoreId($this->storeManager->getStore()->getId()); } }
/** * Prepare options array for info buy request * * @param \Magento\Quote\Model\Quote\Item $item * @return array */ protected function _prepareOptionsForRequest($item) { $newInfoOptions = []; $optionIds = $item->getOptionByCode('option_ids'); if ($optionIds) { foreach (explode(',', $optionIds->getValue()) as $optionId) { $option = $item->getProduct()->getOptionById($optionId); $optionValue = $item->getOptionByCode('option_' . $optionId)->getValue(); $group = $this->_objectManager->get('Magento\\Catalog\\Model\\Product\\Option')->groupFactory($option->getType())->setOption($option)->setQuoteItem($item); $newInfoOptions[$optionId] = $group->prepareOptionValueForRequest($optionValue); } } return $newInfoOptions; }
/** * Returns whether moving to wishlist is allowed for this item * * @param Item $item * @return bool */ public function isMoveToWishlistAllowed($item) { return $item->getProduct()->isVisibleInSiteVisibility(); }
/** * Adds stock item qty to $items (creates new entry or increments existing one) * $items is array with following structure: * array( * $productId => array( * 'qty' => $qty, * 'item' => $stockItems|null * ) * ) * * @param QuoteItem $quoteItem * @param array &$items * @return void */ protected function _addItemToQtyArray(QuoteItem $quoteItem, &$items) { $productId = $quoteItem->getProductId(); if (!$productId) { return; } if (isset($items[$productId])) { $items[$productId] += $quoteItem->getTotalQty(); } else { $stockItem = null; if ($quoteItem->getProduct()) { /** @var Item $stockItem */ $stockItem = $this->stockRegistry->getStockItem($quoteItem->getProduct()->getId(), $quoteItem->getStore()->getWebsiteId()); } $items[$productId] = $quoteItem->getTotalQty(); } }