magmi导入的Cron作业



嗨,我正在尝试制作一个将产品导入我的magento的cron作业。我只是在临时的基础上完成了这一切,但现在我需要一个cron。

理想情况下,我想使用wget,因为它是最直接的。

所以我用wget"http://www.xxxxxx.com/magmi/web/magmi_run.php?profile=default&mode=xcreate&engine=magmi_productimportengine:magmi_productimportengine"-O/dev/null

但是我对这个错误消息有问题HTTP请求已发送,正在等待响应。。。标头中出现读取错误(对等方重置连接)。即将到来——尝试了20次,然后放弃了。

有人能告诉我可能是什么问题吗?谢谢Richard

我建议使用Magmi CLI接口来处理运行的cron作业(因为HTTP连接可能会超时等)。

我会在你的cron作业上运行以下命令:

php /path/to/magmi/cli/magmi.cli.php -mode=create

您可以通过参考此处的文档来定义自定义配置文件和模式:http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Magmi_command_line

我刚刚为Magento创建了一个扩展,允许您通过Magento的cron运行Magmi。这可能会帮助你

在这里免费下载->https://github.com/cameraki/Magento-Magmi-Via-Cron-Extension/

我在这里发布代码是有道理的,以防链接离线,所以它在这里。ps,由于我在github上制作了一个带有扩展名的自述文件,我在底部添加了这些信息,以帮助您入门。

app/code/local/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();
}
}
}

app/code/local/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>

app/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大批量进口商)

这有什么作用

Magmi允许您批量快速地更改产品。它的速度很快,因为它直接导入数据库。因此,请谨慎使用Magmi,并在导入之前备份数据库。

这个扩展允许您使用Magento的cron自动运行Magmi。非常适合每天自动更新产品定价,或每周批量更改一次库存水平。

我可以开箱即用吗

你需要确保你的服务器上有Magmi。然后:

  • 在线10&app/code/local/Oli/Magmi/Cron.php的11更改"/inimporter/…."添加到您的Magmi安装位置。例如,"/magmi/….">
  • 在app/code/local/Oli/Magmi/Cron.php的第23行更改$magmiCsvFil为要从中导入的CVS文件的地址。一旦你已经改变了这一点,改变了线66周围的线来映射列从CSV文件转换为数组。的密钥$newProductArray[$key]['…']应该与您所处的属性相匹配变化。例如,如果你想更新库存水平,你将使用$newProductArray[$key]['qty'],因为'qty'是Magento的库存级别为

自定义

  • 在app/code/local/Oli/Magmi/Cron.php的第36行,设置要重新索引的Magento设置。我补充的是:目录产品属性目录产品价格cataloginventory_stock您可能需要添加更多内容或更改这些内容
  • 在app/code/local/Oli/Magmi/Cron.php的第37行,这是Magmi模式。"create"创建并更新项目,"update"仅更新,"xcreate"仅创建
  • 在app/code/local/Oli/Magmi/Cron.php的第38行,将其更改为匹配您在Magmi中创建的配置文件的名称。您可以使用如果要使用默认配置文件,则为default值得一看,因为你想确保动态索引已关闭,因为此扩展将重新索引您的图像存储在结束。如果使用具有动态索引的配置文件,它将在每个产品之后重新索引,占用大量服务器负载原因

更多信息您可以在此处找到有关Magmi DataDump API的更多信息->http://wiki.magmi.org/index.php/Magmi_Datapump_API

在某些情况下,我必须从Magento cron作业运行Magmi,所以我有这样的东西:

/**
* Includes Magmi files
*/
protected function _includeMagmi()
{
$magentoBaseDir = Mage::getBaseDir();
require_once($magentoBaseDir . DS . 'magmi' . DS . 'inc' . DS . 'magmi_defs.php');
require_once($magentoBaseDir . DS . 'magmi' . DS . 'integration' . DS . 'inc' . DS . 'magmi_datapump.php');
}
/**
* Runs Magmi to update/create for specified data
*
* @param string $profile Magmi import profile (@see magmi/conf/
* @param string $mode
* @param array $data
*/
protected function _runMagmiImport($profile, $mode, array $data)
{
$this->_includeMagmi();
$logger = Mage::getModel('mario/magmi_logger');
/** @var Magmi_ProductImport_DataPump $dataPump */
$dataPump = Magmi_DataPumpFactory::getDataPumpInstance('productimport');
$dataPump->beginImportSession($profile, $mode, $logger);
foreach ($data as $update) {
$dataPump->ingest($update);
}
$dataPump->endImportSession();
return $logger;
}

最新更新