0

I have 50k products in my website and want update tier price . i have existing tier price for products, but based on existing product price i need to update customer price. i have created external root file and created one function. like in magento1 i did $products = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('id') ->addAttributeToFilter('status', 1)->addAttributeToFilter('visibility', 4) ->addAttributeToSort('created_at', 'desc'); Mage::getSingleton('core/resource_iterator')->walk( $products->getSelect(), array(array($this, '_Callback')),array() );

same thing i want with magento2 how to possible with magento2 collection, i want only call _Callback function with magento2 product collection so that it can work collection walk iteratorly.

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

$productcollection = $productCollection->addAttributeToSelect('name')->addAttributeToSelect('price')->addAttributeToSelect('entity_id')->setOrder('entity_id','ASC')->load();

Please give me solution.

0

This is just an example code, please inject correct repositories and models

<?php

use Magento\Framework\Model\ResourceModel\Iterator;
use Magento\Framework\Model\ResourceModel\IteratorFactory;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactoryInterface;
use Zend_Db_Select;

class RunSomeLongTask
{
    public const ORDER_STATE_VALUE = 'example';

    /** @var CollectionFactoryInterface  */
    private $orderCollectionFactory;

    /** @var IteratorFactory */
    private $iteratorFactory;

    /** @var SomeTask */
    private $someTask;

    /**
     * constructor.
     * @param CollectionFactoryInterface $orderCollectionFactory
     * @param IteratorFactory $iteratorFactory
     * @param SomeTask $someTask
     */
    public function __construct(
        CollectionFactoryInterface $orderCollectionFactory,
        IteratorFactory $iteratorFactory,
        SomeTask $someTask
    ) {
        $this->orderCollectionFactory = $orderCollectionFactory;
        $this->iteratorFactory = $iteratorFactory;
        $this->someTask = $someTask; // "important: remember create this class"
    }

    public function process(): void
    {
        $orderCollection = $this->orderCollectionFactory->create();

        $orderCollection
            ->getSelect()
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns(['entity_id', 'state', 'status'])
            ->where('state=?', self::ORDER_STATE_VALUE);

        /** @var Iterator $iterator */
        $iterator = $this->iteratorFactory->create();

        $iterator->walk($orderCollection->getSelect(), [[
            $this->someTask, // instance of class SomeTask
            'run' // the function in your class "SomeTask"
        ]]);
    }
}

class SomeTask
{
    public function run(array $args): void
    {
        // you will receive the row data here
        
        $orderId = $args['row']['entity_id'];

        // do your task here on individual row
    }
}

http://www.rosenborgsolutions.com/articles.php

| improve this answer | |
  • Thanks for your reply. how to use for products collection and root of magento file.can you please give me examples for product collection with root external file. – user3811650 Sep 28 at 6:35

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.