public function testGetAvailableProductTypes() { $productTypes = ['simple']; $this->configDataMock->expects($this->once())->method('get')->with($this->equalTo('order/available_product_types'))->will($this->returnValue($productTypes)); $result = $this->model->getAvailableProductTypes(); $this->assertEquals($productTypes, $result); }
/** * Prepare collection to be displayed in the grid * * @return $this */ protected function _prepareCollection() { $attributes = $this->_catalogConfig->getProductAttributes(); /* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */ $collection = $this->_productFactory->create()->getCollection(); $collection->setStore($this->getStore())->addAttributeToSelect($attributes)->addAttributeToSelect('sku')->addStoreFilter()->addAttributeToFilter('type_id', $this->_salesConfig->getAvailableProductTypes())->addAttributeToSelect('gift_message_available'); $this->setCollection($collection); return parent::_prepareCollection(); }
/** * Filter collection by removing not available product types * * @param \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection $collection * @return \Magento\Framework\Model\Resource\Db\Collection\AbstractCollection */ public function applySalableProductTypesFilter($collection) { $productTypes = $this->_salesConfig->getAvailableProductTypes(); foreach ($collection->getItems() as $key => $item) { if ($item instanceof \Magento\Catalog\Model\Product) { $type = $item->getTypeId(); } elseif ($item instanceof \Magento\Sales\Model\Order\Item) { $type = $item->getProductType(); } elseif ($item instanceof \Magento\Quote\Model\Quote\Item) { $type = $item->getProductType(); } else { $type = ''; } if (!in_array($type, $productTypes)) { $collection->removeItemByKey($key); } } return $collection; }
/** * Retrieve all items * * @return array */ public function getItems() { $items = array(); $collection = $this->getItemCollection(); if ($collection) { $productTypes = $this->_salesConfig->getAvailableProductTypes(); if (is_array($collection)) { $items = $collection; } else { $items = $collection->getItems(); } /* * Filtering items by allowed product type */ foreach ($items as $key => $item) { if ($item instanceof \Magento\Catalog\Model\Product) { $type = $item->getTypeId(); } else { if ($item instanceof \Magento\Sales\Model\Order\Item) { $type = $item->getProductType(); } else { if ($item instanceof \Magento\Sales\Model\Quote\Item) { $type = $item->getProductType(); } else { $type = ''; // Maybe some item, that can give us product via getProduct() if ($item instanceof \Magento\Framework\Object || method_exists($item, 'getProduct')) { $product = $item->getProduct(); if ($product && $product instanceof \Magento\Catalog\Model\Product) { $type = $product->getTypeId(); } } } } } if (!in_array($type, $productTypes)) { unset($items[$key]); } } } return $items; }
/** * Initialize creation data from existing order * * @param \Magento\Sales\Model\Order $order * @return $this * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function initFromOrder(\Magento\Sales\Model\Order $order) { $session = $this->getSession(); $session->setData($order->getReordered() ? 'reordered' : 'order_id', $order->getId()); $session->setCurrencyId($order->getOrderCurrencyCode()); /* Check if we edit guest order */ $session->setCustomerId($order->getCustomerId() ?: false); $session->setStoreId($order->getStoreId()); /* Initialize catalog rule data with new session values */ $this->initRuleData(); foreach ($order->getItemsCollection($this->_salesConfig->getAvailableProductTypes(), true) as $orderItem) { /* @var $orderItem \Magento\Sales\Model\Order\Item */ if (!$orderItem->getParentItem()) { $qty = $orderItem->getQtyOrdered(); if (!$order->getReordered()) { $qty -= max($orderItem->getQtyShipped(), $orderItem->getQtyInvoiced()); } if ($qty > 0) { $item = $this->initFromOrderItem($orderItem, $qty); if (is_string($item)) { throw new \Magento\Framework\Exception\LocalizedException(__($item)); } } } } $shippingAddress = $order->getShippingAddress(); if ($shippingAddress) { $addressDiff = array_diff_assoc($shippingAddress->getData(), $order->getBillingAddress()->getData()); unset($addressDiff['address_type'], $addressDiff['entity_id']); $shippingAddress->setSameAsBilling(empty($addressDiff)); } $this->_initBillingAddressFromOrder($order); $this->_initShippingAddressFromOrder($order); $quote = $this->getQuote(); if (!$quote->isVirtual() && $this->getShippingAddress()->getSameAsBilling()) { $this->setShippingAsBilling(1); } $this->setShippingMethod($order->getShippingMethod()); $quote->getShippingAddress()->setShippingDescription($order->getShippingDescription()); $paymentData = $order->getPayment()->getData(); unset($paymentData['cc_type'], $paymentData['cc_last_4']); unset($paymentData['cc_exp_month'], $paymentData['cc_exp_year']); $quote->getPayment()->addData($paymentData); $orderCouponCode = $order->getCouponCode(); if ($orderCouponCode) { $quote->setCouponCode($orderCouponCode); } if ($quote->getCouponCode()) { $quote->collectTotals(); } $this->_objectCopyService->copyFieldsetToTarget('sales_copy_order', 'to_edit', $order, $quote); $this->_eventManager->dispatch('sales_convert_order_to_quote', ['order' => $order, 'quote' => $quote]); if (!$order->getCustomerId()) { $quote->setCustomerIsGuest(true); } if ($session->getUseOldShippingMethod(true)) { /* * if we are making reorder or editing old order * we need to show old shipping as preselected * so for this we need to collect shipping rates */ $this->collectShippingRates(); } else { /* * if we are creating new order then we don't need to collect * shipping rates before customer hit appropriate button */ $this->collectRates(); } $this->quoteRepository->save($quote); return $this; }
/** * @param \Magento\Sales\Model\Order $order * @return array */ protected function getNonEditableTypes($order) { return array_keys($this->getOrder()->getResource()->aggregateProductsByTypes($order->getId(), $this->_salesConfig->getAvailableProductTypes(), false)); }