Blog

We write about technical solutions for the roadblocks we face in everyday work and more on trends in technology. This will help some of you to tackle issues quickly and get some useful tips.

How can products be assigned to multiple stores programmatically in Magento 2?

Let’s say you have a Magento multi-website/store installation and you want to assign the products of a particular category to the new store. I wrote a few lines of code to accomplish this. <?php use Magento\Framework\App\Bootstrap; require __DIR__ . '/../app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $categoryIds = [41]; // Category IDs whose products you want to assign foreach ($categoryIds as $categoryId) { $category = $objectManager->create('Magento\Catalog\Model\Category')->load($categoryId); $productCollection = $category->getProductCollection(); foreach ...

Page 18 of 18« First...10...1415161718
2hats Logic HelpBot