1

i would like to automatically export all products of my webshop at the first day of the month right after 00:00.

I do know how to export them manually with the export functionality and know how to set a cron for this.

But is there to export all products with the attributes? And preferably only all products with qty 1 or more ...

I have Magento 1.8.1 CE

1 Answer 1

3

I figured it out myself.

By using this code. Naming this: export_mail.php

<?php

/***********************
 * Import/Export Script to run Import/Export profile
 * from command line or cron. Cleans entries from dataflow_batch_(import|export) table
 ***********************/

$mageconf = './app/etc/local.xml';  // Mage local.xml config
$mageapp  = './app/Mage.php';       // Mage app location
$logfile  = 'export_data.log';      // Import/Export log file

/* uncomment following block when moved to server - to ensure this page is
 * not accessed from anywhere else
 */

if ($_SERVER['REMOTE_ADDR'] !== 'server ip') {
 die("You are not a cron job!");
}


/* System -> Import/Export -> Profiles get profile ID from
 * Magento Import/Export Profiles
 */

$profileId = 2;

/* Post run housekeeping table bloat removal
 * imports use "dataflow_batch_import" table
 * exports use "dataflow_batch_export" table
 */

$table = 'dataflow_batch_export';

/* Scan Magento local.xml file for connection information */

if (file_exists($mageconf)) {

$xml = simplexml_load_file($mageconf, NULL, LIBXML_NOCDATA);

$db['host'] = $xml->global->resources->default_setup->connection->host;
$db['name'] = $xml->global->resources->default_setup->connection->dbname;
$db['user'] = $xml->global->resources->default_setup->connection->username;
$db['pass'] = $xml->global->resources->default_setup->connection->password;
$db['pref'] = $xml->global->resources->db->table_prefix;

}

else {
    Mage::log('Export script failed to open Mage local.xml', null, $logfile);
    exit('Failed to open Mage local.xml');
}


/* Initialize profile to be run as Magento Admin and log start of export */

require_once $mageapp;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
$profile->load($profileId);
if (!$profile->getId()) {
    Mage::getSingleton('adminhtml/session')->addError('ERROR: Incorrect profile id');
}

Mage::log('Export ' . $profileId . ' Started.', null, $logfile);

Mage::register('current_convert_profile', $profile);
$profile->run();
$recordCount = 0;
$batchModel = Mage::getSingleton('dataflow/batch');

Mage::log('Export '.$profileId.' Complete. BatchID: '.$batchModel->getId(), null, $logfile);

echo "Export Complete. BatchID: " . $batchModel->getId() . "\n";
/* sent file */
require_once 'app/Mage.php';
umask( 0 );
Mage :: app( "default" );
$mail = new Zend_Mail();
$mail->setType(Zend_Mime::MULTIPART_RELATED);
$mail->setBodyHtml($html_body);
$mail->setFrom('[email protected]', 'Admin');
$mail->addTo('[email protected]', 'Info');
$mail->setSubject('export_product_stocks');
$dir = Mage::getBaseDir();
$path = $dir.DS.'var'.DS.'export'.DS.'export_product_stocks.csv';
$file = $mail->createAttachment(file_get_contents($path));
$file ->type        = 'text/csv';
$file ->disposition = Zend_Mime::DISPOSITION_INLINE;
$file ->encoding    = Zend_Mime::ENCODING_BASE64;
$file ->filename    = 'export_product_stocks.csv';
$mail->send();
echo "E-mail sent";


/* Connect to Magento database */

sleep(30);

mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
mysql_select_db($db['name']) or die(mysql_error());

/* Truncate dataflow_batch_(import|export) table for housecleaning */

$querystring = "TRUNCATE ".$db['pref'].$table;

mysql_query($querystring) or die(mysql_error());

?>

Which is being run with a cronjob by: wget http://mywebsite.com/export_mail.php

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.