Skip to content

Magento2.3.5: How to optimize product export scripts?

I Have a product export script that uses product collection to create an array and write the array to CSV, I want to know if this script can be optimized.

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

ini_set('memory_limit', '16G');

use MagentoFrameworkAppBootstrap;

include __DIR__ . '/../app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$obj->get('MagentoFrameworkRegistry')->register('isSecureArea', true);
$appState = $obj->get('MagentoFrameworkAppState');
$appState->setAreaCode('frontend');


$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$productCollectionFactory = $objectManager->get('MagentoCatalogModelResourceModelProductCollectionFactory');
$StockState = $objectManager->get('MagentoCatalogInventoryApiStockStateInterface');

$dirurl = '/var/www/html/magento245/pub/ProductExports/';
$chunk = 1;
$urlcsvfilename = 'productexport_' . $chunk . '.csv';
echo strftime('%c');
echo "n";
if (file_exists($dirurl . $urlcsvfilename)) {
    unlink($dirurl . $urlcsvfilename);
    echo "n unlink done n";
}

$batchSize = 50000;
$file = $dirurl . $urlcsvfilename;

echo $file, file_exists($file) ? " exists" : " does not exist", "n";
echo $file, is_readable($file) ? ' is readable' : ' is NOT readable', "n";
echo $file, is_writable($file) ? ' is writable' : ' is NOT writable', "n";

$fp = fopen($dirurl . $urlcsvfilename, "w");
$array[] = ["name", "description", "short_description", "url_key", "price", "sku", "qty", "product_type", "weight", "visibility", "meta_title", "categories", "stockstatus", "base_image", "base_image_label", "additional_attributes_manufacturer", "additional_attributes_condition", "additional_attributes_google_product_category"];

$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->getSelect()->reset(Zend_Db_Select::ORDER);
$collection->setOrder('entity_id', 'ASC');

$recordCount = 0; // Initialize a counter for the records
$counter = 0;

foreach ($collection as $product) {
        $categoryname = [];
        foreach ($product->getCategoryIds() as $categoryId) {
            $parentCategories = $objectManager->get('MagentoCatalogModelCategoryFactory')->create()->load($categoryId)->getParentCategories();
            foreach ($parentCategories as $cat) {
                $categoryname[] = $cat->getName();
            }
        }
        $product_category = implode(" / ", $categoryname);

        if ($StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId()) == 0) {
            $stockstatus = 'Out of Stock';
        } else {
            $stockstatus = 'In Stock';
        }

        $array[] = [
            $product->getName(),
            $product->getDescription(),
            $product->getShortDescription(),
            $product->getProductUrl(),
            $product->getFinalPrice(),
            $product->getSku(),
            $StockState->getStockQty($product->getId(), $product->getStore()->getWebsiteId()),
            $product->getTypeId(),
            $product->getData('weight'),
            $product->getAttributeText('visibility')->getText(),
            $product->getMetaTitle(),
            $product_category,
            $stockstatus,
            $product->getThumbnail(),
            $product->getThumbnailLabel(),

            $product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product),
            $product->getResource()->getAttribute('condition')->getFrontend()->getValue($product),
            $product->getResource()->getAttribute('google_product_category')->getFrontend()->getValue($product),
        ];

        $recordCount++;

        if ($recordCount >= $batchSize) {
            // Export the current batch to the CSV file
            foreach ($array as $d) {
                fputcsv($fp, $d);
            }
            echo "n Batch $chunk donen";

            // Reset the array and counter for the next batch
            $array = [];
            $recordCount = 0;
            $chunk++;

            // Close the current file and open a new one
            fclose($fp);
            $urlcsvfilename = "productexport_" . $chunk . '.csv';
            $fp = fopen($dirurl . $urlcsvfilename, "w");
            $array[] = ["name", "description", "short_description", "url_key", "price", "sku", "qty", "product_type", "weight", "visibility", "meta_title", "categories", "stockstatus", "base_image", "base_image_label", "additional_attributes_manufacturer", "additional_attributes_condition", "additional_attributes_google_product_category"];
        }
        $counter++;
}

if (!empty($array)) {
    // Export any remaining products
    foreach ($array as $d) {
        fputcsv($fp, $d);
    }
    echo "n Final batch donen";
}

fclose($fp);
echo "nTotal Products Exported: " . $counter . "n";
echo strftime('%c');
echo "n";