Skip to content

Commit

Permalink
Added const PRODUCT_TYPE_BUNDLE, CONFIG_SEPARATE_BUNDLE_PRODUCTS. Imp…
Browse files Browse the repository at this point in the history
…lemented getSeparateBundle, bundle logic. Fixed method getProductById. Updated version to 1.0.5
  • Loading branch information
Returnless committed Feb 10, 2021
1 parent c217e4c commit 9b22733
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 16 deletions.
99 changes: 85 additions & 14 deletions Model/Api/OrderInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,11 @@
*/
class OrderInfo implements OrderInfoInterface
{
/**
* const PRODUCT_TYPE_BUNDLE
*/
const PRODUCT_TYPE_BUNDLE = 'bundle';

/**
* @var bool
*/
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"OSL-3.0",
"AFL-3.0"
],
"version": "1.0.3"
"version": "1.0.5"
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<label>EAN attribute code</label>
<comment><![CDATA[The name of the EAN attribute in your system]]></comment>
</field>
<field id="bundle_enabled" translate="label" type="select" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Separate bundle products</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Separation of bundles will be enabled.]]></comment>
</field>
</group>
</section>
</system>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Returnless_Connector" setup_version="1.0.3">
<module name="Returnless_Connector" setup_version="1.0.5">
<sequence>
<module name="Magento_Sales"/>
</sequence>
Expand Down

0 comments on commit 9b22733

Please sign in to comment.