有没有办法在存储到DB之前解析Magmi中的数据

时间:2013-11-30 19:54:47

标签: magento magmi

Magmi中是否有一种方法可以动态解析列中的数据,然后保存在数据库中。 只是一个例子。假设我的Images列中包含一个带有“前缀”的图像列表。这样:

“IMG:myimage.png”。 我想在Magento DB中存储之前删除所有IMG前缀。 如果没有在Magmi源代码中深入挖掘,这可能吗?

那里也有某种源代码?

2 个答案:

答案 0 :(得分:1)

您可以利用自定义插件中的processItemBeforeId()功能来执行此操作。

  1. magmi / plugins / extra / itemprocessors / removedtringfromcolumn / RemoveStringFromColumn.php

  2. 中创建一个新的插件文件
  3. 然后将您的插件代码放在一起:

    <?php
    class Magmi_RemoveStringFromColumn extends Magmi_GeneralImportPlugin
    {
    public function initialize($params)
    {   
    }
    
    public function getPluginInfo()
    {
        return array("name"=>"Remove String From Column",
                     "author"=>"Axel Norvell",
                     "version"=>"0.0.1");
    }
    
    // Runs before product is processed by Magmi.
    public function processItemBeforeId(&$item,$params=null)
    {   
        $this->removeStringFromItemColumn($item, 'IMG:', 'images');
    }
    
    /**
     * Removes a string from a column
     *
     * @param array $item
     * @param string $string
     * @param string $column
     * @return bool
     */
    public function removeStringFromItemColumn(&$item, $string, $column)
    {
        //Make sure the column exists.
        if(isset($item[$column]))
        {
            $this->log("The column $column is not defined for this item", "warn");
            return false;       
        }
    
        $column_value = $item[$column];
        $column_value = str_replace($string,'',$column_value); /*remove string */
        $column_value = trim($column_value); /*remove trailing spaces */
    
        $item[$column] = $column_value; /* Update the item's column value */
    
        return true;            
    }
    
    //Defines where the plugin options will appear in the Magmi UI.
    static public function getCategory()
    {
        return "Input Data Preprocessing";
    }
    
    }
    
  4. 现在进入Magmi配置并启用名为从列中删除字符串

  5. 的插件
  6. 如果需要,请使用正确的字符串和列替换值修改插件函数。

  7. 工作原理:

    当Magmi运行时,它将在插件中调用 processItemBeforeId(),然后Magmi处理它并将其插入数据库。此时,您可以操纵传递给Magmi的数据。

答案 1 :(得分:1)

使用“Value Replacer”插件可以完成这一操作,而无需触摸代码。

  • 要替换的属性:image
  • 图片的新价值:{{ str_replace('IMG:','',{item.image}) }}

就是这样! ,值replacer将在magmi使用它进行导入之前处理csv的任何列的值。 它适用于任何列并接受任何PHP r值,并且可以引用任何列。

请参阅Value replacer plugin documentation