How to Get Category Name When Export Products
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=tagHarnesses/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
}
Topic woocommerce-offtopic plugin-development export plugins Wordpress
Category Web