Run magento product import cron

淺唱寂寞╮ 提交于 2019-12-13 08:25:27

问题


I found this solution online and found more similar to this. I need to run a profile same like this. It is a custom product import. What I want to know is how I can select the correct csv file in this script?


回答1:


I got this done by creating a frontend controller using the admin product import controller. Then wrote a script to call the frontend url using php curl.

frontend Layout 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>

fronted phtml file

<?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";
?>

Here is the main part of the script.

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);
    }
}

Comment if you need more details.



来源:https://stackoverflow.com/questions/29610274/run-magento-product-import-cron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!