From 3abbc5b95847e8c02701b13264c7ffaae91b7fc4 Mon Sep 17 00:00:00 2001 From: pniel-cohen Date: Fri, 25 Feb 2022 15:04:37 +0200 Subject: [PATCH] 1.0.6: Added schema patch for grouped products or when missing stock-item --- Model/Schema.php | 20 +++++++++++++++++++- composer.json | 2 +- etc/module.xml | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Model/Schema.php b/Model/Schema.php index 1ee0ae1..6618618 100755 --- a/Model/Schema.php +++ b/Model/Schema.php @@ -23,6 +23,7 @@ use Magento\Catalog\Model\Product\Attribute\Source\Status as ProductStatus; use Magento\Framework\Stdlib\DateTime\TimezoneInterface; use Magento\Framework\UrlInterface; +use Magento\Framework\Exception\NoSuchEntityException; /** * Kimonix schema. @@ -137,7 +138,16 @@ public function getProductImageUrl(Product $product, $imageDisplayAreas = ['prod */ public function getProductStockItem(Product $product) { - return $product->getStockItem() ?: $this->stockItemRepository->get($product->getId()); + try { + if($product->getTypeId() === ProductTypeGrouped::TYPE_CODE){ + return false; + } + return $product->getStockItem() ?: $this->stockItemRepository->get($product->getId()); + } catch (\NoSuchEntityException $e) { + return false; + } catch (\Exception $e) { + return false; + } } /** @@ -197,6 +207,10 @@ public function getProductSchema(Product $product) $schema["inventory"] = (int) $stockItem->getQty(); $schema["inventory_tracked"] = (bool) $stockItem->getManageStock(); $schema["is_in_stock"] = (bool) $stockItem->getIsInStock(); + }else{ + $schema["inventory"] = (int) 0; + $schema["inventory_tracked"] = (bool) 0; + $schema["is_in_stock"] = (bool) $product->isSalable(); } $childKey = false; @@ -248,6 +262,10 @@ public function getProductSchema(Product $product) $childSchema["inventory_quantity"] = (int) $stockItem->getQty(); $childSchema["inventory_item"]["tracked"] = (bool) $stockItem->getManageStock(); $childSchema["inventory_item"]["is_in_stock"] = (bool) $stockItem->getIsInStock(); + }else{ + $childSchema["inventory_quantity"] = 0; + $childSchema["inventory_item"]["tracked"] = 0; + $childSchema["inventory_item"]["is_in_stock"] = (bool) $childProduct->isSalable(); } $schema[$childKey][] = $childSchema; } diff --git a/composer.json b/composer.json index 870abee..ca4ce45 100755 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php": ">=5.6" }, "type": "magento2-module", - "version": "1.0.5", + "version": "1.0.6", "license": [ "OSL-3.0", "AFL-3.0" diff --git a/etc/module.xml b/etc/module.xml index 0d411d3..6aeaa21 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -10,7 +10,7 @@ */ --> - +