1

We are enabling a disabled product with \Magento\Catalog\Model\Product\Action::updateAttributes

$this->productAction->updateAttributes([$magentoProductId], [
     'status' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED
], 0);

This leads to the product not beeing displayed in the categories, because of the price index. (if I run a full reindex, it appears)

When looking into the method

public function updateAttributes($productIds, $attrData, $storeId)
{
    $this->_eventManager->dispatch(
        'catalog_product_attribute_update_before',
        ['attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId]
    );

    $this->_getResource()->updateAttributes($productIds, $attrData, $storeId);
    $this->setData(
        ['product_ids' => array_unique($productIds), 'attributes_data' => $attrData, 'store_id' => $storeId]
    );

    if ($this->_hasIndexableAttributes($attrData)) {
        $this->_productEavIndexerProcessor->reindexList(array_unique($productIds));
    }

    $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
    if (!$categoryIndexer->isScheduled()) {
        $categoryIndexer->reindexList(array_unique($productIds));
    }
    return $this;
}

We see that it only triggers Category and EAV indexer. Why is this limitation?

Is there another quick way to enable a product (without doing a full save operation)?

Should I just trigger all the other indexes manually?

EDIT: As a workaround

n98-magerun2 index:reindex catalog_product_price;

helps, but adding

    $priceIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Price\Processor::INDEXER_ID);
    if (!$priceIndexer->isScheduled()) {
        $priceIndexer->reindexList(array_unique($productIds));
    }

does not seem to help

UPDATE:

  • Index is configured on save

  • I still added the patch with the price Indexer and now update the price to a random value as well (customer_group is a custom attribute)

          $this->productAction->updateAttributes([$magentoProductId], [
              'customer_group' => $groups,
              'status' => $magentoVisibility,
              'price' => rand(0,1000),
          ], 0);
    

Still the same result

UPDATE2: Based on Diana's answer, this updates the price index as a workaround:

        $this->productAction->updateAttributes([$magentoProductId], [
            'customer_group' => $groups,
            'status' => $magentoVisibility,
        ], 0);

        $this->productAction->updateAttributes([$magentoProductId], [
            'price' => rand(0,1000),
        ], 0);
1
  • How is your index configured? On schedule or on save? If it's on schedule did you verify your cron is running?
    – Daniel
    May 20, 2021 at 8:02

1 Answer 1

1
+150

Since you mentioned that the snippet of code which would reindex the product IDs by schedule (partial index) is not helping, I think this happens because during the updateAttributes() call, the changelog is not updated for those entities with respect to price data (basically the price index is not invalidated). Which would be normal, as you are not updating the price, but another EAV attribute.

You could validate this idea by updating the price of a product via the same method and see if the index is properly updated - I would say it should be since the triggers set at db level would detect the change in the price table and will update the changelog.

And since you want to avoid a save() operation (which would indeed trigger the correct partial indexation, price included), I would still use this as starting point and see what happens during a save operation with all its hooks and triggers, maybe you find something relevant there.

3
  • Thanks. Updating the price does not seem to help :-(
    – Alex
    May 20, 2021 at 8:24
  • Update2: Updating the price in the next call helps :)
    – Alex
    May 20, 2021 at 8:29
  • 1
    ok :) i meant you should update the price and see if the price is properly propagated to track the price index invalidation due to changes to the price table. it was just to validate that because you are not updating the price per se, there is no reason to index the price by default :)
    – Diana
    May 20, 2021 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.