Adding Custom Field in Export Products Dropdown Woocommerce

When we need to add custom field in dropdown list of Woocommerce product export filter. We can add below Filter Hook.
By defualt when we select ‘Export all columns’ Woocommerce will export all values.

function add_export_data( $product, $name ) {
        $pid = $product->get_id();
        if($product->get_type() == 'variation'){
             $pid = $product->get_parent_id();
        }
        $sale_code  = get_post_meta( $pid, $name, true );
    return $sale_code;
}

function add_export_column( $columns ) {
    $columns['sale_code'] = 'Report Code 1';
    $columns['reportcode2'] = 'Report Code 2';
    return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );

function column_sale_code( $value, $product ) { 
    return add_export_data($product, 'sale_code');
}
function column_reportcode2( $value, $product ) { 
    return add_export_data($product, 'reportcode2');
}

// Filter hook postfix by custom field name: 'woocommerce_product_export_product_column_{$custom_field_name}'.
add_filter( 'woocommerce_product_export_product_column_sale_code', 'column_sale_code', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_reportcode2', 'column_reportcode2', 10, 2 );

woocommerce export product

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *