【问题标题】:WooCommerce csv import custom fields - adjust built-in importer to include custom fields?WooCommerce csv 导入自定义字段 - 调整内置导入器以包含自定义字段?
【发布时间】:2018-04-12 23:50:42
【问题描述】:

我在这种情况下,我必须为超过 2000 种产品的自定义字段导入值,我根本找不到任何免费插件或内置功能可以实现这一点。目前,我发现了一系列具有此功能的高级插件,但最便宜的一个要 50 美元,而且目前根本超出预算。

因此,我想问一下这里是否有人知道一种将自定义字段添加到内置 csv 导入器和导出器的方法,Woocommerce 3.1 之后的更高版本附带了这种方法?到目前为止,我已经成功地为产品数据创建了自己的自定义字段,但它们不会自动显示在内置导入器中(很遗憾)。

是否有任何可行的方法来做到这一点,或者就此而言,您是否知道任何其他方法,例如我还没有找到的免费插件或其他任何方法?

请注意,我已经按照this post 尝试了“wp 终极 csv 导入器”,但不幸的是,自定义字段没有出现在这个免费插件中。

【问题讨论】:

    标签: php wordpress csv import woocommerce


    【解决方案1】:

    好吧,我发布这个问题似乎有点太快了,因为我刚刚找到了答案 - 但希望这可以为一些人节省很多时间和金钱(本来可以为我节省很多时间,如果我只在其他地方找到这个答案)。

    我不小心碰到了 Woocommerce 文档部分的“Column Header Reference”(虽然显然我应该从一开始就在那里),在这里我实际上发现这是完全可行的,只需在csv 文件,前缀为 meta:,后跟自定义字段的 id

    此解决方案的要求
    此解决方案的唯一要求是您已安装并激活 Woocommerce 版本 >3.1,并使用插件的内置 csv 导入器。这可以在“工具 --> 导入 --> Woocommerce 产品 (csv)”下的管理面板中找到

    示例
    如果您为每个产品都有一个自定义字段,例如,包含每个产品的品牌并且 ID 为 brand,您可以为每个产品导入值通过将 csv 文件中的 Header Reference 命名为:meta:brand

    作为另一个示例,如果自定义字段的 id 是_product_brand,则可以通过命名标题引用(例如 excel 中 csv 文件中的第一行)来导入此自定义字段的值: meta:_product_brand

    解决方案是在标题的 csv 文件中为自定义字段 id 加上 meta: 前缀。

    注意: 如果在导入的第二步中它没有自动发生,请确保在您希望导入自定义字段的列的“映射到字段”列中选择“导入为元”选项。

    创建新的自定义字段
    如果您只是上传带有元标题引用且字段 ID 不存在的 csv,这将使用 csv 文件中给出的名称创建一个新的自定义字段。

    【讨论】:

    • 谢谢!我也有类似的情况,按照你的指示我解决了!
    • 抱歉,这对我不起作用 - 值没有被保存 - 有什么想法吗?
    • @LiamBailey 我刚刚对其进行了测试,它在我的站点上运行良好。您确定您已正确填写所有内容吗?请注意,如果您的文件使用的不是,(逗号),您必须手动更改列分隔符。
    • 当前 Product CSV Import Schema 用于内置 Woocommerce 导入器。
    【解决方案2】:

    我也有同样的需求,最后为此开发了自己的插件。

    首先你需要在你的插件中使用PHPExcel。之后读取您的 xls 文件并使用 woocommerce 功能导入产品。

    读取您的 xls 文件并将其转换为具有以下结构的 PHP 数组:

    function fileInitializer($file, $needCells){
         $array_data = array();
         require_once 'libraries/PHPExcel/PHPExcel.php';
         $objPHPExcel = new PHPExcel();
         $target_dir = untrailingslashit( dirname( __FILE__ ) )."/upload-file/".$file;
         $objReader= new PHPExcel_Reader_Excel5();
         $objReader->setReadDataOnly(true);
         $objPHPExcel = $objReader->load( $target_dir);
         $rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
         $rowIndex = 0;
         foreach($rowIterator as $row){
             $cellIterator = $row->getCellIterator();
             $cellIterator->setIterateOnlyExistingCells(false); 
             if(1 == $row->getRowIndex ()) continue;
    
            foreach ($cellIterator as $cell) {
                foreach($needCells as $needCell){
                    if($needCell['cell_name'] == $cell->getColumn()){
                        $array_data[$rowIndex][$needCell['array_name']] = fai_convert_string_to_persian($cell->getCalculatedValue());
                    } 
                }
            }
            $rowIndex++;
        }
        return $array_data;
    }
    $file = 'FILEPATH';
    $needCells = array(
         array('cell_name'=>'A', 'array_name'=>'id')
        , array('cell_name'=>'B', 'array_name'=>'full_name')
    );
    $array_data = fileInitializer($file, $needCells);
    

    在上述过程之后,在 $array_data 中添加一段时间并添加如下产品:

    $post = array(
            'post_author' => $user_id,
            'post_content' => '',
            'post_status' => "publish",
            'post_title' => $value['product_name'],
            'post_parent' => '',
            'post_type' => "product",
        );
    
        $post_id = wp_insert_post( $post, $wp_error );
    
        //ADDING EXTERA FEATURES
        update_post_meta( $post_id, 'main_code_text_field', $value['main_code'] 
     );
    

    这些也是 woocommerce 元数据:

        update_post_meta( $post_id, 'total_sales', '0');
        update_post_meta( $post_id, '_downloadable', 'yes');
        update_post_meta( $post_id, '_virtual', 'yes');
        update_post_meta( $post_id, '_regular_price', "" );
        update_post_meta( $post_id, '_sale_price', "");
        update_post_meta( $post_id, '_purchase_note', "" );
        update_post_meta( $post_id, '_featured', "no" );
        update_post_meta( $post_id, '_weight', "" );
        update_post_meta( $post_id, '_length', "" );
        update_post_meta( $post_id, '_width', "" );
        update_post_meta( $post_id, '_height', "" );
        update_post_meta( $post_id, '_sku', "");
        update_post_meta( $post_id, '_product_attributes', array());
        update_post_meta( $post_id, '_sale_price_dates_from', "" );
        update_post_meta( $post_id, '_sale_price_dates_to', "" );
        update_post_meta( $post_id, '_sold_individually', "" );
        update_post_meta( $post_id, '_manage_stock', "no" );
        update_post_meta( $post_id, '_backorders', "no" );
        update_post_meta( $post_id, '_stock', "" );
    

    【讨论】:

    • 如何调整此代码以使用 Dokan CSV 导入扩展?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    相关资源
    最近更新 更多