plugins – How to Get Category Name When Export Products

Question

I’m writing an export plugin for WooCommerce. I want to print the product categories and subcategories in separate columns while exporting the products in the plugin I developed.

My code is as below.

function wpae_wp_all_export_csv_headers( $headers, $export_id ) {
global $wpdb;
$last_id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'bek_export' . ' ORDER BY id DESC LIMIT 1 ');
    if ( $export_id == $last_id++ ) { 
        $additional_headers = array(
            'Category 1',
            'Category 2',
            'Category 3',
            'Category 4',
            'Sub-Category 1',
            'Sub-Category 2',
            'Sub-Category 3',
            'Sub-Category 4'
        );
        $headers = array_merge( $headers, $additional_headers );
    }
    return $headers; 
}

function wp_all_export_csv_rows( $articles, $options, $export_id ) {
global $wpdb;
$last_id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'bek_export' . ' ORDER BY id DESC LIMIT 1 ');    
    if ( $export_id == $last_id++ ) { // change to your export ID
        foreach( $articles as $key => $article ) {
            if ( array_key_exists( 'ID', $article ) ) {
                $i = 1;
                $product = wc_get_product( $article['ID'] );

                if ( ! empty( $product ) ) {

                    $m_category = get_term_by( 'id',  'product_cat' );

                    if ( ! empty( $m_category ) ) {
                        foreach ( $m_category as $id ) {
                            $articles[ $key ]['Category '] = $m_category;
                        }
                    }
                }
            }    
        }
    }
    return $articles; // Return the array of records to export

But the code returning nothing to csv column.

0
bekoyzc 5 months 2022-05-08T18:22:10-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse