I want to export sku with magento id.
I am trying to find how to export Product ID with a matching SKU
I want to export sku with magento id.
I am trying to find how to export Product ID with a matching SKU
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.