1

I need to place every category in a separate category column.

Like Category_1, Category_2, Category_3.....

enter image description here

        $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
        $cat_name = array();
        $cat_array = $product->getCategoryIds();
        for($k = 0; $k < count($cat_array); $k++)
        {
            $id =  $cat_array[$k];
            $cat->load($id);
            $cat_name[] =  $cat->getName();
        }
        $_cate_name = implode(',', $cat_name);

Full code

<?php
ini_set('memory_limit','2048M');
ini_set('max_execution_time',3600);
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
    $products = Mage::getModel("catalog/product")->getCollection();
    $products = Mage::getModel("catalog/product")->getCollection();
    $products->addAttributeToSelect('mgs_brand');
    $products->addAttributeToSelect('manufacturer');
    $products->addAttributeToSelect('qty');
    $products->addAttributeToSelect('category_name');
    $products->addAttributeToSelect('name');
    $products->addAttributeToSelect('description');
    $products->addAttributeToSelect('short_description');
    $products->addAttributeToSelect('url_key');
    $products->addAttributeToSelect('price');
    $products->addAttributeToSelect('ean');
    $products->addAttributeToSelect('sku');
    $products->addAttributeToSelect('category_ids');
    //$products->addAttributeToFilter('status', 1);//optional for only   enabled products
    //$products->addAttributeToFilter('visibility', 4);//optional for   products only visible in catalog and search
    $fp = fopen('exports.csv', 'w');
    $csvHeader = array("Visible", "Brand", "Supplier", "NL_Title_Short", "NL_Title_Long", "NL_Description_Short", "NL_Description_Long", "NL_URL", "NL_Variant", "Price", "Price_Old", "Price_Cost", "Price_Unit", "Unit", "Tax", "Stock_Track", "Stock_Disable_Sold_Out", "Stock_Level", "Stock_Min", "Stock_Alert", "Article_Code", "EAN", "SKU", "Category");
    fputcsv( $fp, $csvHeader,",");
    $cat_name = array();
    $cat_array = array();
    $cat = Mage::getModel('catalog/category');
    foreach ($products as $product)
    {
        $brand = $product->getMgs_brand();
        $supplier = $product->getManufacturer();

        $qty = $product->getQty();

        $sku = $product->getSku();
        $name = $product->getName();
        $desc = $product->getDescription();
        $shortDesc = $product->getShort_description();
        $urlKey = $product->getUrl_key();
        $price = $product->getPrice();
        $ean = $product->getEan();
        $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
        $cat_name = array();
        $cat_array = $product->getCategoryIds();
        for($k = 0; $k < count($cat_array); $k++)
        {
            $id =  $cat_array[$k];
            $cat->load($id);
            $cat_name[] =  $cat->getName();
        }
        $_cate_name = implode(',', $cat_name);

        fputcsv($fp, array(
            'Y',
            $brand,
            $supplier,
            $name,
            $name,
            $shortDesc,
            $desc,
            $urlKey,
            'Product',
            $price,
            '0',
            '0',
            ' ',
            ' ',
            '21',
            'Y',
            'Y',
            $qty,
            '0',
            '5',
            ' ',
            $ean,
            $sku,
            $_cate_name

        )
        ,
        ","
        );
    }
fclose($fp);

?>

1

you can use the code below to add the category names in separate columns in csv file, just modified your code a bit.

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect(array('sku','price','ean','category_ids','mgs_brand','manufacturer','qty','category_name','name','description','short_description','url_key'));
//$products->addAttributeToFilter('status', 1);//optional for only   enabled products
//$products->addAttributeToFilter('visibility', 4);//optional for   products only visible in catalog and search
$fp = fopen('exports.csv', 'w');
$csvHeader = array("Visible", "Brand", "Supplier", "NL_Title_Short", "NL_Title_Long", "NL_Description_Short", "NL_Description_Long", "NL_URL", "NL_Variant", "Price", "Price_Old", "Price_Cost", "Price_Unit", "Unit", "Tax", "Stock_Track", "Stock_Disable_Sold_Out", "Stock_Level", "Stock_Min", "Stock_Alert", "Article_Code", "EAN", "SKU", "Category");
fputcsv( $fp, $csvHeader,",");
$cat_name = array();
$cat_array = array();
$cat = Mage::getModel('catalog/category');
foreach ($products as $product)
{
    $brand = $product->getMgs_brand();
    $supplier = $product->getManufacturer();

    $qty = $product->getQty();

    $sku = $product->getSku();
    $name = $product->getName();
    $desc = $product->getDescription();
    $shortDesc = $product->getShort_description();
    $urlKey = $product->getUrl_key();
    $price = $product->getPrice();
    $ean = $product->getEan();
    $categoryIds = implode('|', $product->getCategoryIds());//change the category separator if needed
    $cat_name = array();
    $cat_array = $product->getCategoryIds();
    for($k = 0; $k < count($cat_array); $k++)
    {
        $id =  $cat_array[$k];
        // loading models inside the loop is bad practice, use joins instead 
        $cat->load($id);
        $cat_name[] =  $cat->getName();
    }
    $_cate_name = implode(',', $cat_name);

    $csvData = array();     
    $csvData[] = 'Y';
    $csvData[] = $brand;
    $csvData[] = $supplier;
    $csvData[] = $name;
    $csvData[] = $name;
    $csvData[] = $shortDesc;
    $csvData[] = $desc;
    $csvData[] = $urlKey;
    $csvData[] = 'Product';
    $csvData[] = $price;
    $csvData[] = 0;
    $csvData[] = 0;
    $csvData[] = '';
    $csvData[] = '';
    $csvData[] = '21';
    $csvData[] = 'Y';
    $csvData[] = 'Y';
    $csvData[] = $qty;
    $csvData[] = '0';
    $csvData[] = '5';
    $csvData[] = '';
    $csvData[] = $ean;
    $csvData[] = $sku;

    /* use array_walk to add the Category Names in separate columns */
    array_walk(
        $cat_name,
        function ($categoryName) use (&$csvData) {
            $csvData[] = $categoryName;
        }
    );

    fputcsv($fp, $csvData,",");
}
fclose($fp); 
|improve this answer|||||
  • Hi Haijerome thank you for your replay! – MediaDeveloper Nov 21 '19 at 11:13
  • Works great! Is it possible to auto-generate a custom CSV header like Category_1, Category_2, Category_3? – MediaDeveloper Nov 21 '19 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.