简体   繁体   中英

Export product data to CSV with PHP from Magento with flat catalog

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM