Morne
Morne

Reputation: 101

Woccomerce Related and cross-sell categories

Good Day,

I have this code below in the code you will find the following $value = 'Custom value'; and $value = 'Custom value2'; Here I need to add Related Categories to a product and cross-sell to a product

$value = 'Custom value'; needs to bring up the category/s the product is in and $value = 'Custom value2'; needs to bring up the cross-sell categories for that product

// Add Column Names
if( !function_exists('yith_wcbep_add_columns') ) {
 function yith_wcbep_add_columns( $columns ) {

 $columns['new_column'] = 'Related...';
 $columns['new_column2'] = 'Also See...';

 return $columns;
 }

   add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}

// Add Column content to Column Names
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';
 }
if ( 'new_column2' == $column_name ) {

 $value = 'Custom value2';
 }
 return $value;
 }

   add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}

see screenshot enter image description here

Cross-sell can be queried by something like this SELECT meta_value FROM wp_termmeta WHERE term_id='$id' AND meta_key=\'crosssell\'";

but even that only brings up it blank.

Upvotes: 0

Views: 98

Answers (1)

Morne
Morne

Reputation: 101

This is my solution after spending days and hours on this testing and testing.

First $columns['new_column'];

I added this it brings in the categories

if ( 'new_column' == $column_name ) {
 /*Begin*/
    $product = wc_get_product($post);
    $categories = strip_tags(get_the_term_list( $product->get_id(), 'product_cat', '', ',', '' ));
 
 /*End*/
     $value = $categories;
 }

then on $columns['new_column2']; I changes it to the following, first i got the prod_cat_args array the i did a foreach on $woo_categories then i got the result on to $cat_term_id = $woo_cat_id; from there I added it to the get_term_meta and it worked!

if ( 'new_column2' == $column_name ) {
    /*Begin*/
        
         $prod_cat_args = array(
      'taxonomy'     => 'product_cat', //woocommerce
      'orderby'      => 'name',
      'empty'        => 0
    );
    
    $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
    }
        $cat_term_id = $woo_cat_id;
        $crosssell = get_term_meta( $cat_term_id, 'crosssell', true );
    /*End*/
        
     $value = $crosssell;
        }

// Add Column Names
if( !function_exists('yith_wcbep_add_columns') ) {
 function yith_wcbep_add_columns( $columns ) {

 $columns['new_column'] = 'Related...';
 $columns['new_column2'] = 'Also See...';

 return $columns;
 }

   add_filter( 'yith_wcbep_default_columns', 'yith_wcbep_add_columns', 99 );
}

// Add Column content to Column Names
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 ) {
 /*Begin*/
    $product = wc_get_product($post);
    $categories = strip_tags(get_the_term_list( $product->get_id(), 'product_cat', '', ',', '' ));
 
 /*End*/
     $value = $categories;
 }
if ( 'new_column2' == $column_name ) {
/*Begin*/
    
     $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
}
    $cat_term_id = $woo_cat_id;
    $crosssell = get_term_meta( $cat_term_id, 'crosssell', true );
/*End*/
    
 $value = $crosssell;
    }
 return $value;
 }

   add_filter( 'yith_wcbep_manage_custom_columns', 'yith_wcbep_manage_custom_columns_add_columns_to_new_column', 10, 3 );
}

Hopes this helps any body else

Upvotes: 1

Related Questions