Good Day, I have the following Code Snippet to help me add custom columns to a plugin the first bit works perfectly but now I would like to add Related Categories to the content (All of this works only in the admin panel) so no shortcode will work
// ADD CUSTOM COLUMN
if( !function_exists('yith_wcbep_add_columns') ) {
function yith_wcbep_add_columns( $columns ) {
$columns['new_column'] = 'Related Categories';
return $columns;
}
add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}
this snipped added content to the columns
// ADD CONTECT TO CUSTOM COLUMN
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'new_column' == $column_name ) {
$value = 'Custom value'; // This shows the value as Custom value
}
return $value;
}
add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}
as soon as I add this code everything disappears from the columns, this code goes where the Custom value is at
$terms = get_the_terms($product->ID, 'product_cat');
foreach ($terms as $term) {
echo $product_cat = $term->name.', ';
}
UPDATE
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'also_column' == $column_name ) {
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$my_query = "SELECT meta_value FROM wp_termmeta WHERE term_id='$woo_cat_id' AND meta_key='crosssell'";
foreach ($my_query as $row) {
//$value = 'Test - '.$woo_cat->term_id.' - '.$woo_cat->name;
$value = 'Test - '.$row->meta_value;
}
}
}
return $value;
}
but the result keeps being blank, what I’m I doing wrong? if I take out the $my_query section it shows the category and ID but I need to get the CROSSSELL CATEGORY
UPDATED 2 This is the Whole Code
// ADD CUSTOM COLUMN
if( !function_exists('yith_wcbep_add_columns') ) {
function yith_wcbep_add_columns( $columns ) {
$columns['also_column'] = 'Also See...';
return $columns;
}
add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}
// ADD CONTECT TO CUSTOM COLUMN
if( !function_exists('yith_wcbep_manage_custom_columns_add_columns_to_new_column') ) {
function yith_wcbep_manage_custom_columns_add_columns_to_new_column( $value, $column_name, $post ) {
if ( 'also_column' == $column_name ) {
$prod_cat_args = array(
'taxonomy' => 'product_cat', //woocommerce
'orderby' => 'name',
'empty' => 0
);
//$woo_categories = get_categories( $prod_cat_args );
$woo_categories = get_the_terms( $post->ID, 'product_cat' );
foreach ( $woo_categories as $woo_cat ) {
$woo_cat_id = $woo_cat->term_id; //category ID
$woo_cat_name = $woo_cat->name; //category name
$my_query = "SELECT meta_value FROM wp_termmeta WHERE term_id='$woo_cat_id' AND meta_key='crosssell'";
foreach ($my_query as $row) {
//$value = 'Test - '.$woo_cat->term_id.' - '.$woo_cat->name;
$value = 'Test - '.$row->meta_value;
}
}
}
return $value;
}
add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}
2
Answers
after very a long time I got this to work
it shows the categories as it should I added strip_tags to remove the links so now only text remains.
But Now I ran into another problem I cannot get crosssell to show up even if it is there
can display $cat_name and $cat_id no problem but the $crosssell does not display at all
You can try with the following.
But sill there might some question, in function you used post as parameter, but you use product in this.