public function saveProduct($_id, $_data = false) { $_product = \Mage::getModel('catalog/product')->setStoreId('0')->load($_id); $_validData = false; if ($_product && $_data) { // MAGMI require_once "/home/www/ecommerce/shop01/webapps/magmi/inc/magmi_defs.php"; //Datapump include require_once "magmi_datapump.php"; // create a Product import Datapump using Magmi_DatapumpFactory $dp = \Magmi_DataPumpFactory::getDataPumpInstance("productimport"); // Begin import session with a profile & running mode, here profile is "default" & running mode is "create". // Available modes: "create" creates and updates items, "update" updates only, "xcreate creates only. // Important: for values other than "default" profile has to be an existing magmi profile $dp->beginImportSession("default", "update"); $_magmiData = array("store" => "admin", "sku" => $_product->getSku()); foreach ($_data as $_name => $_value) { if ($_name == 'setcategoryids') { // push new data onto mamgmi import $_magmiData += array("category_ids" => $_value); $_validData = true; continue; } // push new data onto mamgmi import $_magmiData += array($_name => str_replace('__and__', '&', $_value)); $_validData = true; } if ($_validData) { $dp->ingest($_magmiData); $dp->endImportSession(); return array('magmi' => true, 'attributename' => $_name, 'sku' => $_product->getSku(), 'name' => $_product->getName()); } } return false; }
public function testExistingProfileFromCustomFile() { $conf = Magmi_Config::getInstance(); $conf->load(__DIR__ . "/test.ini"); $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); $dp->beginImportSession("xmlimport", "create"); $ep = $dp->getEngine()->getPluginClasses(); $this->assertContains('CategoryImporter', $ep['itemprocessors']); $this->assertContains('ImageAttributeItemProcessor', $ep['itemprocessors']); $this->assertContains('ItemIndexer', $ep['itemprocessors']); $this->assertContains('GenericMapperProcessor', $ep['itemprocessors']); $dp->endImportSession(); }
public function testCatMultiRoot() { $conf = Magmi_Config::getInstance(); $conf->load(__DIR__ . "/test.ini"); $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); $reader = new Magmi_CSVReader(); $reader->initialize(array("CSV:filename" => __DIR__ . '/categories/category_multiroot.csv')); $reader->openCSV(); $reader->getColumnNames(); $dp->beginImportSession("catconf", "create", new FileLogger(__DIR__ . '/test.log')); while ($item = $reader->getNextRecord()) { $dp->ingest($item); } $dp->endImportSession(); $reader->closeCSV(); }
static function getDataPumpInstance($pumptype) { if (self::$_factoryprops == null) { self::$_factoryprops = new Properties(); self::$_factoryprops->load(dirname(__FILE__) . DIRSEP . "pumpfactory.ini"); } $pumpinfo = self::$_factoryprops->get("DATAPUMPS", $pumptype, ""); $arr = explode("::", $pumpinfo); if (count($arr) == 2) { $pumpfile = $arr[0]; $pumpclass = $arr[1]; try { require_once dirname(__FILE__) . DIRSEP . "{$pumpfile}.php"; $pumpinst = new $pumpclass(); } catch (Exception $e) { $pumpinst = null; } } else { echo "Invalid Pump Type"; } return $pumpinst; }
public function test_CanInjectData() { $this->itemholder->setMagmi(\Magmi_DataPumpFactory::getDataPumpInstance("productimport"), 'travis', ItemHolder::MAGMI_CREATE_UPDATE)->import(); }
class TestLogger { /** * logging methos * @param string $data : log content * @param string $type : log type */ public function log($data, $type) { echo "{$type}:{$data}\n"; } } /** * create a Product import Datapump using Magmi_DatapumpFactory */ $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); /** * Start import session * with : * - profile : test_ptj * - mode : create * - logger : an instance of the class defined above */ /** * FOR THE SAMPLE TO WORK CORRECTLY , YOU HAVE TO DEFINE A test_ptj profile with : * UPSELL/CROSS SELL, ITEM RELATER, CATEGORIES IMPORTER/CREATOR selected * ON THE FLY INDEXER IS RECOMMENDED (better endimport performance) * Reindexer needed also to have products show up on front : select all but "catalog_category_product" & "url_rewrite" (both are handled by on the fly indexer) */ $dp->beginImportSession("test_ptj", "create", new TestLogger()); /* Create 5000 items , with every 100 :
public function testGrouped() { $conf = Magmi_Config::getInstance(); $conf->load(__DIR__ . "/test.ini"); $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); $dp->beginImportSession("grouped", "create", new FileLogger(__DIR__ . "/log_" . __FUNCTION__ . ".txt")); //import 10 simples for ($i = 0; $i < 10; $i++) { $item = array("sku" => "SG" . str_pad($i, 4, "0", STR_PAD_LEFT) . "_item", "name" => "simple item {$i}", "description" => "test description", "short_description" => "test short desc", "type" => "simple", "attribute_set" => "apparel", "weight" => 0, "price" => 10, "qty" => 1, "visibility" => "1"); $dp->ingest($item); unset($item); } //import configurable $item = array("sku" => "G000", "name" => "grouped item 0", "description" => "config desc", "short_description" => "config short", "type" => "grouped", "is_in_stock" => 1, "attribute_set" => "apparel", "configurable_attributes" => "color", "grouped_skus" => "SG0001_item,SG0003_item,SG0005_item", "category_ids" => "2", "visibility" => "4"); $dp->ingest($item); $dp->endImportSession(); }
/** Description: Upload inventory/price/image master csv files from FTP location to database by manual click/cron url Input/OutPut: NA */ public function importitemCsv($cronName) { set_time_limit(0); $moduleName = ucfirst(str_replace("_", " ", $cronName)); try { //Initilize customer model $customerModel = Mage::getModel('customerexport/customerexport'); //Initilize logger model $loggerModel = Mage::getModel('logger/logger'); //Initilize logger model $cronModel = Mage::getModel('logger/cron'); //Initilize fulfillment process model $processModel = Mage::getModel('fulfillment/process'); //start time $startTime = $processModel->getCurrentDateTime(); //logger $cronStartMessage = ucfirst(str_replace("_", " ", $cronName)) . ' Cron started.'; $statusStart = "Information"; $filename = ''; $loggerModel->saveLogger($moduleName, $statusStart, $filename, $cronStartMessage); if ($cronName == 'product_inventory') { //Config path setting for Archive, Archive failure and file folder $inboundfolderPath = Mage::getStoreConfig("inventory/directories/custom_csv_directory_inbound"); $folderPath = Mage::getStoreConfig("inventory/directories/custom_csv_directory"); $archiveSuccessPath = Mage::getStoreConfig("inventory/directories/custom_csv_archive_directory"); $archiveFailurePath = Mage::getStoreConfig("inventory/directories/custom_csv_failure_directory"); $cronText = "Products Stock"; } else { if ($cronName == 'price_update') { //Config path setting for Archive, Archive failure and file folder $inboundfolderPath = Mage::getStoreConfig("inventory/directories_priceupdate/custom_csv_directory_inbound"); $folderPath = Mage::getStoreConfig("inventory/directories_priceupdate/custom_csv_directory"); $archiveSuccessPath = Mage::getStoreConfig("inventory/directories_priceupdate/custom_csv_archive_directory"); $archiveFailurePath = Mage::getStoreConfig("inventory/directories_priceupdate/custom_csv_failure_directory"); $cronText = "Products Special price"; } } //check config path if ($inboundfolderPath == '' || $folderPath == '' || $archiveSuccessPath == '' || $archiveFailurePath == '') { throw new Exception("Import, Archive or Archive failure directory should not be empty"); } //Config path setting for email $toEmail = Mage::getStoreConfig("inventory/notificationemail/notification_email"); $ccEmail = Mage::getStoreConfig("inventory/notificationemail/notification_ccemail"); $subject = Mage::getStoreConfig("inventory/notificationemail/notification_email_subject"); if ($toEmail == '' || $ccEmail == '' || $subject == '') { throw new Exception("toemail, ccemail, and subject field can not be empty"); } /* * ****************************************************************** */ /* Check files exist in inbound location, move to out bound location */ /* * ****************************************************************** */ $checkCronUrl = Mage::helper('itemmaster')->checkCronUrl($cronName); if (!$checkCronUrl) { $failureMessage = 'While running ' . $cronText . ' feed import some cron url issue is coming'; Mage::getSingleton('adminhtml/session')->addError(Mage::helper('inventory')->__($failureMessage)); $loggerModel->saveLogger($moduleName, "Error", $filename, $failureMessage); $loggerModel->sendNotificationMail($toEmail, $ccEmail, $subject, $failureMessage); print $failureMessage; $finishTime = $processModel->getCurrentDateTime(); $cronModel->updateCron($cronName, "Finished", '', $finishTime, $failureMessage); return; } else { $baseDir = Mage::getBaseDir(); $varToDir = $baseDir . '/' . $folderPath; $varToArchiveSuccessDir = $baseDir . '/' . $archiveSuccessPath; $varToArchiveFailureDir = $baseDir . '/' . $archiveFailurePath; //create directory if not exist $customerModel->checkDirectory($varToDir); $customerModel->checkDirectory($varToArchiveSuccessDir); $customerModel->checkDirectory($varToArchiveFailureDir); $filesExist = $processModel->readFromRemote($inboundfolderPath, $varToDir, $cronName, $fileformat = "csv"); /* * ****************************************************************** */ /* Check files exist in inbound location, move to out bound location */ /* * ****************************************************************** */ // if(count($filesExist['success'])<1){ // $failureMessage = 'Failed to copy from inbound location '.$inboundfolderPath; // Mage::getSingleton('adminhtml/session')->addError(Mage::helper('inventory')->__($failureMessage)); // $loggerModel-> saveLogger($moduleName, "Exception", $filename, $failureMessage); // $loggerModel-> sendNotificationMail($toEmail,$ccEmail, $subject, $failureMessage); // return; // } /* * *********************************************************** */ /* Check files permission */ /* * *********************************************************** */ $filename = Mage::helper('itemmaster')->checkFilesDirectory($folderPath, $cronName); if ($filename != "" && !file_exists($varToDir . $filename) or !$filename) { $failureMessage = "While running " . $cronText . " feed import, it has been found that no more file exist to be imported"; Mage::getSingleton('adminhtml/session')->addError(Mage::helper('inventory')->__($failureMessage)); $loggerModel->saveLogger($moduleName, "Error", $filename, $failureMessage); if ($cronName == 'price_update') { $loggerModel->sendNotificationMail($toEmail, $ccEmail, $subject, $failureMessage); } print $failureMessage; $finishTime = $processModel->getCurrentDateTime(); $cronModel->updateCron($cronName, "Finished", '', $finishTime, $failureMessage); return; } else { /* * ***************************************** */ /* Mannual cron setting */ /* * ***************************************** */ Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $recordCount = 0; $totalRecordCount = 0; define('IMPORT_FILE_NAME', $varToDir . $filename); $adapter = Mage::getModel('catalog/convert_adapter_product'); $filesRead = fopen(IMPORT_FILE_NAME, 'r'); $headersData = fgetcsv($filesRead); $errors = array(); $errorsSku = array(); $consolidatedMesage = ""; $cronModel->updateCron($cronName, "Processing", $startTime, '', $cronStartMessage); /* Magmi import */ $loggerModel->sendNotificationMail($toEmail, $ccEmail, $subject, 'Inventory update start'); $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); $dp->beginImportSession("default", "update"); while ($data = fgetcsv($filesRead)) { $recordCount++; $product = Mage::helper('itemmaster')->fcmImportData($headersData, $data); try { $dp->ingest($product); //echo "<pre>"; print_r($product); $product = null; //clear memory unset($product); $totalRecordCount++; } catch (Exception $e) { $errorsSku[] = $mergedData['sku']; $errors[] = $e->getMessage(); continue; } } unset($data); $dp->endImportSession(); $this->indexer(); /*old code $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $qry = " SELECT oi.sku FROM `sales_flat_order` AS o LEFT JOIN `sales_flat_order_item` AS oi ON o.entity_id = oi.order_id WHERE o.`sent_to_erp` =0 AND oi.product_type = 'simple' GROUP BY oi.sku "; $res = $read->fetchAll($qry); //get array foreach ($res as $skus) { $ordered_sku[] = $skus['sku']; } while ($data = fgetcsv($filesRead)) { $recordCount++; $mergedData = Mage::helper('itemmaster')->fcmImportData($headersData, $data); try { if (in_array($mergedData['sku'], $ordered_sku)) { $qry = "SELECT oi.sku, sum(oi.qty_ordered) as total FROM `sales_flat_order` AS o LEFT JOIN `sales_flat_order_item` AS oi ON o.entity_id = oi.order_id WHERE o.`sent_to_erp` =0 AND oi.product_type = 'simple' and oi.sku='" . $mergedData['sku'] . "'"; $ordercount = $read->fetchRow($qry); $mergedData['qty'] = ($mergedData['qty'] - $ordercount['total']) < 0 ? 0 : ($mergedData['qty'] - $ordercount['total']); } $adapter->saveRow($mergedData); $totalRecordCount++; } catch (Exception $e) { $errorsSku[] = $mergedData['sku']; $errors[] = $e->getMessage(); continue; } } */ if (count($errors) < 1) { $successMessage = $cronText . " imported successfully.<br>" . $totalRecordCount . " out of Total " . $recordCount . " record imported successfully -> " . $filename; if (rename($varToDir . $filename, $varToArchiveSuccessDir . $filename)) { Mage::getSingleton('core/session')->addSuccess(Mage::helper('inventory')->__('File has been moved to archive location successfully!<br> -> ' . $archiveSuccessPath)); } //Count total number of files remaining to be imported $filesCount = Mage::helper('itemmaster')->getFilesCount($folderPath); $successMessage .= "<br><br>" . "Total Number of files remaining to be imported: <b>" . $filesCount . "</b>"; Mage::getSingleton('core/session')->addSuccess(Mage::helper('inventory')->__("<br>" . $successMessage)); $loggerModel->saveLogger($moduleName, "Success", $filename, $successMessage); $consolidatedMesage = $successMessage; } else { $errorMessage = "<b>Below ERROR found while importing " . $cronText . " (Please note: If same error is getting repeated Multiple times that means that error persist for more than 1 " . $cronText . " SKUs because of which they are getting imported): </b><br>"; $errorMessage .= ">" . implode("<br> >", $errors); $errorMessage .= ' <br><br> <b>Filename -></b> ' . $filename . '<br><br> <b>ALERT:</b> ' . count($errorsSku) . " out of Total " . $recordCount . " " . $cronText . " SKUs are not imported "; foreach ($errorsSku as $skuKey => $skuVal) { if (strlen($skuVal) > 0) { $skuMsgs .= $skuVal . ", "; } } if (strlen($skuMsgs) > 0) { $errorMessage .= '<br>' . $cronText . ' <b>Stock SKUs not imported are -></b> ' . $skuMsgs; } if (rename($varToDir . $filename, $varToArchiveFailureDir . $filename)) { Mage::getSingleton('core/session')->addError(Mage::helper('inventory')->__('File has been moved to error location!<br> -> ' . $archiveFailurePath)); } //Count total number of files remaining to be imported $filesCount = Mage::helper('itemmaster')->getFilesCount($folderPath); $errorMessage .= "<br><br>" . "Total Number of files remaining to be imported: <b>" . $filesCount . "</b>"; Mage::getSingleton('core/session')->addError(Mage::helper('inventory')->__("<br>" . $errorMessage)); $loggerModel->saveLogger($moduleName, "Failure", $filename, $errorMessage); $consolidatedMesage = $errorMessage; return false; } Mage::helper('itemmaster')->refresh_cache_1_4(); //end time $finishTime = $processModel->getCurrentDateTime(); $cronModel->updateCron($cronName, "Finished", '', $finishTime, $consolidatedMesage); $loggerModel->sendNotificationMail($toEmail, $ccEmail, $subject, $consolidatedMesage); } } } catch (Exception $ex) { //$errorMessage = "Error"; Mage::getSingleton('core/session')->addError(Mage::helper('inventory')->__($ex)); $loggerModel->saveLogger($moduleName, "Exception", $fileName, $ex); //end time $finishTime = $processModel->getCurrentDateTime(); $cronModel->updateCron($cronName, "Failed", $startTime, $finishTime, $ex); $loggerModel->sendNotificationMail($toEmail, $ccEmail, $subject, $ex); return false; } return true; }
public function importcategoriesAction() { $csv_mimetypes = array('text/csv', 'text/plain', 'text/html', 'application/csv', 'text/comma-separated-values', 'application/excel', 'application/vnd.ms-excel', 'application/vnd.msexcel', 'text/anytext', 'application/octet-stream', 'application/txt'); if ($this->getRequest()->isPost() && in_array($_FILES['import_file']['type'], $csv_mimetypes)) { if ($_FILES["import_file"]["error"] > 0) { Mage::getSingleton("adminhtml/session")->addError($_FILES["import_file"]["error"]); $this->_redirect('*/*/'); } else { $fileName = str_replace(".csv", "", $_FILES['import_file']['name']) . '_' . time() . '.csv'; $path = Mage::getBaseDir('var') . DS . 'import'; if (!file_exists($path)) { mkdir($path, 0777); } if (move_uploaded_file($_FILES["import_file"]["tmp_name"], $path . DS . $fileName)) { $handle = fopen($path . DS . $fileName, "r"); //convert data into array $this->csv2Array($handle); $import = Mage::getModel('catimportexport/catimportexport'); $dp = Magmi_DataPumpFactory::getDataPumpInstance("productimport"); $dp->beginImportSession("default", "update"); foreach ($this->arrayCollection as $category) { if (array_key_exists('sku', $category) && array_key_exists('category_ids', $category)) { $categoryArray = array(); $categoryArray['sku'] = $category['sku']; if (array_key_exists('position', $category)) { $categoryArray['category_ids'] = $category['category_ids'] . "::" . $category['position']; } else { $categoryArray['category_ids'] = $category['category_ids']; } $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $category['sku']); $cats = $product->getCategoryIds(); foreach ($cats as $category_id) { if ($category['category_ids'] == $category_id) { continue; } $productCategories = $this->getCategory($category_id, $category['sku']); $catArray = $productCategories->getData(); $categoryArray['category_ids'] = $categoryArray['category_ids'] . ',' . $category_id . '::' . $catArray[0]['cat_index_position']; } //echo $categoryArray['category_ids'];die('kk'); //echo $products->getSelect()->__toString();die; if (array_key_exists('store', $category)) { $categoryArray['store'] = $category['store']; } else { $categoryArray['store'] = 'admin'; } if (array_key_exists('websites', $category)) { $categoryArray['websites'] = $category['websites']; } else { $categoryArray['websites'] = 'base'; } //Mage::log($category['sku'].'-----'.$category['category_ids']); //pr($categoryArray);die; //magmi update is defined in coming soon cron model. $import->magmiCategoryImport($categoryArray, $dp); } else { Mage::getSingleton("adminhtml/session")->addError($this->__('unable to find sku or category_ids in csv')); $this->_redirect('*/*/'); } } $dp->endImportSession(); Mage::getSingleton("adminhtml/session")->addSuccess($this->__("%s product(s) successfully assigned to their category.", count($this->arrayCollection))); $deleteCsv = $this->getRequest()->getParam('keep_file'); if ($deleteCsv) { unlink($path . DS . $fileName); Mage::getSingleton("adminhtml/session")->addSuccess($this->__("Csv is successfully deleted")); } $this->_redirect('*/*/'); } else { Mage::getSingleton("adminhtml/session")->addError($this->__('unable to move file in ' . $path)); $this->_redirect('*/*/'); } } } else { Mage::getSingleton("adminhtml/session")->addError($this->__('Invalid csv file')); $this->_redirect('*/*/'); } }