0

I have a Magento 1.9 store and already programmed an export script for exporting product data into a CSV file. After I turned the flat catalog on for an extension i missed some attributes SKU and Leverancier.

I Cold turn the flat catalog on and off while exporting but has anybody an idea of how to fix this with my code?

<?php
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->addAttributeToSelect('name');
$products->addAttributeToSelect('afmetingen');
$products->addAttributeToSelect('leverancier');
$products->addAttributeToSelect('price');
$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "name", "afmetingen", "leverancier", "price");
fputcsv( $fp, $csvHeader,";");

foreach ($products as $product){
$sku = $product->getSku();
$name = $product->getName();
$afmetingen = $product->getAfmetingen();
$leverancier = $product->getLeverancier();
$price = $product->getPrice();
fputcsv($fp, array($sku, $name, $afmetingen, $leverancier, $price), ";");
}
fclose($fp);

1 Answer 1

0

Some attributes are not included in flat tables. You can try to disable flat products on the fly with:

$flatProducts = Mage::helper('catalog/product_flat')->getProcess();
$flatStatus = $process->getStatus();
$flatProducts->setStatus(Mage_Index_Model_Process::STATUS_RUNNING);

and re-enable flat status afterwards:

$flatProducts->setStatus($flatStatus);
4
  • Thank you for your answer. But i am getting an error by trying this.
    – Dennis
    Aug 18, 2017 at 7:59
  • Fatal error: Uncaught exception 'Exception' with message 'Notice: Undefined variable: process in /var/www/vhosts/wwwwwww/httpdocs/export.php on line 11' in /var/www/vhosts/wwwwwwwwwwwww/httpdocs/app/code/core/Mage/Core/functions.php:245 Stack trace: #0 /var/www/vhosts/wwwwwwwwww/httpdocs/export.php(11): mageCoreErrorHandler(8, 'Undefined varia...', '/var/www/vhosts...', 11, Array) #1 {main} thrown in /var/www/vhosts/wwwwwwwwwwwww/httpdocs/app/code/core/Mage/Core/functions.php on line 245
    – Dennis
    Aug 18, 2017 at 7:59
  • $flatProducts = Mage::helper('catalog/product_flat')->getProcess(); $flatStatus = $process->getStatus(); $flatProducts->setStatus(Mage_Index_Model_Process::STATUS_RUNNING); $products = Mage::getModel('catalog/product')->getCollection(); $products->addAttributeToSelect('name'); $products->addAttributeToSelect('afmetingen'); $products->addAttributeToSelect('leverancier'); $products->addAttributeToSelect('price'); $fp = fopen('exports.csv', 'w');
    – Dennis
    Aug 18, 2017 at 8:14
  • Still getting the same error. Do I need to replace $products by $flatProducts as well?
    – Dennis
    Aug 18, 2017 at 8:14

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.