从Magento导出类别并导入到Neto

时间:2019-01-08 13:20:22

标签: magento-1.9 neto

如何从magento导出csv文件中的类别,该类别可以直接导入到Neto?

1 个答案:

答案 0 :(得分:0)

创建一个PHP文件示例categories.php并将其放置在magento根目录下,然后在该文件中放置以下代码:

        <?php

    error_reporting(E_ALL);
    ini_set("memory_limit", "100000M");
    include("app/Mage.php");
    umask(0);
    Mage::app();

    $data=array();
    // Neto like Csv header, modify it according to you
     $csv_header=Array
            (
                0 => "Content Type",
                1 => "Content Path",
                2 => "Name",
                3 => "Description 1",
                4 => "Description 2",
                5 => "SEO Meta Description",
                6 => "Sort Order",
                7 => "SEO Page Title",
                8 => "SEO Meta Keywords",                   
            );
    $data[0]=   $csv_header;    

     /* magento category fetching start */
    function getChildCategories($category, $isFirst = false) {       
            $children = $isFirst ? $category : $category->getChildren();        
            foreach ($children as $child) {         
                    $_categories[] = [ "name" => $child->getName(), "id" => $child->getId(), "children" => getChildCategories($child) ];
            }
            return $_categories;
    };

    $_categories = Mage::helper('catalog/category')->getStoreCategories();
    $categories = getChildCategories($_categories, true);

    // put desired field in below array
    $csvData=array();
    foreach($categories as $key=>$val){
            $csvData[0]="Product Category";
            $csvData[1]="";
            $csvData[2]=$val['name'];
            $csvData[3]="";
            $csvData[4]="";
            $csvData[5]="";
            $csvData[6]=0;
            $csvData[7]="";
            $csvData[8]="";
            $data[]=$csvData;
    }
    $csvData=array();
    foreach($categories as $key=>$val){
            $csvData[0]="Product Category";
            $csvData[1]=$val['name'];       
            if(isset($val["children"])){
                $childs=$val["children"];
                foreach($childs as $child){
                    $csvData[2]=$child['name']; 
                    $csvData[3]="";
                    $csvData[4]="";
                    $csvData[5]="";
                    $csvData[6]=0;
                    $csvData[7]="";
                    $csvData[8]="";
                    $data[]=$csvData;
                }
            }
    }

    // generate csv
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="productCategories.csv"');

    $fp = fopen('php://output', 'wb');
    foreach ($data as $line) {
        fputcsv($fp, $line, ',');
    }
    fclose($fp);

然后在像yourDomain/categories.php这样的浏览器中调用此文件,您将获得一个csv,可以直接将其导入到Neto存储中。