草庐IT

php - Magento 中的 CSV 导入/导出问题

coder 2024-04-27 原文

好的,所以我按照 justcode.in 上的一组说明设法将我的类别导出到 csv 文件中。我想将我的 CSV 导入回新安装的 Magento。

我正在运行 Magento 1.9.2.2 当我尝试运行配置文件时,它会找到我的 CSV 文件并加载它,但会抛出此错误。

错误信息,

<br /> <b>Fatal error</b>: Call to undefined method ImpCat_Catalog_Model_Convert_Adapter_Category::getStoreById() in <b>/var/www/public/secondstore/app/code/local/ImpCat/Catalog/Model/Convert/Adapter/Category.php</b> on line <b>32</b><br />

CSV文件的结构是这样的,

store;categories;is_active;meta_title;meta_keywords;meta_description;include_in_menu;is_anchor;description

然后我在 app\code\local\ImpCat\Catalog\etc\config.xml 中创建了 config.xml。 这个文件看起来像这样,

<?xml version="1.0"?>
<config>
 <global>
  <models>
   <catalog>
    <rewrite>
     <convert_adapter_category>ImpCat_Catalog_Model_Convert_Adapter_Category</convert_adapter_category>
    </rewrite>
   </catalog>
  </models>
 </global>
</config>

然后我继续在 app\code\local\ImpCat\Catalog\Model\Convert\Adapter\Category.php 中创建 Category.php

Category.php 看起来像这样,

<?php
class ImpCat_Catalog_Model_Convert_Adapter_Category extends Mage_Eav_Model_Convert_Adapter_Entity
{
 protected $_categoryCache = array();
 protected $_stores;
 /**
 * Category display modes
 */
 protected $_displayModes = array( 'PRODUCTS', 'PAGE', 'PRODUCTS_AND_PAGE');

 public function parse()
 {
 $batchModel = Mage::getSingleton('dataflow/batch');
 $batchImportModel = $batchModel->getBatchImportModel();
 $importIds = $batchImportModel->getIdCollection();
 foreach ($importIds as $importId){
  $batchImportModel->load($importId);
  $importData = $batchImportModel->getBatchData();
  $this->saveRow($importData);
 }
 }
 /**
 * Save category (import)
 * @param array $importData
 * @throws Mage_Core_Exception
 * @return bool
 */
 public function saveRow(array $importData)
 {
 if (empty($importData['store'])) {
   if (!is_null($this->getBatchParams('store'))) {
   $store = $this->getStoreById($this->getBatchParams('store'));
   } else {
   $message = Mage::helper('catalog')->__('Skip import row, required field "%s" not defined', 'store');
   Mage::throwException($message);
   }
 }else{
  $store = $this->getStoreByCode($importData['store']);
 }
 if ($store === false){
  $message = Mage::helper('catalog')->__('Skip import row, store "%s" field not exists', $importData['store']);
  Mage::throwException($message);
 }
 $rootId = $store->getRootCategoryId();
  if (!$rootId) {
  return array();
  }
 $rootPath = '1/'.$rootId;
  if (empty($this->_categoryCache[$store->getId()])) {
  $collection = Mage::getModel('catalog/category')->getCollection()
              ->setStore($store)
              ->addAttributeToSelect('name');
 $collection->getSelect()->where("path like '".$rootPath."/%'");
 foreach ($collection as $cat) {
  $pathArr = explode('/', $cat->getPath());
  $namePath = '';
  for ($i=2, $l=sizeof($pathArr); $i<$l; $i++) {
  $name = $collection->getItemById($pathArr[$i])->getName();
  $namePath .= (empty($namePath) ? '' : '/').trim($name);
  }
  $cat->setNamePath($namePath);
 }
  $cache = array();
  foreach ($collection as $cat) {
  $cache[strtolower($cat->getNamePath())] = $cat;
  $cat->unsNamePath();
  }
  $this->_categoryCache[$store->getId()] = $cache;
  }
  $cache =& $this->_categoryCache[$store->getId()];
  $importData['categories'] = preg_replace('#\s*/\s*#', '/', trim($importData['categories']));
  if (!empty($cache[$importData['categories']])) {
  return true;
  }
  $path = $rootPath;
  $namePath = '';
  $i = 1;
 $categories = explode('/', $importData['categories']);
 /*$IsActive = $importData['IsActive'];*/
 $IsActive = $importData['is_active'];
 $IsAnchor =$importData['is_anchor'];
 $Description =$importData['description'];
 $IncludeInMenu=$importData['include_in_menu'];
 $MetaTitle=$importData['meta_title'];
 $MetaKeywords=$importData['meta_keywords'];
 $MetaDescription=$importData['meta_description'];
 $Image=$importData['image'];
 $URlkey=$importData['url_key'];
  foreach ($categories as $catName) {
  $namePath .= (empty($namePath) ? '' : '/').strtolower($catName);
  if (empty($cache[$namePath])) {
  $dispMode = $this->_displayModes[2];
    $cat = Mage::getModel('catalog/category')
    ->setStoreId($store->getId())
    ->setPath($path)
    ->setName($catName)
    ->setIsActive($IsActive)
    ->setIsAnchor($IsAnchor)
    ->setDisplayMode($dispMode)->save();
   $cat = Mage::getModel('catalog/category')->load($cat->getId());
   $cat->setIncludeInMenu($IncludeInMenu);
   $cat->setDescription($Description);
   $cat->setMetaTitle($MetaTitle).
    $cat->setMetaKeywords($MetaKeywords);
    $cat->setMetaDescription($MetaDescription);
    $cat->save();
   $cat = Mage::getModel('catalog/category')->load($cat->getId());
   $data['meta_keywords']=$MetaKeywords;
   $data['meta_title']=$MetaTitle;
   $data['meta_keywords']=$MetaKeywords;
   $data['meta_description']=$MetaDescription;
   $data['url_key']= $URlkey;
   $cat->addData($data);
   $cat->save();
  $cache[$namePath] = $cat;
  }
  $catId = $cache[$namePath]->getId();
  $path .= '/'.$catId;
  $i++;
  }
  return true;
 }

 /**
 * Retrieve store object by code
 *
 * @param string $store
 * @return Mage_Core_Model_Store
 */
 public function getStoreByCode($store)
 {
  $this->_initStores();
  if (isset($this->_stores[$store])) {
  return $this->_stores[$store];
  }
  return false;
 }

 /**
 * Init stores
 *
 * @param none
 * @return void
 */
 protected function _initStores ()
 {
  if (is_null($this->_stores)) {
  $this->_stores = Mage::app()->getStores(true, true);
  foreach ($this->_stores as $code => $store) {
  $this->_storesIdCode[$store->getId()] = $code;
  }
  }
 }
}

?>

然后我继续在 app\etc\modules 创建 ImpCat_All.xml ImpCat_All.xml 看起来像这样,

<?xml version="1.0"?>
<config>
 <modules>
  <ImpCat_Catalog>
   <codePool>local</codePool>
   <active>true</active>
  </ImpCat_Catalog>
 </modules>
</config>

然后我在 admin > system >Import/Export > Dataflow - Advanced Profile 中创建了一个名为 category import 的数据流高级配置文件,并添加了这个 XML 代码。

<action type="dataflow/convert_adapter_io" method="load">
     <var name="type">file</var>
     <var name="path">var/import</var>
     <var name="filename"><![CDATA[Categories.csv]]></var>
     <var name="format"><![CDATA[csv]]></var>
    </action>
    <action type="dataflow/convert_parser_csv" method="parse">
     <var name="delimiter"><![CDATA[,]]></var>
     <var name="enclose"><![CDATA["]]></var>
     <var name="fieldnames">true</var>
     <var name="store"><![CDATA[0]]></var>
     <var name="number_of_records">1</var>
     <var name="decimal_separator"><![CDATA[.]]></var>
     <var name="adapter">catalog/convert_adapter_category</var>
     <var name="method">parse</var>
 </action>

最佳答案

在尝试了一个又一个脚本之后,我终于想出了如何将我的所有类别导出和导入到新安装的 Magento 并保留所有原始 ID。

从您的旧 Magento 导出

这是我的导出工具 exporter.php(通过浏览器或 CLI 运行),将此文件放在您的 Magento 根目录下。

exporter.php

<?php
require_once 'app/Mage.php';
Mage::app();
$allCategories = Mage::getModel ( 'catalog/category' );
$categoryTree = $allCategories->getTreeModel();
$categoryTree->load();
$categoryIds = $categoryTree->getCollection()->getAllIds();
if ($categoryIds) {
  $outputFile = "var/export/categories-and-ids.csv";
  $write = fopen($outputFile, 'w');
  foreach ( $categoryIds as $categoryId ) {
    $parentId = $allCategories->load($categoryId)->getParentId();
    $data = array($parentId, $allCategories->load($categoryId)->getName(), $categoryId);
    fputcsv($write, $data);
  }
}
fclose($write);
echo "File written at /var/export";
?>

这会在 /var/export/categories-and-ids.csv 中生成一个 CSV 文件 打开此文件并删除包含默认类别的顶行。您需要在安装中手动创建一个新的默认类别,记住它的 ID 并更正您的顶级类别父 ID。您从导出中获得的 CSV 文件的结构如下,

  1. 第一列是 Parent_Id
  2. 第二列是子类别的名称
  3. 第三列是您旧商店的原始类别 ID

我建议使用 OpenOffice Calc 来编辑和保存 CSV 文件。文件必须是没有 BOM 格式的 UTF-8。

导入到您的新商店

要从您创建/编辑的 CSV 文件中导入您的类别,您只需从您的 Magento 根目录运行此导入程序。 请记住编辑第 21 行以反射(reflect)您的 CSV 的名称和位置。

创建类别.php

<?php
/**
* Script created by sonassi.com (http://www.sonassi.com/knowledge-base/quick-script-batch-create-magento-categories/)
*
* Edited by Christer Johansson for Magento 1.9.2.2 in december 2015
*
* File format of the CSV file categories-and-ids.csv :
* parent_category_id,category_name,category_id
* example: 3,subcat,5
* -> This will create a subcategory with 'subcat' as name and 5 as category id. The parent category id is 3 (Can also be Root
* Category with id 0).
*/

define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';

setlocale(LC_ALL, 'en_US.UTF-8');
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$file = fopen('./var/import/categories-and-ids.csv', 'r');
while (($column = fgetcsv($file)) !== FALSE) {
        //$column is an array of the csv elements
        if (!empty($column[0]) && !empty($column[1]) && !empty($column[2])) {
                $data['general']['name'] = $column[1];
                $data['general']['entity_id'] = $column[2];
                $data['general']['meta_title'] = "";
                $data['general']['meta_description'] = "";
                $data['general']['is_active'] = 1;
                $data['general']['url_key'] = "";
                $data['general']['display_mode'] = "PRODUCTS";
                $data['general']['is_anchor'] = 0;

                $data['category']['parent'] = $column[0]; // 2 or 3 is top level depending on Magento version
                $storeId = 0;

                createCategory($data, $storeId);
                sleep(0.5);
                unset($data);
        }
}

function createCategory($data, $storeId) {
        echo "Starting {$data['general']['name']} [{$data['category']['parent']}] ...";

        $category = Mage::getModel('catalog/category');
        $category->setStoreId($storeId);

        if (is_array($data)) {
                $category->addData($data['general']);

                $parentId = $data['category']['parent'];
                $parentCategory = Mage::getModel('catalog/category')->load($parentId);
                $category->setPath($parentCategory->getPath() . "/" . $category->getId());

                /**
                * Check "Use Default Value" checkboxes values
                */
                if ($useDefaults = $data['use_default']) {
                        foreach ($useDefaults as $attributeCode) {
                                $category->setData($attributeCode, null);
                        }
                }

                $category->setAttributeSetId($category->getDefaultAttributeSetId());

                if (isset($data['category_products']) && !$category->getProductsReadonly()) {
                        $products = [];
                        parse_str($data['category_products'], $products);
                        $category->setPostedProducts($products);
                }

                try {
                        $category->save();
                        echo "Import successful - ID: " . $category->getId() . " - " . $category->getPath() . "<br /> ";
                } catch (Exception $e){
                        echo "Failed import <br />";
                }
        }

}

根据您网站的大小和导入的类别数量,这可能需要几秒到几分钟甚至几小时(如果您有数千个类别)。

请耐心等待,脚本将为您提供所有导入类别的列表,并显示是否有任何导入失败。请记住在导入前清理您的类别名称。

导入完成后,清除缓存并通过管理重新索引 Magento。在“管理类别”中查看您的类别,然后开始导入或创建产品。

祝你好运! :)

关于php - Magento 中的 CSV 导入/导出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213371/

有关php - Magento 中的 CSV 导入/导出问题的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  5. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  6. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  7. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  8. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  9. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  10. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

随机推荐