skip to Main Content

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


  1. Chosen as BEST ANSWER

    after very a long time I got this to work

    **$product = wc_get_product($post);
         $categories = strip_tags(get_the_term_list( $product->get_id(), 'product_cat', '', ',', '' ));
     
     /*End*/
         $value = $categories;**
    

    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

    foreach((get_term_meta( $post->ID, 'crosssell' )) as $category) {
    $cat_name = $category->name . '</br>';
    $cat_id = $category->term_id . '</br>';
    $crosssell = get_term_meta( $cat_id, 'crosssell', true );
    }
    

    can display $cat_name and $cat_id no problem but the $crosssell does not display at all


  2. You can try with the following.

    $terms = get_the_terms($product->ID, 'product_cat');
    $categories = '';
      foreach ($terms as $term) {
    
        $categories .= $product_cat = $term->name.', ';
    }
    return $categories;
    

    But sill there might some question, in function you used post as parameter, but you use product in this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search