I've tried to get this working, but it can't seem to find a solution. I'm looking to run an existing dataflow profile which has an ID = 3, and has the import file name already configured.
All the research I've done, leads to some variation of the following code:
public function importProducts($profile_id = 3)
{
require_once('../app/Mage.php');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
// Instantiate a session for the "root" user.
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
// Load the dataflow profile.
$profile = Mage::getModel('dataflow/profile');
$profile->load($profile_id);
if (!$profile->getId()) {
exit("Profile with id #{$profile_id} does not exist.");
}
$profile->run();
$batchModel = Mage::getSingleton('dataflow/batch');
// Reporting.
$direction = ucwords($profile->getDirection());
$success = "{$direction} with id #{$batchModel->getId()} completed succesfully.\n";
echo $success;
return true;
}
Running the profile in question (ID = 3) from the Magento backend works perfectly, just can't seem to be able to trigger this from the PHP function above.
I'm looking for a way to trigger the "Import All Products" Dataflow Profile programmatically.
Other posts, questions, and sites I've come across but have had no success with them:
- http://phpmysqltalk.com/1718-magento-dataflow-exportimport-form-the-command-line.html
- https://www.variux.com/magento-1-8-ce-importexport-from-shell-on-bitnami/
- Run multiple Magento DataFlow profiles in sequence
- https://gist.github.com/ameenross/91b85beb45f1ff0a23c6
Any and all help will be much appreciated!
Thank you!
After much frustration, here's the answer that works:
Note that in this case I've configured the default Magento Dataflow Profile for Import All Products (ID: 3) to read in an XML import format, from a predefined file. This is a file I create as needed prior to running the import operations. (Refer to the screenshot in the question above)
Once you have your profile created you'll need 2 files:
You can place the files in the /magento/root/shell/ directory, or adjust the paths as needed if you're including in a separate location. Once in the directory, you can call trigger the operation via cron using:
The -f parameter above is to "parse and execute" the file being called.
Source for each file is:
importer.php
batch_import_processor.php
It's not too difficult to add to this code so you can run multiple profiles in sequence if that is something that you require. Otherwise, be sure to get the profile ID from the Magento backend and set the $profile_id variable with the desired value.
I've included calls to the Magento Log (Mage::log) with the respective echo statements below if you prefer that route. Edit as needed. The logs should be saved in the /magento/root/var/log/ directory. Be sure to enable this feature in the Magento backend by going to:
And setting enabled to "Yes" see below:
After running the importer.php file, you should have 2 log files in the /magento/root/var/log/ directory:
You may also have the exception.log file which is always enabled.
This is the only solution that I've found to work with Magento 1.9.1.0, thanks go out to Andrey for the inspiration.
Any thoughts or improvements are always welcome.