运行magento产品导入cron [英] Run magento product import cron

查看:102
本文介绍了运行magento产品导入cron的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

我通过使用管理产品导入控制器创建前端控制器来完成此任务.然后编写了一个脚本,使用php curl调用前端URL.

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.

前端布局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开头的文件

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

这是脚本的主要部分.

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.

这篇关于运行magento产品导入cron的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆