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 );
}
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.
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
Related
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
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.
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
I have this code to display term metadata but for some reason, it's not showing up / Data is blank Sorry for not making it clear the first time
UPDATED
if ( 'new_column2' == $column_name ) {
/*Begin*/
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 );
}
/*End*/
$value = $crosssell;
}
return $value;
}
what am i doing wrong
You're assigning variables but not actually outputting anything.
foreach( ( get_term_meta( $post->ID, 'crosssell' ) ) as $category ) {
echo $category->name . '</br>';
echo $category->term_id . '</br>';
echo get_term_meta( $category->term_id, 'crosssell', true );
}
This fixed the problem I had.
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;
}
I am trying to force woocommerce to append product categories instead of overwriting when uploading new categories via csv.
I have tried finding code snippets.searched the codex and tried to use wp-includes/post.php to create function.
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = true ) {
$post_id = (int) $post_id;
if ( ! $post_id ) {
return true;
}
if ( empty( $tags ) ) {
$tags = array();
}
if ( ! is_array( $tags ) ) {
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$tags = str_replace( $comma, ',', $tags );
}
$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
}
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$tags = array_unique( array_map( 'intval', $tags ) );
}
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
}
I expect woocommerce product categories to be appended rather than overwritten.
actual result
fatal error on line 29: Cannot redeclare wp_set_post_terms()
(previously declared in /var/www/html/wp-includes/post.php:4108)
tried this code. no errors but does not append categories
function append_post_categories( $post_ID = array(), $post_categories = array(), $append = true ) {
$post_ID = (int) $post_ID;
$post_type = get_post_type( $post_ID );
$post_status = get_post_status( $post_ID );
// If $post_categories isn't already an array, make it one:
$post_categories = (array) $post_categories;
if ( empty( $post_categories ) ) {
if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
$post_categories = array( get_option( 'default_category' ) );
$append = true;
} else {
$post_categories = array();
}
} elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
return true;
}
return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
}
The post type for WooCommerce products is product but not post and the taxonomy for "product category" is not category but product_cat instead as it is a custom taxonomy for product custom post type…
So if you want to use your function for WooCommerce product category on products, try this:
function append_product_categories( $product_id, $term_ids, $append = true ) {
$product_id = (int) $product_id;
$post_type = get_post_type( $product_id );
$post_status = get_post_status( $product_id );
$term_ids = (array) $term_ids;
if ( empty( $term_ids ) ) {
if ( 'product' == $post_type && 'auto-draft' != $post_status ) {
$term_ids = array( get_option( 'default_product_cat' ) );
$append = true;
} else {
$term_ids = array();
}
} elseif ( 1 == count( $term_ids ) && '' == reset( $term_ids ) ) {
return true;
}
// Check for existing term id in the product | Check if term exist in Woocommerce
foreach( $term_ids as $key => $term_id ) {
if( hast_term( $term_id, 'product_cat', $product_id ) || ! term_exists( $term_id, 'product_cat' ) ) {
unset($term_ids[$key]); // remove term id from the array
}
}
return wp_set_post_terms( $product_id, $term_ids, 'product_cat', $append );
}
It should better works for WooCommerce Product categories (untested)
I currently have a function which successfully adds products to Woocommerce using wp_insert_post().
I'm now trying to assign products to relevant categories based on their product tags. For example, if the product is added with the product tags 'ring' or 'necklace' then it is auto assigned to the 'Jewellery' category.
Using the following function, I'm able to achieve the correct functionality on posts, but have had no luck in attempting to make this work for the products post type used in woocommerce.
Works for posts:
function auto_add_category ($post_id = 0) {
if (!$post_id) return;
$tag_categories = array (
'ring' => 'Jewellery',
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
$post_tags = get_the_tags($post_id);
foreach ($post_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat_id = get_cat_ID($tag_categories[$tag->name]);
if ($cat_id) {
$result = wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
}
}
}
}
add_action('publish_post','auto_add_category');
I've tried to repurpose the code to work for products as follows:
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
$tag_categories = array (
'ring' => 'Jewellery'
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
$product_tags = get_terms( array( 'taxonomy' => 'product_tag') );
foreach ($product_tags as $tag) {
if ($tag_categories[$tag->name] ) {
$cat = get_term_by( 'name', $tag_categories[$tag->name], 'product_cat' );
$cat_id = $cat->term_id;
if ($cat_id) {
$result = wp_set_post_terms( $product_id, $tags = $cat_id, $taxonomy = 'product_cat', $append = true );
}
}
}
}
add_action('publish_product','auto_add_category');
However, it doesn't assign the relevant categories upon product creation. Any assistance would be greatly appreciated by this novice coder muddling his way through wordpress!
Try this code:
function auto_add_category ($product_id = 0) {
if (!$product_id) return;
// because we use save_post action, let's check post type here
$post_type = get_post_type($post_id);
if ( "product" != $post_type ) return;
$tag_categories = array (
'ring' => 'Jewellery'
'necklace' => 'Jewellery',
'dress' => 'Clothing',
);
// get_terms returns ALL terms, so we have to add object_ids param to get terms to a specific product
$product_tags = get_terms( array( 'taxonomy' => 'product_tag', 'object_ids' => $product_id ) );
foreach ($product_tags as $term) {
if ($tag_categories[$term->slug] ) {
$cat = get_term_by( 'name', $tag_categories[$term->slug], 'product_cat' );
$cat_id = $cat->term_id;
if ($cat_id) {
$result = wp_set_post_terms( $product_id, $cat_id, 'product_cat', true );
}
}
}
}
add_action('save_post','auto_add_category');
I create plugin and want to get brand name from woocommerce.
In fist time i post product no.1 the plugin it's work and get product brand as i wish but when i create product post no.2 and no.3 from brand name get from product no.1
whats wrong ?
if ( class_exists( 'WooCommerce' ) ) {
$kode = $product->get_sku();
$terms = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name'));
$brands = get_terms( 'brand', array(
'orderby' => 'name'
//,'product__in' => $product->id
// orderby arguments ('name', 'slug','term_group', 'term_id', 'id', 'description')
) );
foreach ( $brands as $key => $brand ) :
$brand_name = $brand->name;
endforeach;
$merk = $brand_name;
//this i want to print the brand name
echo "<strong>STOCK ".$merk." ".$kode."</strong><br/>";
} else {
echo "please active your WooCommerce plugin.";
}
this should work for you.
if ( class_exists( 'WooCommerce' ) ) {
$kode = $product->get_sku();
$tax = array(
'STOCK'=>'brand',
);
foreach ($tax as $mk_label => $mk_value) {
$make_array = wp_get_post_terms(get_the_ID(), $mk_value, array("fields" => "names"));
foreach ($make_array as $value) {
$mk .= $value.", ";
}
echo '<strong>'.$mk_label.' '.rtrim($mk,", ").' '.$kode.'</strong>';
$mk ="";
}
} else {
echo "please active your WooCommerce plugin.";
}
We are taking terms dynamically based on current product id and fetching the brand accordingly. Let me know if you need any more help.
Thanks
Updated code:
foreach ( $brands as $key => $brand ) :
echo "<strong>STOCK ".$brand->name." ".$kode."</strong><br/>";
endforeach;
The echo must be inside the foreach loop.
If you want get terms from product, you should try this:
$terms = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name'));
//you could use: var_dump($terms); to see what it is.
If you want to concat your brand names, you could do this:
$term_names = wp_get_post_terms( $product_id, 'brand', array('orderby'=>'name', 'fields' => 'names'));
$term_names = implode(' ', $term_names);
echo $term_names;
Instead of taxonomy "brand" it should be "pa_brand".
$term_names = wp_get_post_terms( $product_id, 'pa_brand', array('orderby'=>'name', 'fields' => 'names'));