首页 > 解决方案 > 通过 CSV 将自定义分类法导入 WooCommerce

问题描述

我使用名为 Designers (rug_designers) 和 Product Lines (product_line) 的 CPT UI 插件创建了两个自定义分类。我正在使用内置的 WooCommerce 导入工具(不是产品 CSV 导入套件)通过 CSV 将产品导入 WooCommerce。我能够按照本指南使用自动映射在导入中注册自定义列,但是,每当我上传 CSV 文件时,数据都不会保存到自定义分类中。

这是我的 functions.php 文件中的当前代码

 * Register the 'Custom Column' column in the importer.
 *
 * @param array $options
 * @return array $options
 */
function add_column_to_importer( $options ) {

    // column slug => column name
    $options['rug_designers'] = 'Designers';
    $options['product_line'] = 'Product Line';

    return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );

/**
 * Add automatic mapping support for 'Custom Column'. 
 * This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
 *
 * @param array $columns
 * @return array $columns
 */
function add_column_to_mapping_screen( $columns ) {
    
    // potential column name => column slug
    $columns['Designers'] = 'rug_designers';
    $columns['designers'] = 'rug_designers';
    $columns['Product Lines'] = 'product_line';

    return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );

/**
 * Process the data read from the CSV file.
 * This just saves the value in meta data, but you can do anything you want here with the data.
 *
 * @param WC_Product $object - Product being imported or updated.
 * @param array $data - CSV data read for the product.
 * @return WC_Product $object
 */
function process_import( $object, $data ) {
    
// This appears to be part I am having trouble with, I have even tried it with just the 'rug_designers'

    if ( ! empty( $data['rug_designers'] ) || ! empty( $data['product_line'] ) ) {
        $object->update_meta_data( 'rug_designers', $data['rug_designers'] );
        $object->update_meta_data( 'product_line', $data['product_line'] );
    }

    return $object;
}

正如我上面的评论所提到的,我认为问题出在进程导入功能中。我试图在没有“product_line”代码的情况下运行它,但仍然无法让“rug_designers”在导入 csv 时更新数据。

我将非常感谢任何建议。谢谢。

标签: phpwordpresscsvwoocommercecustom-taxonomy

解决方案


自己寻找解决方案。我使用 CPT UI 插件来创建供应商。按照文档添加列没有问题,但它将列映射到自定义分类法是问题所在。希望此评论会引发问题并为我们提供答案。干杯!


推荐阅读