1

There should be a possibility to setup a cron, which does the dump click on "start profile" button. I want to automatically start an import profile which I have setup in Magento CE 1.8.1, which is getting it's csv from local destination.

  • What have you tried? How technical are you - as this will take some code to implement. – Joseph at SwiftOtter Jan 3 '14 at 17:31
  • coding is no problem ;) – user3084928 Jan 3 '14 at 17:41
2

Based on your current knowledge of cron jobs, here is how I would instantiate the profile process. This could be as simple or complex as you would like it to be.

In your Cron.php file, put this code in your cron job method:

$profileId = Mage::getStoreConfig('path/to/config');

$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.');

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

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

echo "Export Complete. BatchID: " . $batchModel->getId() . "\n";

Code based on: http://www.magentocommerce.com/boards/viewthread/113901/P30/#t277875

  • 1
    profileid is the import profile right? I have done both above, but the cron doesn't schedule it (looked in the cron database table) – user3084928 Jan 3 '14 at 18:54
  • okay, now it works, but somehow the import doesn't work right... There comes and error like this in the popup: Starting Mage_Dataflow_Model_Convert_Adapter_Io :: load Loaded successfully: "testimport.csv". Starting Mage_Dataflow_Model_Convert_Parser_Csv :: parse Found 29033 rows. Starting catalog/convert_adapter_product :: parse <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de"> – user3084928 Jan 3 '14 at 19:46
  • Does the import normally work with that file? – Joseph at SwiftOtter Jan 3 '14 at 19:48
  • okay, I had to set the max_input_vars in php higher. now everything works. THANKS! – user3084928 Jan 3 '14 at 20:17
1

you have to follow below steps.

1) Create your module for that create below xml file in app/etc/module/Keyul_Testmodule.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Keyul_Testmodule>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
        </Keyul_Testmodule>
  </modules>
</config>

2). create the below directory structure app\code\local\Keyul\Testmodule and in that create the two folder etc and Model.

3) In etc folder create the config.xml file as below.

<?xml version="1.0"?>
<config>
  <modules>
    <Keyul_Testmodule>
      <version>0.1.0</version>
    </Keyul_Testmodule>
  </modules>
  <global>        
    <models>
      <testmodule>
    <class>Keyul_Testmodule_Model</class>
    <resourceModel>testmodule_mysql4</resourceModel>
  </testmodule>
</models>
  </global>    
    <crontab>
        <jobs>            
            <testmodule_mycustommethod>
                <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
                <run><model>testmodule/cron::MyCustomMethod</model></run>
            </testmodule_mycustommethod>
        </jobs>
    </crontab>
</config> 

4) create the cron.php file in Model folder as below.

<?php
  class Keyul_Testmodule_Model_Cron
  { 
   public function MyCustomMethod()
       {
    //do hear what ever your code .
   } 
  }

This cron job run every 5 min

example (*/5 * * * *) Every 5 minute run cron job.

for more reference http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job

  • well that sounds good and I could follow your steps so far, but where I am defining which import profile should be started? – user3084928 Jan 3 '14 at 17:53
  • Can i just paste the Dataprofile code in step 4 ?? – user3084928 Jan 3 '14 at 17:56
  • for that you have to set a different cron job and each cron job must gave one method so you can write your code there – Keyul Shah Jan 5 '14 at 2:57
  • 1
    if you paste data profile code in step 4 It might not working because you to first do analysis like how many cron job I want to create and based on that you have to distribute your code. – Keyul Shah Jan 5 '14 at 2:59
0

Finally I am using a php script. But something is still wrong. The CSV File has some data which updates some article attributes. Executed from Magento backend. Everything works. With the script it doesn't. There are no error messages. Nowhere... Any clues?

 <?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'] !== '<your server ip address>') {
//   die("You are not a cron job!");
//}


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

$profileId = 9;

/* 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"; */

/* Connect to Magento database */

sleep(400);

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


?> 
0

I apologise for the late reply. I got here through another search for myself about this script.

In order to run the script from shell or cron of cpanel you need sudo rights. So from shell you do "sudo php -f script.php" from cpanel you need to setup usr account.

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.