Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,20 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['8.2', '8.3']
php-version: ['8.2', '8.3', '8.4']
steps:
- uses: actions/checkout@v4
with:
path: module
# Magento 2.4.7 does not run on PHP 8.4; that version gets the lint and isolated checks only.
- uses: actions/checkout@v4
if: matrix.php-version != '8.4'
with:
repository: magento/magento2
ref: 2.4.7-p10
path: magento
- uses: actions/checkout@v4
if: matrix.php-version != '8.4'
with:
repository: magento/inventory
ref: 1.2.7-p10
Expand All @@ -77,12 +80,14 @@ jobs:
find Api Block Console Controller Model Observer Plugin Setup Test view registration.php \( -name '*.php' -o -name '*.phtml' \) -print0 | xargs -0 -n1 php -l
for test in Test/*.test.php; do php "$test"; done
- name: Install Magento unit test dependencies
if: matrix.php-version != '8.4'
run: |
cp -R ../inventory/InventoryApi ../inventory/InventorySalesApi ../magento/app/code/Magento/
mkdir -p ../magento/app/code/Hellotext
ln -s "$GITHUB_WORKSPACE/module" ../magento/app/code/Hellotext/Tracking
composer --working-dir=../magento install --no-interaction --prefer-dist --no-progress
- name: Run PHPUnit against Magento 2.4.7-p10
if: matrix.php-version != '8.4'
working-directory: magento
run: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Hellotext/Tracking/Test/Unit

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

## Unreleased
## 1.0.1 - 2026-10-19

- Install on stores without Magento Inventory (MSI): stock checks use Magento's default stock when MSI is not enabled.
- Allow the fonts of Hellotext popups, webchat and forms in the Content Security Policy.
- Allow PHP 8.4, used by Magento 2.4.8.

## 1.0.0 - 2026-10-17

- Track page views, products, carts and checkout activity through Hellotext.js.
- Display Hellotext popups, webchat, WhatsApp widgets and forms.
Expand Down
59 changes: 51 additions & 8 deletions Model/StockManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
use Hellotext\Tracking\Model\Data\StockResult;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Webapi\Rest\Response;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
Expand All @@ -19,13 +23,16 @@

class StockManagement implements StockManagementInterface
{
// Magento Inventory (MSI) is optional: its services are resolved only when it is enabled,
// so stores that removed or disabled it can still install and compile the module.
public function __construct(
private CollectionFactory $collections,
private ScopeConfigInterface $config,
private StoreManagerInterface $stores,
private StockResolverInterface $stockResolver,
private AreProductsSalableInterface $salability,
private Response $response
private Response $response,
private ModuleManager $modules,
private ObjectManagerInterface $objects,
private StockRegistryInterface $legacyStock
) {}

public function getStock(array $productIds): StockResultInterface
Expand Down Expand Up @@ -66,11 +73,9 @@ public function getStock(array $productIds): StockResultInterface

$salable = [];
if ($skus !== []) {
$website = $this->stores->getWebsite((int) $store->getWebsiteId());
$stock = $this->stockResolver->execute('website', (string) $website->getCode());
foreach ($this->salability->execute(array_values($skus), (int) $stock->getStockId()) as $result) {
$salable[$result->getSku()] = $result->isSalable();
}
$salable = $this->inventoryAvailable()
? $this->inventorySalability(array_values($skus), (int) $store->getWebsiteId())
: $this->legacySalability(array_values($skus));
foreach ($skus as $sku) {
if (!array_key_exists($sku, $salable)) {
throw new LocalizedException(__('Inventory did not return a result for every product.'));
Expand All @@ -85,4 +90,42 @@ public function getStock(array $productIds): StockResultInterface
);
return new StockResult($installation, $storeId, $business, $items);
}

// Removing the MSI packages can leave a stale enabled flag in app/etc/config.php, so its classes must exist too.
private function inventoryAvailable(): bool
{
return $this->modules->isEnabled('Magento_InventorySalesApi')
&& interface_exists(StockResolverInterface::class)
&& interface_exists(AreProductsSalableInterface::class);
}

/** @return array<string, bool> */
private function inventorySalability(array $skus, int $websiteId): array
{
$website = $this->stores->getWebsite($websiteId);
$stock = $this->objects->get(StockResolverInterface::class)->execute('website', (string) $website->getCode());
$salable = [];
foreach ($this->objects->get(AreProductsSalableInterface::class)->execute($skus, (int) $stock->getStockId()) as $result) {
$salable[$result->getSku()] = $result->isSalable();
}
return $salable;
}

/**
* Without MSI, Magento keeps one global stock whose status already accounts for quantity and backorders.
*
* @return array<string, bool>
*/
private function legacySalability(array $skus): array
{
$salable = [];
foreach ($skus as $sku) {
try {
$salable[$sku] = (int) $this->legacyStock->getProductStockStatusBySku($sku) === 1;
} catch (NoSuchEntityException) {
$salable[$sku] = false;
}
}
return $salable;
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Connect your Magento store to Hellotext to track storefront activity and orders,

## Requirements

- Magento Open Source 2.4.7
- PHP 8.2 or 8.3
- Magento Open Source 2.4 (tested on 2.4.7)
- PHP 8.2, 8.3 or 8.4
- Magento Inventory (MSI) is optional; without it, stock checks use Magento's default stock
- An HTTPS storefront and a Magento API reachable by Hellotext

## Install
Expand Down
2 changes: 1 addition & 1 deletion Test/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Running tests

Use Node.js 22 and PHP 8.2 or 8.3. From the module directory:
Use Node.js 22 and PHP 8.2, 8.3 or 8.4. From the module directory:

```sh
composer validate
Expand Down
36 changes: 35 additions & 1 deletion Test/Unit/Model/StockManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\CatalogInventory\Api\StockRegistryInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Webapi\Rest\Response;
use Magento\InventoryApi\Api\Data\StockInterface;
use Magento\InventorySalesApi\Api\AreProductsSalableInterface;
Expand All @@ -29,6 +33,8 @@ class StockManagementTest extends TestCase
private Collection $collection;
private AreProductsSalableInterface $salability;
private StockResolverInterface $resolver;
private StockRegistryInterface $legacyStock;
private bool $inventoryEnabled = true;

protected function setUp(): void
{
Expand Down Expand Up @@ -61,7 +67,35 @@ protected function setUp(): void
$response->expects(self::once())->method('setHeader')->with(
'Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store', true
);
$this->subject = new StockManagement($collections, $config, $stores, $this->resolver, $this->salability, $response);
$modules = $this->createMock(ModuleManager::class);
$modules->method('isEnabled')->with('Magento_InventorySalesApi')->willReturnCallback(fn (): bool => $this->inventoryEnabled);
$objects = $this->createMock(ObjectManagerInterface::class);
$objects->method('get')->willReturnMap([
[StockResolverInterface::class, $this->resolver],
[AreProductsSalableInterface::class, $this->salability],
]);
$this->legacyStock = $this->createMock(StockRegistryInterface::class);
$this->subject = new StockManagement($collections, $config, $stores, $response, $modules, $objects, $this->legacyStock);
}

public function testFallsBackToTheGlobalStockWhenInventoryIsDisabled(): void
{
$this->inventoryEnabled = false;
$this->collection->method('getItems')->willReturn([
$this->product(11, 'SIMPLE'), $this->product(12, 'GONE'), $this->product(13, 'SOLD-OUT')
]);
$this->legacyStock->method('getProductStockStatusBySku')->willReturnCallback(static function (string $sku): int {
if ($sku === 'GONE') {
throw new NoSuchEntityException(__('Missing'));
}
return $sku === 'SIMPLE' ? 1 : 0;
});
$this->resolver->expects(self::never())->method('execute');
$this->salability->expects(self::never())->method('execute');

$result = $this->subject->getStock([11, 12, 13]);

self::assertSame([true, false, false], array_map(static fn ($item) => $item->getSalable(), $result->getItems()));
}

public function testChecksSimpleProductsParentsAndExactChildrenTogetherInTheWebsiteStock(): void
Expand Down
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "hellotext/magento",
"description": "Connect Magento Open Source stores to Hellotext: storefront activity, customer identity, orders and catalog/customer import.",
"type": "magento2-module",
"version": "1.0.0",
"version": "1.0.1",
"license": "proprietary",
"keywords": [
"magento",
Expand All @@ -20,24 +20,27 @@
"docs": "https://github.com/hellotext/magento/blob/main/docs/getting-started.md"
},
"require": {
"php": ">=8.2 <8.4",
"php": ">=8.2 <8.5",
"magento/framework": "^103.0",
"magento/module-backend": "^102.0",
"magento/module-catalog": "^104.0",
"magento/module-catalog-inventory": "^100.4",
"magento/module-checkout": "^100.4",
"magento/module-config": "^101.2",
"magento/module-configurable-product": "^100.4",
"magento/module-cookie": "^100.4",
"magento/module-csp": "^100.4",
"magento/module-customer": "^103.0",
"magento/module-integration": "^100.4",
"magento/module-inventory-sales-api": "^1.2",
"magento/module-newsletter": "^100.4",
"magento/module-quote": "^101.2",
"magento/module-sales": "^103.0",
"magento/module-store": "^101.1",
"magento/module-webapi": "^100.4"
},
"suggest": {
"magento/module-inventory-sales-api": "Checks product availability in each website's stock when Magento Inventory (MSI) is installed."
},
"autoload": {
"files": [
"registration.php"
Expand Down
5 changes: 5 additions & 0 deletions etc/csp_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<value id="hellotext-assets" type="host">https://www.hellotext.com</value>
</values>
</policy>
<policy id="font-src">
<values>
<value id="hellotext-assets" type="host">https://www.hellotext.com</value>
</values>
</policy>
<policy id="connect-src">
<values>
<value id="hellotext-production-api" type="host">https://api.hellotext.com</value>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<module name="Hellotext_Tracking">
<sequence>
<module name="Magento_Catalog"/>
<module name="Magento_CatalogInventory"/>
<module name="Magento_Checkout"/>
<module name="Magento_Quote"/>
<module name="Magento_Customer"/>
<module name="Magento_ConfigurableProduct"/>
<module name="Magento_Cookie"/>
<module name="Magento_Csp"/>
<module name="Magento_Integration"/>
<module name="Magento_InventorySalesApi"/>
<module name="Magento_Webapi"/>
<module name="Magento_Newsletter"/>
<module name="Magento_Backend"/>
Expand Down
Loading