Woocommerce 如何显示相关类别

时间:2021-07-12 12:02:18

标签: php wordpress woocommerce

美好的一天,我有以下代码片段来帮助我将自定义列添加到插件中,第一个可以完美运行,但现在我想将相关类别添加到内容中(所有这些仅在管理面板中有效)所以没有简码会起作用

// 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 );
}

此剪辑添加到列的内容

// 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 );
}

一旦我添加此代码,列中的所有内容都会消失,此代码将转到自定义值所在的位置

$terms = get_the_terms($product->ID, 'product_cat');
      foreach ($terms as $term) {

        echo $product_cat = $term->name.', ';
  }

更新

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;
 }

但结果一直空白,我做错了什么?如果我取出 $my_query 部分,它会显示类别和 ID,但我需要获取 CROSSSELL CATEGORY

更新 2 这是完整的代码

// 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 个答案:

答案 0 :(得分:0)

您可以尝试以下方法。

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

    $categories .= $product_cat = $term->name.', ';
}
return $categories;

但是还是有一些问题,在函数中你使用了 post 作为参数,但你在这个中使用了 product。

答案 1 :(得分:0)

很长一段时间后,我得到了这个工作

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

它显示了类别,因为我应该添加 strip_tags 来删除链接,所以现在只剩下文本了。

但是现在我遇到了另一个问题,即使存在交叉销售也无法显示

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 );
}

可以显示 $cat_name 和 $cat_id 没问题,但是 $crosssell 根本不显示

相关问题