Woocommerce Display Termmeta - php

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

Related

Woocommerce How to Show Related Categories

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

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

How to programmatically update/create wordpress menu from agiven list of categories?

I am writing a small wordpress plugin that would read a list of WooCommerce product categories and then create a menu or update an existing menu as per the category names and links. The update is triggered by clicking a button in a POSTed form. So far the plugin creates the menu with all categories and sub categories as parent items in the menu, I want it to put sub categories into sub menus but it fails.
Here is the code that helps me achieve this:
public function update_menu(){
$children = array();
$mains = array();
$no_menu = false;
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'name'
);
$all_categories = get_categories( $args );
$menu_name = get_option('woo_cats_menu_menu_name');
$locations = get_nav_menu_locations();
$menus = get_registered_nav_menus();
foreach ( $menus as $name => $description ){
if ( $menu_name == $name )
$menu_description = $description;
}
if ( !empty ( $all_categories ) ){
//public variables defined in the class
$this->added = 0;
$this->notadded = 0;
$this->notinmenu = 0;
$this->inmenu = 0;
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id); //convert to array
$menu_items = $this->toArray( $menu_items );
echo "<br><br>Menus printouts.. Converted to array<br />";
print_r( $menu_items );
foreach ($all_categories as $cat) {
if (!empty( $locations ) && isset($locations[$menu_name]) ) {
//if the category is not in the menu, then we add it//
//if ($cat->name != $title && get_term_link($cat->slug, 'product_cat') != $url && )
if ( false == $this->in_multiarray($cat->name, $menu_items) ){//!$this->in_array_md( $cat->name, $menu_items)
if ( $cat->parent == 0 || $cat->parent == "" || $cat->category_parent == 0 ){ //if is a main category
$mains[] = array(
'cat_id' => $cat->term_id,
'name' => $cat->name,
'cat_name' => $cat->cat_name,
'slug' => $cat->slug,
'category_parent' => $cat->category_parent,
'link' => get_term_link($cat->slug, 'product_cat')
);
}else{ // if child_of is > 0
$children[] = array(
'cat_id' => $cat->term_id,
'name' => $cat->name,
'cat_name' => $cat->cat_name,
'slug' => $cat->slug,
'category_parent' => $cat->category_parent,
'child_of' => $cat->category_parent,
'link' => get_term_link($cat->slug, 'product_cat')
);
}
$this->notinmenu++;
}else{
$this->inmenu++;
}
}else{
$no_menu = true;
}
}
}
/*echo "<br />Second printout<br />";
print_r( $menu_items );*/
if ( $no_menu == false && !empty( $mains ) ){
echo "<br /><strong>Adding main categories</strong><br />";
/*print_r( $mains );
echo "<br />End: Mains<br />";*/
if ( count( $mains ) > 1 ){
//if we have multiple categories
foreach ( $mains as $main ){
//print_r ( $main );
if ( !$this->in_multiarray($main['name'], $menu_items) ){ //if is a main category
echo "Category not in menu...., ";
echo "Adding to menu::> '".$main['name']."' = '".$main['link'] . "'<br />";/**/
$menu_id = wp_update_nav_menu_item($menu->term_id, 0, array(
'menu-item-title' => esc_attr( $main['name'] ),
'menu-item-url' => esc_attr( $main['link'] ),
'menu-item-status' => 'publish')
);
if ( !is_wp_error($menu_id) ) $this->added++;
else $this->notadded++;
}else{
$this->notadded++;
}
}
}elseif ( count ( $mains ) == 0 ){
//if we have one category, call it mains
$main = $mains;
if ( !$this->in_multiarray($main['name'], $menu_items) ){ //if is a sub category
echo "Category not in menu...., ";
echo "Adding to menu::> '".$main['name']."' = '".$main['link'] . "'<br />";/**/
$menu_id = wp_update_nav_menu_item($menu->term_id, 0, array(
'menu-item-title' => esc_attr( $main['name'] ),
'menu-item-url' => esc_attr( $main['link'] ),
'menu-item-status' => 'publish')
);
if ( !is_wp_error($menu_id) ) $this->added++;
else $this->notadded++;
}else{
$this->notadded++;
}
}else{
$this->notadded++;
}
}
if ( $no_menu == false && !empty( $children ) ){
echo "<br /><strong>Adding child menu items.</strong><br />";
if ( count ( $children ) > 1 ) {
foreach ( $children as $child ){
if ( !$this->in_multiarray($child['name'], $menu_items) ){//!$this->in_array_md( $child['name'], $menu_items, false)
echo "-- Adding Sub Category:: ID: " . $child['cat_id'].", parent: ". $child['category_parent'].", Name: ".$child['name'].", -> \"".$child['link']."\"<br />";/**/
$parent_id = esc_attr( $this->get_parent_id( $mains, $children, esc_attr( $child['cat_id'] ) ) );
$parent_name = $this->get_parent_name( $mains, $parent_id );
$menu_item = $this->get_menu_item( $parent_name, $child['link'] );
//print_r ( $menu_item );
echo "Real Parent: $parent_name, {$menu_item->ID}<br />";
$menu_id = wp_update_nav_menu_item($menu->term_id, 0, array(
'menu-item-title' => esc_attr( $child['name'] ),
'menu-item-parent-id' => $menu_item->ID,
'menu-item-url' => esc_attr( $child['link'] ),
'menu-item-status' => 'publish')
);
//echo $sub_category->name ;
//update post_parent
if ( !is_wp_error($menu_id) ) {
wp_update_post(array('ID'=>$menu_id,'post_parent'=>$menu_item->ID));
update_post_meta( $menu_id, '_menu_item_menu_item_parent', $menu_item->ID );
$this->added++;
}
else $this->notadded++;
}else{
$this->notadded++;
}
}
}elseif ( count ( $children ) == 1 ) {
if ( false == $this->in_multiarray($child['name'], $menu_items) ){//!$this->in_array_md( $child['name'], $menu_items, false)
echo "-- Adding Sub Category:: ".$child['name'].", -> \"".$child['link']."\"<br />";/**/
//$parent_id = esc_attr( $this->get_parent_id( $mains, $children, esc_attr( $child['cat_id'] ) ) );
$parent_id = esc_attr( $this->get_parent_id( $mains, $children, esc_attr( $child['cat_id'] ) ) );
$parent_name = $this->get_parent_name( $mains, $parent_id );
$menu_item = $this->get_menu_item( $parent_name, $child['link'] );
echo "Real Parent: $parent_name, {$menu_item->ID}<br />";
$menu_id = wp_update_nav_menu_item($menu->term_id, 0, array(
'menu-item-title' => esc_attr( $child['name'] ),
'menu-item-parent-id' => $menu_item->ID,
'menu-item-url' => esc_attr( $child['link'] ),
'menu-item-status' => 'publish')
);
//echo $sub_category->name ;
//update post_parent
if ( !is_wp_error($menu_id) ) {
wp_update_post(array('ID'=>$menu_id,'post_parent'=>$menu_item->ID));
update_post_meta( $menu_id, '_menu_item_menu_item_parent', $menu_item->ID );
$this->added++;
}
else $this->notadded++;
}else{
$this->notadded++;
}
}else{
$this->notadded++;
}
}
/*echo "<br>Mains<br>";
print_r( $mains );
echo "<br>Children<br>";
print_r( $children );*/
//if we have updated a menu
if ( $no_menu == true ){
echo '<div class="alert alert-warning settings-error notice update-nag"><p><strong>Sorry but nothing to do!</strong> Please make sure you created an empty menu and assign it to the menu <em><strong>"'.$menu_description.'"</strong></em> selected on the right. Go to <em><strong>"Appearance > Menus"</strong></em>, select the menu you selected on this page and check the checkbox next to <em><strong>"'.$menu_description.'"</strong></em>.</p></div>';
}else{
echo '<div class="updated alert alert-success"><p><strong>The menu has been updated!</strong> ' . $this->notinmenu . ' Items were added to the menu, '. $this->inmenu .' Items were already in the menu.</p></div>';
}
}
public function get_parent_id( $parents, $children, $kid ){
foreach ( $parents as $parent ){
foreach ( $children as $child ){
if ( $parent['cat_id'] == $child['child_of'] && $child['cat_id'] == $kid ){
return $parent['cat_id'];
}
}
}
return 0;
}
public function get_parent_name( $parents, $parent_id ){
foreach ( $parents as $parent ){
if ( $parent['cat_id'] == $parent_id ){
return $parent['name'];
}
}
return null;
}
public function get_menu_item($menu_item_name, $link){
global $wpdb;
$table = $wpdb->prefix.'posts';
$post = $wpdb->get_row("SELECT * FROM {$table} WHERE post_title='{$menu_item_name}' AND post_type='nav_menu_item'"); // url='{$link}'");
return $post;
}
public function in_multiarray($elem, $array){
//checks if $elem is in multidimensional $array
}

Invalid Argument Error

Im getting an error at the following URL:
http://www.greenmonkeypublicrelations.com/citypads/apartments/the-ivinghoe-2-bedroom-1-bathroom-apartment/
The purpose here is to identify if the post has children, if it has children, to list the terms of those children and echo the permalink to the post as the href and echo the term title as the text to show. Also if the post is a child, then to get the parent post and list the same to show the links on both levels.
Heres my code:
<?php
if( has_children() ){
$postid = get_the_ID();
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
} elseif (!has_children()) {
if ( wp_get_post_parent_id( get_the_id() ) ){
$postid = wp_get_post_parent_id( get_the_id() );
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
} elseif( ( !has_children() ) && (!get_post_ancestors( $post->ID ))) {
echo 'on its own';
} ?>
The problem is that you expect $children_array or $terms are always arrays. foreach expects an array, so if the $children_array or $terms have the value (i.e.) false you'll get this error.
Can you try this:
<?php
if( has_children() ){
$postid = get_the_ID();
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
if( is_array( $children_array ) ) {
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
if( is_array( $terms ) ) {
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
}
} elseif (!has_children()) {
if ( wp_get_post_parent_id( get_the_id() ) ){
$postid = wp_get_post_parent_id( get_the_id() );
$args = array(
'post_parent' => $postid,
'post_type' => 'any',
'numberposts' => -1,
'post_status' => 'any'
);
$children_array = get_children( $args, OBJECT );
if( is_array( $children_array ) ) {
foreach ($children_array as $child) {
$terms = get_the_terms( $child->ID, 'Apartment_type' );
if( is_array( $terms ) ) {
foreach ($terms as $term) {
echo '' . $term->name . '<br/>';
}
}
}
}
}
} elseif( ( !has_children() ) && (!get_post_ancestors( $post->ID ))) {
echo 'on its own';
} ?>

wordpress sub taxonomies are not showing properly

I want to display only 1st level of sub taxonomies, below is my code,it's just not working, can anyone please tell whats wrong with my code?? Many thanks.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array('child_of' => $term_id, 'parent' => $term_id);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>
Add hide_empty.
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array(
'child_of' => $term_id,
'parent' => $term_id,
'hide_empty' => false // ADDED
);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach($termchildren AS $child) {
$term = get_term_by('id', $child, $taxonomy_name);
echo '<li>' . $term->name . '</li>';
}
echo '</ul>';
?>

Categories