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
Related
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
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 want to show attibutes from a simple product, in cart and checkout. Just like variable products does, but there is only one attibute. See image below:
Is this possible to achive with PHP?
I was thinking about something like using echo $product->get_attributes()
Add the follows code snippets to achieve your above task -
function modify_woocommerce_get_item_data( $item_data, $cart_item ) {
if( $item_data || $cart_item['data']->is_type( 'variation' ) ) return $item_data;
if ( $cart_item['data']->is_type( 'simple' ) ) {
$attributes = array_filter( $cart_item['data']->get_attributes(), 'wc_attributes_array_filter_visible' );
foreach ( $attributes as $attribute ) {
$values = array();
if ( $attribute->is_taxonomy() ) {
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $cart_item['data']->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
$value_name = esc_html( $attribute_value->name );
if ( $attribute_taxonomy->attribute_public ) {
$values[] = '' . $value_name . '';
} else {
$values[] = $value_name;
}
}
} else {
$values = $attribute->get_options();
foreach ( $values as &$value ) {
$value = make_clickable( esc_html( $value ) );
}
}
$item_data[] = array(
'key' => wc_attribute_label( $attribute->get_name() ),
'value' => apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values ),
);
}
}
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'modify_woocommerce_get_item_data', 99, 2 );
Codes goes to your active theme's functions.php
Yes you can. Please try adding a filter as bellow,
add_filter('woocommerce_cart_item_name', function($name, $cart_item) {
//has attributes
if ($cart_item['data']->is_type( 'simple' ) && $attributes = $cart_item['data']->get_attributes()) {
$name .= " - ";
foreach ($attributes as $att)
$name .= $att->get_name() . " : " . implode(',', $att->get_options());
}
return $name;
}, 10, 2);
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 have a category with products not visible to everyone.
I already have a script in place, that removes this category from the sidebar widget, when no items in the category are visible to the user.
I created a function that puts the exclude term_id in a global variable.
Now I need something that excludes them from view in the shop.
$GLOBALS['cat_exclude'] = NULL;
function getExcludedCats( ) {
//if( ! is_admin() && (is_product_category() || is_shop())){
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$parentid = $term->term_id;
$args = array(
'hide_empty' => true,
'parent' => $parentid
);
$product_categories = get_terms( 'product_cat', $args );
$exclude = array();
foreach ( $product_categories as $category ) {
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new WC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
if ( false === $show_category ) {
$exclude[] = $category->term_id;
}
}
if ( ! empty( $exclude ) ) {
$GLOBALS['cat_exclude'] = implode( ',', $exclude );
}
//}
}
add_action('wp_head', 'getExcludedCats');
However, in the shop view itself, the category is still visible. How can I remove it there, when no items in this category are visible to the user.
I have tried:
https://gist.github.com/rynaldos/a9d357b1e3791afd9bea48833ff95994
But it removes the category ALWAYS, in both widget and shop.
Products are being displayed by group membership of the customer:
https://wordpress.org/plugins/groups/
I think I managed to create a script that made this possible:
https://gist.github.com/DarkAllMan/cffb114eb97c6f26882e54793e023587
<?php
/**
Plugin Name: WooCommerce - Hide categories where no products are visible to user
Plugin URI: https://www.randall.nl
Description: Excludes categories with no visible products from the WooCommerce category overview in shop
Version: 0.1
Author: Randall Kam
Author URI: https://www.randall.nl
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( !class_exists( 'ExcludeCats' ) ) :
class ExcludeCats {
public $version = '0.1',
$exclude = array();
protected static $_instance = null;
/**
* Main Plugin Instance
*
* Ensures only one instance of plugin is loaded or can be loaded.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*/
public function __construct() {
// CHECK CATEGORIES FOR VISIBLE PRODUCTS
add_action('wp_head', array( $this, 'get_excluded_cats' ), 10, 3 );
// ADD THE WIDGET SIDEBAR FILTER
add_filter( 'woocommerce_product_categories_widget_args', array( $this, 'kfg_exclude_categories_from_widget'), 10, 1 );
// ADD THE SHOP FILTER
add_filter( 'woocommerce_product_subcategories_args', array( $this, 'filter_woocommerce_product_subcategories_args'), 10, 1 );
}
// GET CATEGORIES WITH NO VISIBLE PRODUCTS AND PUT IN GLOBAL IF GLOBAL FALSE
public function get_excluded_cats( $terms, $taxonomies, $args ) {
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$args = array(
'parent' => $term_id,
'hide_empty' => false,
'hierarchical' => false,
);
$product_categories = get_terms( 'product_cat', $args );
// if a product category and on the shop page
//if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
if ( ! is_admin() && is_shop() ) {
foreach ( $product_categories as $key => $term ) {
unset($this->exclude);
if($term->taxonomy=='product_cat'){
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $term->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new WC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
}
if ( false === $show_category ) {
$this->exclude[] = $term->term_id;
}
}
}
}
public function get_parent_cats ($cat_termid, $found = array()) {
array_push ($found, $cat_termid);
$term =get_term_by( 'term_id', $cat_termid, 'product_cat');
if($term->parent > 0){
return get_parent_cats($term->parent, $found);
}
return $found;
}
// ADD FILTERS FOR CATEGORIES AND EXCLUDE EMPTY
public function filter_woocommerce_product_subcategories_args( $temp_args = array() ) {
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$temp_args = array(
'parent' => $term_id,
'menu_order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'product_cat',
'pad_counts' => 1,
'include' => NULl,
'exclude' => $this->exclude,
);
return $temp_args;
}
public function kfg_exclude_categories_from_widget( $category_list_args ) {
$current_tax = get_query_var( 'product_cat' );
$term = get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$parents = $this->get_parent_cats($term_id);
$args = array(
'hide_empty' => false,
'hierarchical' => true,
);
$product_categories = get_terms( 'product_cat', $args );
$wexclude = array();
foreach ( $product_categories as $category ) {
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new wC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
if ( false === $show_category || ( $category->parent > 0 && !in_array($category->parent,$parents) ) ) {
$wexclude[] = $category->term_id;
}
}
if ( ! empty( $wexclude ) ) {
$category_list_args['exclude'] = implode( ',', $wexclude );
unset( $category_list_args['include'] );
}
return $category_list_args;
}
} // class ExcludeCats
endif; // class_exists
/**
* Returns the main instance of the plugin class to prevent the need to use globals.
*
* #since 2.0
* #return WooCommerce_PostcodeAPInu
*/
function ExcludeCats() {
return ExcludeCats::instance();
}
ExcludeCats(); // load plugin