Magento ver. 2.4.2-p1

I have a Magento install with 3 unique websites. I need to export products for only 1 website at a time.

There does not appear to be an option for this in:

- System - Export - Entity type = Products

How to best do this?

1 Answer 1


Run below script :

protected $storeManager;
protected $productCollectionFactory;
protected $csvProcessor;

public function __construct(
    StoreManagerInterface $storeManager,
    CollectionFactory $productCollectionFactory,
    Csv $csvProcessor
) {
    $this->storeManager = $storeManager;
    $this->productCollectionFactory = $productCollectionFactory;
    $this->csvProcessor = $csvProcessor;

public function execute()
    $websiteId = 1; // Change this to the ID of the target website

    $productCollection = $this->productCollectionFactory->create();

    $exportData = [];
    $exportData[] = ['sku', 'name', 'price']; // Add more fields as needed

    foreach ($productCollection as $product) {
        $exportData[] = [$product->getSku(), $product->getName(), $product->getPrice()]; // Add more fields as needed

    // Generate and save the CSV file
    $exportPath = $this->getExportPath(); // Define a method to get the export file path
    $this->csvProcessor->saveData($exportPath, $exportData);

private function getExportPath()
    $mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
    $exportPath = 'export/products_for_website.csv';
    return $mediaDirectory->getAbsolutePath($exportPath);
  • Would I do this in phpMyAdmin? Or do I make it into a file and then access the file?
    – Allysin
    Jul 31, 2023 at 14:05
  • Create one controller and execute it at any action.and it is working. Aug 1, 2023 at 5:20
  • I googled how to create a controller in Magento 2 and it seems rather complicated. There is no way for me to execute this script in phpMyAdmin? Is there an existing controller somewhere in Magento I may be able to (re)use for this and just plug your code in it?
    – Allysin
    Aug 1, 2023 at 10:13
  • yes you can add it in any button action controller and done that , Like on create account or login customer, or any other button action, or you know the any custom action, you can execute in. Aug 1, 2023 at 11:56
  • I know this is a lot to ask but any chance you could put up steps for the easiest way to accomplish this please?
    – Allysin
    Aug 1, 2023 at 19:55

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.