0

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.

Update 1

I changed code. I am getting categories names with url below

 <a href=http://url.net/product-category/harnesses/ rel=tag>Harnesses</a>

Code below

$m_category = wc_get_product_category_list( $product-> get_id() );
    if ( ! empty( $m_category ) ) {
    // use $m_category->parent to check if it's a parent category or sub-category.
    $articles[ $key ]['Category ' . $i] = $m_category->name; //category name from category object
                            }
1
  • 2
    Discussions on third-party plugins are off-topic here. You will get better and precise help in WooCommerce support channels.
    – Abhik
    May 9 at 3:53

1 Answer 1

0

You should change this block of code

if ( ! empty( $m_category ) ) {
   foreach ( $m_category as $id ) {
       $articles[ $key ]['Category '] = $m_category;
   }
}

to be something like this:

if ( ! empty( $m_category ) ) {
   // use $m_category->parent to check if it's a parent category or sub-category.
   $articles[ $key ]['Category '] = $m_category->name; //category name from category object
}
3
  • I am trying right now. Thank you.
    – bekoyzc
    May 9 at 13:22
  • Wordpress says "Undefined variable: m_category and is_search was called incorrectly." I don't understand where I am doing wrong.
    – bekoyzc
    May 9 at 13:46
  • Please check the website error logs, this may help you find the detailed information about the problem
    – Ahmad Wael
    May 10 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.