0

I want to export sku with magento id.

https://prnt.sc/njkYD1jkJrH0

I am trying to find how to export Product ID with a matching SKU

1
  • Below answer useful for you @kiran ?
    – Dotsquares
    Commented Dec 16, 2022 at 11:39

1 Answer 1

0

By default, the magento2 export function does not provide a product ID so we will have to create a standalone file on the root folder. But if you are using Magento's latest version then the file will be inside the pub folder.

Please create a product-export.php file inside the pub folder and then place this code.

<?php
use Magento\Framework\App\Bootstrap;

include '../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

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

$productCollection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$productCollection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

$collection = $productCollection->addAttributeToSelect(array('id','sku'))
                ->addAttributeToFilter('status', 1)
                ->load();
                
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="product.csv"');

$user_CSV[0] = array('Product ID', 'SKU');

$fp = fopen('php://output', 'wb');

foreach ($collection as $product)
{
    $user_CSV[] = array($product->getId(), $product->getSku());
}

foreach ($user_CSV as $indexValue)
{
    fputcsv($fp, $indexValue, ',');
}

fclose($fp);

?>

then hit this URL in browser /product-export.php then a CSV file will be downloaded.

Hope this will help from my side.

2
  • Hi @Dotsquares, I tried and working but showing only few sku instead of all
    – kiran
    Commented Dec 16, 2022 at 17:16
  • It should not happen as this code is properly tested and working as expected. I tried many instances even. How can I help you more apart from this?
    – Dotsquares
    Commented Dec 19, 2022 at 5:41

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.