谁能告诉我如何自动化magmi每天按计划时间执行导入。
听说可以通过cli来完成,但不知道如何使用cli。
请给我一个关于如何 cli 以及使用哪些命令来自动导入的逐步过程。
我看到magmi wiki网站不太了解如何使用cli。
请给我一个关于如何自动化 magmi 的正确解决方案。
我什至尝试使用下面的链接,但它现在可以工作了
wget "http://user:[email protected]/magmi/web/magmi_run.php?mode=create&profile=default&engine=magmi_productimportengine:Magmi_ProductImportEngine&CSV:filename=/magmitest.csv" -O /dev/null
如果您可以使用系统 cron(针对您的问题的 cli 版本),那么这是我在我的一个项目(简化版本)中使用的完整解决方案。
我将使用 Company 作为供应商名称,模块名称将是 Magmi。
第一步是像往常一样安装magmi。我认为您已经安装了它。
接下来,创建包含以下内容的 app/etc/modules/Company_Magmi.xml
<?xml version="1.0"?>
<config>
<modules>
<Company_Magmi>
<active>true</active>
<codePool>local</codePool>
<version>0.0.1</version>
</Company_Magmi>
</modules>
</config>
然后使用以下内容创建 app/code/local/Company/Magmi/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Company_Magmi>
<version>0.0.1</version>
</Company_Magmi>
</modules>
<global>
<models>
<company_magmi>
<class>Company_Magmi</class>
</company_magmi>
</models>
</global>
<crontab>
<jobs>
<magmi_update>
<schedule>
<cron_expr>*/5 * * * *</cron_expr>
</schedule>
<run>
<model>company_magmi/cron::magmiUpdate</model>
</run>
</magmi_update>
</jobs>
</crontab>
</config>
使用以下内容创建 app/code/local/Company/Magmi/Cron.php 文件
<?php
require_once(dirname(__FILE__) . "/../../../../../magmi/plugins/inc/magmi_datasource.php");
require_once(dirname(__FILE__) . "/../../../../../magmi/integration/productimport_datapump.php");
class Company_Magmi_Cron {
public function magmiUpdate()
{
$items = array(); // build your own list of items to create/update
$this->import($items);
}
private function import($items, $mode = 'create', $indexes = 'all')
{
if (count($items) > 0) {
$dp = new Magmi_ProductImport_DataPump();
$dp->beginImportSession("PROFILE_NAME", $mode);
foreach ($items as $item) {
$dp->ingest($item);
}
$dp->endImportSession();
$this->reindex($indexes);
}
}
private function reindex($string = 'all')
{
/** @var $indexer Mage_Index_Model_Indexer */
$indexer = Mage::getModel('index/indexer');
$processes = array();
if ($string == 'all') {
$processes = $indexer->getProcessesCollection();
} else {
$codes = explode(',', $string);
foreach ($codes as $code) {
$process = $indexer->getProcessByCode(trim($code));
if ($process) {
$processes[] = $process;
}
}
}
/** @var $process Mage_Index_Model_Process */
foreach ($processes as $process) {
$process->reindexEverything();
}
}
}
最后将 PROFILE_NAME 更改为您在 magmi 中的个人资料名称。
完成所有这些后,您将必须构建要创建/更新的项目列表。这真的很简单。这是一个例子:
假设您想要更新产品的库存。您可以像这样创建 CSV 文件:
sku,qty
"SOMESKU","10"
"SNOTHERSKU","2"
只需构建这样的$items:
$items[] = array(
"sku" => "SOMESKU",
"qty" => "10"
);
$items[] = array(
"sku" => "ANOTHERSKU",
"qty" => "2"
);
不要忘记为 Magento 设置 cron!
你已经有想法了,对吧?
就是这样。
我们还可以通过curl命令实现自动化,这很简单
$url="http://learning.iksuladev.com/satyendra/magmi/web/magmi_run.php?mode=create&profile=satyendra&engine=magmi_productimportengine:Magmi_ProductImportEngine&CSV:filename=../../var/import/sample.csv -O /dev/null";
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
添加上述答案:如果以上
required
文件不起作用,请使用以下文件。
require_once(dirname(__FILE__) . "/../../../../../magmi/inc/magmi_defs.php");
require_once(dirname(__FILE__) . "/../../../../../magmi/integration/inc/productimport_datapump.php");
这些文件对我有用。
使用 @sickelap 的代码作为基础和 @Ranjith 关于所需文件的注释,我刚刚为 Magento 创建了一个扩展,允许您通过 Magento 的 cron 运行 Magmi。希望这有帮助
在这里免费下载 -> https://github.com/cameraki/Magento-Magmi-Via-Cron-Extension/
正如评论中提到的,我在这里发布代码是有道理的,以防链接离线,所以就在这里。 ps,因为我在 github 上制作了带有扩展的自述文件,所以我在底部添加了此信息以帮助您入门。再次感谢 @sickelap 和 @Ranjith,因为这是基于他们的答案
应用程序/代码/本地/Oli/Magmi/Cron.php
<?php
/**
* Created by PhpStorm.
* User: Oli
* Date: 02/11/2017
* Time: 17:05
*/
/* Change importer to the name of your Magmi directory */
require_once(Mage::getBaseDir() . "/importer/plugins/inc/magmi_datasource.php");
require_once(Mage::getBaseDir() . "/importer/integration/inc/productimport_datapump.php");
class Oli_Magmi_Cron {
const LOG_DIRECTORY = "oli-extensions.log";
public function runMagmi()
{
try {
$directory = Mage::getBaseDir('var') . '/import/';
$magmiCsvFile = $directory . 'magmiUploadReady.csv';
if(!file_exists($magmiCsvFile)) {
throw new Exception('CSV file for Magmi to upload cant be found.');
}
} catch (Exception $e) {
$message = date('F jS (l) Y h:i:s A') . " === MAGMI (" . __METHOD__ . ") === Could not load todays magmiupload file. It must not have run today. This script will now stop: ".$e->getMessage();
Mage::log($message, null, self::LOG_DIRECTORY);
die();
}
$productArray = $this->csvToArray($magmiCsvFile);
$indexes = 'catalog_product_attribute,catalog_product_price,cataloginventory_stock';
$mode = 'update';
$profile = 'cronUpdatePrices';
try {
$this->import($productArray, $mode, $profile);
} catch (Exception $e) {
$message = date('F jS (l) Y h:i:s A') . " === MAGMI (" . __METHOD__ . ") === There was an issue importing the products: ".$e->getMessage();
Mage::log($message, null, self::LOG_DIRECTORY);
}
try {
$this->reindex($indexes);
} catch (Exception $e) {
$message = date('F jS (l) Y h:i:s A') . " === MAGMI (" . __METHOD__ . ") === There is a problem with the reindex function: ".$e->getMessage();
Mage::log($message, null, self::LOG_DIRECTORY);
}
/* log that it actually ran */
$message = date('F jS (l) Y h:i:s A') . " === MAGMI (" . __METHOD__ . ") === Has finished running";
Mage::log($message, null, self::LOG_DIRECTORY);
}
public function csvToArray($filename)
{
$productArray = array_map('str_getcsv', file($filename));
array_shift($productArray);
$newProductArray = array();
/* Customise the item array index's to match the file you are importing */
foreach ($productArray as $key => $item) {
$newProductArray[$key]['sku'] = $item[0];
$newProductArray[$key]['buy_price'] = $item[1];
$newProductArray[$key]['price'] = $item[2];
$newProductArray[$key]['store'] = $item[3];
$newProductArray[$key]['price_updated'] = $item[4];
$newProductArray[$key]['amazon_euros'] = $item[5];
$newProductArray[$key]['amazon_dollars'] = $item[6];
$newProductArray[$key]['qty'] = $item[7];
$newProductArray[$key]['is_in_stock'] = $item[8];
}
return $newProductArray;
}
private function import($items, $mode, $profile)
{
if (count($items) > 0) {
try{
$dp = new Magmi_ProductImport_DataPump();
$dp->beginImportSession($profile, $mode);
foreach ($items as $item) {
$dp->ingest($item);
}
$dp->endImportSession();
} catch (Exception $e) {
$message = date('F jS (l) Y h:i:s A') . " === MAGMI (" . __METHOD__ . ") === Seems to be an issue with the import function: ".$e->getMessage();
Mage::log($message, null, self::LOG_DIRECTORY);
}
}
}
private function reindex($string)
{
/** @var $indexer Mage_Index_Model_Indexer */
$indexer = Mage::getModel('index/indexer');
$processes = array();
if ($string == 'all') {
$processes = $indexer->getProcessesCollection();
} else {
$codes = explode(',', $string);
foreach ($codes as $code) {
$process = $indexer->getProcessByCode(trim($code));
if ($process) {
$processes[] = $process;
}
}
}
/** @var $process Mage_Index_Model_Process */
foreach ($processes as $process) {
$process->reindexEverything();
}
}
}
应用程序/代码/本地/Oli/Magmi/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Oli_Magmi>
<version>0.0.1</version>
</Oli_Magmi>
</modules>
<global>
<models>
<oli_magmi>
<class>Oli_Magmi</class>
</oli_magmi>
</models>
</global>
<crontab>
<jobs>
<update_prices_via_magmi>
<schedule>
<cron_expr>30 3 * * 2</cron_expr>
</schedule>
<run>
<model>oli_magmi/cron::runMagmi</model>
</run>
</update_prices_via_magmi>
</jobs>
</crontab>
</config>
应用程序/etc/modules/Oli_Magmi.xml
<?xml version="1.0"?>
<config>
<modules>
<Oli_Magmi>
<active>true</active>
<codePool>local</codePool>
</Oli_Magmi>
</modules>
</config>
Magento Magmi 通过 Cron 扩展
Magento 扩展通过 Magento 的 cron 从 CSV 文件运行 Magmi
注意:这仅在您有 Magmi(Magento Mass Importer)时才有效
这个有什么作用?
Magmi 允许您快速批量更改产品。它的速度很快,因为它直接导入到数据库中。因此请谨慎使用 Magmi,并在导入之前备份数据库。
此扩展允许您使用 Magento 的 cron 自动运行 Magmi。非常适合每天自动更新产品定价,或每周一次批量更改库存水平。
我可以直接使用它吗?
您需要确保您的服务器上有 Magmi。然后:
定制
更多信息 您可以在此处找到有关 Magmi DataDump API 的更多信息 -> http://wiki.magmi.org/index.php/Magmi_Datapump_API