diff --git a/Model/Api/OrderInfo.php b/Model/Api/OrderInfo.php index add9a6f..036c225 100644 --- a/Model/Api/OrderInfo.php +++ b/Model/Api/OrderInfo.php @@ -9,6 +9,7 @@ use Psr\Log\LoggerInterface; use Magento\Catalog\Helper\Image; use Returnless\Connector\Model\Config; +use Magento\Framework\Exception\NoSuchEntityException; /** * Interface OrderInfo @@ -17,6 +18,11 @@ */ class OrderInfo implements OrderInfoInterface { + /** + * const PRODUCT_TYPE_BUNDLE + */ + const PRODUCT_TYPE_BUNDLE = 'bundle'; + /** * @var bool */ @@ -116,31 +122,90 @@ public function getOrderInfoReturnless($incrementId) $orderInfo['billing_address']['addition'] = isset($street[2]) ? $street[2] : ''; } - $orderItems = $order->getAllVisibleItems(); + $separateBundle = $this->config->getSeparateBundle(); + $orderItems = $separateBundle ? $order->getAllItems() : $order->getAllVisibleItems(); $this->logger->debug('Order has items', [count($orderItems)]); foreach ($orderItems as $orderItemKey => $orderItem) { + if ($orderItem->getParentItemId()) { + continue; + } + $orderInfo['order_products'][$orderItemKey]['product_id'] = $orderItem->getProductId(); $orderInfo['order_products'][$orderItemKey]['quantity'] = $orderItem->getQtyOrdered(); $orderInfo['order_products'][$orderItemKey]['order_product_id'] = $orderItem->getItemId(); + + $orderInfo['order_products'][$orderItemKey]['price'] = $orderItem->getBasePrice(); + $orderInfo['order_products'][$orderItemKey]['discount_amount'] = $orderItem->getDiscountAmount(); $orderInfo['order_products'][$orderItemKey]['price_inc_tax'] = $orderItem->getPriceInclTax(); + $orderInfo['order_products'][$orderItemKey]['total_amount'] = $orderItem->getRowTotalInclTax(); $orderInfo['order_products'][$orderItemKey]['model'] = $orderItem->getSku(); - $product = $this->getProductById($orderItem->getProductId()); - - $orderInfo['order_products'][$orderItemKey]['name'] = $product->getName(); - $orderInfo['order_products'][$orderItemKey]['images'][0]['http_path'] = $this->getImageByProduct($product); - $orderInfo['order_products'][$orderItemKey]['images'][1]['http_path'] = $this->getImageByProduct1($product); - $orderInfo['order_products'][$orderItemKey]['url'] = $product->getProductUrl(); - $orderInfo['order_products'][$orderItemKey]['categories_ids'] = $product->getCategoryIds(); - $orderInfo['order_products'][$orderItemKey]['u_brand'] = $product->getBrand(); + $itemType = $orderItem->getProductType(); + + $orderInfo['order_products'][$orderItemKey]['item_type'] = $itemType; + + if ($itemType === self::PRODUCT_TYPE_BUNDLE && $separateBundle) { + $orderInfo['order_products'][$orderItemKey]['is_bundle'] = true; + $orderInfo['order_products'][$orderItemKey]['bundle_item_id'] = $orderItem->getItemId(); + $orderInfo['order_products'][$orderItemKey]['is_separated'] = 1; + + if ($orderItem->getChildrenItems()) { + foreach ($orderItem->getChildrenItems() as $key => $bundleChildren) { + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['product_id'] = $bundleChildren->getProductId(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['quantity'] = $bundleChildren->getQtyOrdered(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['order_product_id'] = $bundleChildren->getItemId(); + + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['price'] = $bundleChildren->getBasePrice(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['discount_amount'] = $bundleChildren->getDiscountAmount(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['price_inc_tax'] = $bundleChildren->getPriceInclTax(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['total_amount'] = $bundleChildren->getRowTotalInclTax(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['model'] = $bundleChildren->getSku(); + + $product = $this->getProductById($bundleChildren->getProductId()); + + if ($product) { + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['cost'] = $product->getPrice(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['name'] = $product->getName(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['images'][0]['http_path'] = $this->getImageByProduct($product); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['images'][1]['http_path'] = $this->getImageByProduct1($product); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['url'] = $product->getProductUrl(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['categories_ids'] = $product->getCategoryIds(); + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['u_brand'] = $product->getBrand(); + + $eavAttributeCode = $this->config->getEanAttributeCode(); + + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['u_upc'] = null; + if (!empty($eavAttributeCode)) { + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['u_upc'] = $product->getData($eavAttributeCode); + } + } else { + $orderInfo['order_products'][$orderItemKey]['bundle_children'][$key]['name'] = $bundleChildren->getName(); + } + } + } + } - $eavAttributeCode = $this->config->getEanAttributeCode(); + $product = $this->getProductById($orderItem->getProductId()); - $orderInfo['order_products'][$orderItemKey]['u_upc'] = null; - if (!empty($eavAttributeCode)) { - $orderInfo['order_products'][$orderItemKey]['u_upc'] = $product->getData($eavAttributeCode); + if ($product) { + $orderInfo['order_products'][$orderItemKey]['cost'] = $product->getPrice(); + $orderInfo['order_products'][$orderItemKey]['name'] = $product->getName(); + $orderInfo['order_products'][$orderItemKey]['images'][0]['http_path'] = $this->getImageByProduct($product); + $orderInfo['order_products'][$orderItemKey]['images'][1]['http_path'] = $this->getImageByProduct1($product); + $orderInfo['order_products'][$orderItemKey]['url'] = $product->getProductUrl(); + $orderInfo['order_products'][$orderItemKey]['categories_ids'] = $product->getCategoryIds(); + $orderInfo['order_products'][$orderItemKey]['u_brand'] = $product->getBrand(); + + $eavAttributeCode = $this->config->getEanAttributeCode(); + + $orderInfo['order_products'][$orderItemKey]['u_upc'] = null; + if (!empty($eavAttributeCode)) { + $orderInfo['order_products'][$orderItemKey]['u_upc'] = $product->getData($eavAttributeCode); + } + } else { + $orderInfo['order_products'][$orderItemKey]['name'] = $orderItem->getName(); } } @@ -177,7 +242,13 @@ public function setReturnFlag() */ protected function getProductById($id) { - return $this->productRepository->getById($id); + try { + $product = $this->productRepository->getById($id); + } catch (NoSuchEntityException $e) { + $product = false; + } + + return $product; } /** diff --git a/Model/Config.php b/Model/Config.php index 2ea1dbb..06ff5c5 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -30,6 +30,11 @@ class Config */ const CONFIG_EAN_ATTRIBUTE_CODE = 'returnless_connector/general/u_upc'; + /** + * const CONFIG_SEPARATE_BUNDLE_PRODUCTS + */ + const CONFIG_SEPARATE_BUNDLE_PRODUCTS = 'returnless_connector/general/bundle_enabled'; + /** * Config constructor. * @@ -85,4 +90,21 @@ public function getEanAttributeCode($store = null) return $eanAttributeCode; } + + /** + * Enabled separate bundle products + * + * @param null $store + * @return string + */ + public function getSeparateBundle($store = null) + { + $separateBundle = (string)$this->scopeConfig->getValue( + self::CONFIG_SEPARATE_BUNDLE_PRODUCTS, + ScopeInterface::SCOPE_STORE, + $store + ); + + return $separateBundle; + } } diff --git a/composer.json b/composer.json index 3c59e8e..d433bd9 100644 --- a/composer.json +++ b/composer.json @@ -17,5 +17,5 @@ "OSL-3.0", "AFL-3.0" ], - "version": "1.0.3" + "version": "1.0.5" } diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index dc29696..7784f56 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -24,6 +24,11 @@ + + + Magento\Config\Model\Config\Source\Yesno + + diff --git a/etc/module.xml b/etc/module.xml index 987d41b..d15a699 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - +