admin管理员组文章数量:1200985
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=/ 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
}
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
}
Share
Improve this question
edited May 9, 2022 at 17:26
bekoyzc
asked May 8, 2022 at 23:22
bekoyzcbekoyzc
11 bronze badge
1
- 2 Discussions on third-party plugins are off-topic here. You will get better and precise help in WooCommerce support channels. – Abhik Commented May 9, 2022 at 3:53
1 Answer
Reset to default 0You 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
}
本文标签: pluginsHow to Get Category Name When Export Products
版权声明:本文标题:plugins - How to Get Category Name When Export Products 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738595713a2101774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论