运行magento产品导入cron

时间:2015-04-13 16:22:45

标签: magento magento-1.8

我在网上找到了this解决方案,发现它与此类似。我需要像这样运行一个配置文件。这是一个定制产品导入。我想知道的是如何在此脚本中选择正确的csv文件?

1 个答案:

答案 0 :(得分:-1)

我通过使用admin产品导入控制器创建前端控制器来完成此操作。然后编写了一个脚本,使用php curl调用前端url。

前端布局xml

<layout version="0.1.0">

    <importproduct_cronimport_run>
       <remove name="footer" />
       <remove name="top.search" />
       <remove name="cart_sidebar" />
       <remove name="header" />
       <remove name="top.menu" />
       <remove name="root" />
       <reference name="content">
            <block type="importproduct/system_convert_run" name="system_convert_run" template="importproduct/cronimport.phtml" output="toHtml" />
       </reference>
    </importproduct_cronimport_run>

</layout>

前面的phtml文件

<?php
    $this->getProfile()->run();

    $array = array("batchId" => $this->getBatchModel()->getId(), "count" => $this->getBatchItemsCount());
    $rows = [];

    $importData = $this->getImportData();
    $arrCounter = 0;
    foreach ($importData as  $importValue){
        $rows[] = $importValue["rows[]"][0];
        $arrCounter++;
    }

    $array["rows"] = $rows;
    echo "<br>jsonDataStart{".$this->jsonEncode($array)."}jsonDataEnd";
?>

这是剧本的主要部分。

function importcsv($baseurl,$csv,$logFile){
    Mage::log('Start importproduct/cronImport/run',null,'prod_import.log');
    $url = $baseurl."importproduct/cronImport/run/id/3/files/".$csv;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    $error  = curl_error($curl)."\r\n";
    curl_close ($curl);

    Mage::log('Print errors if any',null,'prod_import.log');
    file_put_contents($logFile, $error, FILE_APPEND | LOCK_EX);
    Mage::log('End importproduct/cronImport/run',null,'prod_import.log');

    /**** Extract Batch ID and Row IDs ***********/
    $jsonDataStart = stripos($result,"jsonDataStart{") + 14;
    $jsonDataEnd   = stripos($result,"}jsonDataEnd");
    $jsonLength    = $jsonDataEnd - $jsonDataStart;

    $jsonArray = substr($result, $jsonDataStart, $jsonLength);

    $simpleArray = json_decode($jsonArray, true);

    $batchId      = $simpleArray["batchId"];
    $totalRecords = $simpleArray["count"];
    $rowIdList    = $simpleArray["rows"];

    for ($x = 0; $x < $totalRecords; $x++) {
        /*** call batchRun ***/
        Mage::log('Start importproduct/cronImport/batchRun/id/'.$batchId."/row/".$rowIdList[$x],null,'prod_import.log');

        $url = $baseurl."importproduct/cronImport/batchRun/";
        $post = "batch_id=".$batchId."&rows%5B%5D=".$rowIdList[$x];

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $result = curl_exec ($curl);
        $error  = curl_error($curl)."\r\n";
        curl_close ($curl);

        Mage::log('Print errors if any',null,'prod_import.log');
        file_put_contents($logFile, $error, FILE_APPEND | LOCK_EX);
        Mage::log('End importproduct/cronImport/batchRun/id/'.$batchId."/row/".$rowIdList[$x],null,'prod_import.log');
    } 

    /*** call batchFinish ***/
    Mage::log('Start importproduct/cronImport/batchFinish/id/'.$batchId,null,'prod_import.log');

    $url = $baseurl."importproduct/cronImport/batchFinish/id/".$batchId."/";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($curl);
    $error  = curl_error($curl)."\r\n";
    curl_close ($curl);

    Mage::log('Print errors if any',null,'prod_import.log');
    file_put_contents($logFile, $error, FILE_APPEND | LOCK_EX);
    Mage::log('End importproduct/cronImport/batchFinish/id/'.$batchId,null,'prod_import.log');

    /**** Move imported file to directory Imported ****/
    if (file_exists("var/import/".$csv)) {
        $t=time();
        $timestamp = date("Y-m-d-H-i-s",$t);
        rename("var/import/".$csv, "var/import/imported/".$timestamp." - ".$csv);
    }
}

评论是否需要更多详细信息。