Skip to content

Commit

Permalink
Fix product availability for grouped products (#333)
Browse files Browse the repository at this point in the history
* fix

* version

* feedback

* feedback

* remove unused functions
  • Loading branch information
juan-gm authored Jul 15, 2024
1 parent 920a491 commit 5fbfde4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 58 deletions.
74 changes: 32 additions & 42 deletions Helper/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\App\Helper\Context;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Module\Manager;
use Magento\GroupedProduct\Model\Product\Type\Grouped;
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
use Magento\InventorySales\Model\ResourceModel\GetAssignedStockIdForWebsite;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
Expand Down Expand Up @@ -55,21 +56,6 @@ public function __construct(
parent::__construct($context);
}

/**
* Get quantity and stock status
*
* @param ProductModel $product
* @param int|null $stockId
*
* @return string
*/
public function getQuantityAndStockStatus(ProductModel $product, ?int $stockId = null)
{
return $this->isMsiActive() ?
$this->getQuantityAndStockStatusWithMSIMessage($product, $stockId) :
$this->getQuantityAndStockStatusWithoutMSIMessage($product);
}

/**
* Get quantity and product availability
*
Expand Down Expand Up @@ -126,27 +112,48 @@ private function getQuantityAndStockStatusWithMSI(ProductModel $product, ?int $s
{
$stockItemData = $this->getStockItemData($product->getSku(), $stockId);
$qty = $stockItemData[GetStockItemDataInterface::QUANTITY];
$availability = $stockItemData[GetStockItemDataInterface::IS_SALABLE];
$availability = $this->isProductAvailable($product, $stockId);

return [$qty, $availability];
}

/**
* Get quantity and stock status for environments with MSI dependency
* Get info about the availability of a product
* If a product is a grouped product, we consider it is available
* if any of its associated products is salable
*
* @param ProductModel $product
* @param int|null $stockId
*
* @return string
* @return boolean
*/
private function getQuantityAndStockStatusWithMSIMessage(ProductModel $product, ?int $stockId = null)
{
$qtyAndAvailability = $this->getQuantityAndStockStatusWithMSI($product, $stockId);
$qtyAndAvailability[1] = $qtyAndAvailability[1] ? $this->getInStockLabel(): $this->getOutOfStockLabel();
private function isProductAvailable(ProductModel $product, ?int $stockId = null)
{
if ($product->getTypeId() == Grouped::TYPE_CODE) {
$associatedProducts = $product->getTypeInstance()->getAssociatedProducts($product);
foreach ($associatedProducts as $associatedProduct) {
if ($this->isProductSalable($associatedProduct, $stockId)) {
return true;
}
}
return false;
}

return $this->isProductSalable($product, $stockId);
}

return implode(' - ', array_filter($qtyAndAvailability, function ($item) {
return $item !== null;
}));
/**
* Get info about the salability of any product
*
* @param ProductModel $product
* @param int|null $stockId
*
* @return boolean
*/
private function isProductSalable(ProductModel $product, ?int $stockId = null)
{
$stockItemData = $this->getStockItemData($product->getSku(), $stockId);
return $stockItemData[GetStockItemDataInterface::IS_SALABLE];
}

/**
Expand Down Expand Up @@ -202,23 +209,6 @@ private function getQuantityAndStockStatusWithoutMSI(ProductModel $product)
return [$qty, $availability];
}

/**
* Get quantity and stock status for environments without MSI dependency
*
* @param ProductModel $product
*
* @return string
*/
private function getQuantityAndStockStatusWithoutMSIMessage(ProductModel $product)
{
$qtyAndAvailability = $this->getQuantityAndStockStatusWithoutMSI($product);
$qtyAndAvailability[1] = $qtyAndAvailability[1] ? $this->getInStockLabel(): $this->getOutOfStockLabel();

return implode(' - ', array_filter($qtyAndAvailability, function ($item) {
return $item !== null;
}));
}

/**
* Get product availability for environments without MSI dependency
*
Expand Down
14 changes: 0 additions & 14 deletions Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,20 +460,6 @@ public function getAttributeArray(ProductModel $product, string $attributeCode):
return $this->getAttribute($product, $attributeCode);
}

/**
* Get quantity and stock status
*
* @param ProductModel $product
* @param int|null $stockId
*
* @return string
* @throws LocalizedException
*/
public function getQuantityAndStockStatus(ProductModel $product, ?int $stockId = null): string
{
return $this->inventoryHelper->getQuantityAndStockStatus($product, $stockId);
}

/**
* Get product availability
*
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "doofinder/doofinder-magento2",
"version": "0.14.3",
"version": "0.14.4",
"description": "Doofinder module for Magento 2",
"type": "magento2-module",
"require": {
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="Doofinder_Feed" setup_version="0.14.3">
<module name="Doofinder_Feed" setup_version="0.14.4">
<sequence>
<module name="Magento_Integration" />
</sequence>
Expand Down

0 comments on commit 5fbfde4

Please sign in to comment.