Skip to content

Conversation

@ede-somogyi-algolia
Copy link
Collaborator

Re-opened PR instead of closed PR #246 to point to develop

Issue description

Setting the value of the Algolia_InStockThreshold site preference to 0 (the current default) breaks in_stock calculation.

The default value was changed from no value to 0 in #110 .

When setting up a new cartridge installation, a default value of 0 leads to all products always returning in_stock == true.

Context: SFCC didn’t require a default to be set before – the code treated leaving it empty as if there was no limit set and handled it correctly (i.e. didn’t take the value of this preference into account at all, falling back to the expected behavior of “treat a product as in-stock if it has at least 1 product in stock”).

The documentation shows this value as set to 1, which results in correct behavior.

#110 introduced an explicit 0 as the default value of this preference, which leads to problems due to the code returning in_stock === true if the product has at least 0 products in stock, so it always returns true for all products, which is erroneous behavior. Existing cartridge installations where there was no default value set should not encounter problems, but new installations will as all products will always return in_stock === true. To solve this, the logic was changed so that an ATS of 0 will never return true, no matter what value the Algolia_InStockThreshold preference is set to. This also guards against user error as the user can set 0 as the InStockThreshold, even if that’s not the default.

Changes in this PR

  • changed the default value of Algolia_InStockThreshold to 1 so new installations will now set this value
  • isInStock() in productFilter.js now returns true only if ATS > 0
  • when none of the variants are in stock, the isInStock() method didn't previously return anything, fixed
  • isInStoreStock() in productFilter.js now returns true only if ATS > 0
  • isCustomVariationGroupInStock() in productFilter.js now returns true only if ATS > 0
  • storeAvailability() in algoliaLocalizedProduct.js now returns true only if ATS > 0
  • productFilterConfig.example.js now shows the correct default of 1
  • made the description of the Algolia_InStockThreshold site preference more explicit

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes incorrect stock availability calculations caused by the Algolia_InStockThreshold site preference defaulting to 0. The core issue is that a threshold of 0 causes all products to be marked as in-stock (since ATS >= 0 is always true for any product). The fix updates the default to 1 and adds explicit ATS > 0 checks throughout the codebase to prevent threshold=0 from incorrectly marking products as in-stock.

Key Changes:

  • Changed default value of Algolia_InStockThreshold from 0.0 to 1.0 in system metadata
  • Added explicit ATS > 0 checks in all stock validation methods to guard against threshold=0 edge cases
  • Added fallback value || 1 when reading the preference to ensure correct behavior for existing installations

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
metadata/algolia/meta/system-objecttype-extensions.xml Updated default value of Algolia_InStockThreshold from 0.0 to 1.0
cartridges/int_algolia/cartridge/scripts/algolia/filters/productFilter.js Added ATS > 0 checks to isInStock(), isInStoreStock(), and new isCustomVariationGroupInStock() methods; added missing return statement
cartridges/int_algolia/cartridge/scripts/algolia/model/algoliaLocalizedProduct.js Added ATS > 0 check in storeAvailability() handler; simplified in_stock handler
cartridges/int_algolia/cartridge/scripts/algolia/model/algoliaProduct.js Added fallback value `
cartridges/int_algolia/cartridge/scripts/algolia/steps/algoliaProductIndex.js Added fallback value `
cartridges/int_algolia/cartridge/scripts/algolia/steps/algoliaProductDeltaIndex.js Added fallback value `
cartridges/int_algolia_sfra/cartridge/scripts/hooks/order/algoliaHooks.js Added fallback value `
cartridges/int_algolia/cartridge/configuration/productFilterConfig.example.js Updated documentation to show correct default value of 1
cartridges/bm_algolia/cartridge/templates/resources/algolia.properties Improved description clarity for InStockThreshold and IndexOutOfStock preferences
test/unit/int_algolia/scripts/algolia/helper/orderHelper.test.js Added variant data with ATS=5 to test fixtures
test/unit/int_algolia/scripts/algolia/helper/__snapshots__/orderHelper.test.js.snap Updated snapshots to reflect variants in test output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

var atsValue = invRecord ? invRecord.getATS().getValue() : 0;
return atsValue >= threshold;

return (atsValue > 0 && atsValue >= threshold);
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition atsValue > 0 && atsValue >= threshold is redundant when threshold >= 1. Since the default is now 1 and there's a fallback of || 1, consider simplifying to atsValue >= threshold.

Copilot uses AI. Check for mistakes.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ede-somogyi-algolia ede-somogyi-algolia self-assigned this Dec 23, 2025
* @param {Number} threshold
* @returns {Boolean} whether at least one of the variants is in stock
*/
function isCustomVariationGroupInStock(variationModel, threshold) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep the introduction of this helper in the dedicated feature branch

Copy link
Collaborator Author

@ede-somogyi-algolia ede-somogyi-algolia Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't, as the changes to the would-be helper are here as well which would be lost if I removed this from here. This will nicely fall into place once the other pull requests are merged into develop and this branch is rebased. The linter error will also be gone.

Copy link
Collaborator

@sbellone sbellone Dec 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as the changes to the would-be helper are here as well which would be lost if I removed this from here

Do "the changes" refer to the variantATSValue > 0 addition? I think it can be easily added on the attribute-slicing feature branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants