Skip to content

[BUG] Wrong loop break condition in indexPrices() prevents full price indexation #1198

@csoon1992

Description

@csoon1992

Prerequisites

Describe the bug and add attachments

When indexing product prices, the method indexPrices() uses an incorrect loop condition comparing $cursor (last indexed id_product) against $nbProducts (total number of products to index).

Because product IDs may not be sequential and are frequently greater than the total number of products, the loop exits after the first iteration, preventing full indexation.

Expected behavior

Actual result

The loop stops because:
$cursor < $nbProducts

fails, since:

  • $cursor is the last indexed product ID (example: 10391)
  • $nbProducts is the number of products to index (example: 3525)

IDs are not sequential, therefore the check is invalid.

Expected result
The loop should continue until the number of indexed products reaches $nbProducts, not until $cursor is less than $nbProducts.

Problematic code
From indexPrices():

} while (
    $cursor < $nbProducts
    && (Tools::getMemoryLimit() == -1 || Tools::getMemoryLimit() > memory_get_peak_usage())
    && $time_elapsed < $maxExecutiontime
);

Why this is a bug
$cursor is the last product ID returned from: indexPricesUnbreakable().
It is not a counter.

Therefore comparing it to the number of products makes the loop stop incorrectly.

Suggested fix
Replace
$cursor < $nbProducts

with:
$indexedProducts < $nbProducts

Since $indexedProducts already tracks real progress:
$indexedProducts += $length;

Steps to reproduce

  1. Install PrestaShop 8.x with ps_facetedsearch.
  2. Have more product IDs than active products (normal in real stores: deleted products, migrations, imports).
  3. Run price indexation with all products (full = true and default $length = 100).
  4. The indexation stops after the first batch.

PrestaShop version(s) where the bug happened

8.0.1

PHP version(s) where the bug happened

8.1.33

Your company or customer's name goes here (if applicable).

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Status

    Waiting for QA

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions