I have a walk with large product collection. But I want to stop walk working. I don't have any idea for it. Here is my code:

        [[$this, 'callbackMappingProduct']],
            'product' => $this->_productFactory->create()

public function callbackMappingProduct($args){
     * @var $product \Magento\Catalog\Model\Product
    if(true == $this->hasStop) {
          //Here I wanna stop my walk collection.

Thank Any tips.


I do not think that you can do this natively.

You can create your custom iterator based on the Magento\Framework\Model\ResourceModel\Iterator and add some logic inside the walk function.

But another approach is to load bulk of data for large collection such as by 1000.

Example of batch implementation for Indexes:

You can also create your own class wich implements Iterator such as Magento\CatalogSearch\Model\Indexer\Fulltext\Action

Then use :

   Magento\Framework\Indexer\SaveHandler\Batch $this->batch

   foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
        // do ur stuff

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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