In WooCommerce I am using Category and Taxonomy Image plugin that allow me to add the images to product attribute terms.
Now I am trying to display for a specific product attribute, the related term images for each product on shop page.
The author of Category and Taxonomy Image plugin metion to use the following code to display a term image:
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//It will give category/term image url
}
echo $meta_image; // category/term image url
I am using the code below to display "color" product attribute term names on the shop page:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$spec_val = $product->get_attribute('spec');
if(!empty($spec_val)) {
echo'<span class="view_attr"> SPECIFICATION: ' . $spec_val . '</span>';
}
}
How To display the term images?
Maybe this is the solution:
add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
global $product;
$ingredients = $product->get_attributes( 'color' );
foreach( $ingredients as $attr_name => $attr ){
foreach( $attr->get_terms() as $term ){
if ( wc_attribute_label( $attr_name ) == "Color" ) {
echo $term->name ;
$meta_image = get_wp_term_image($term->term_id);
echo '<img src="'.$meta_image.'"/>';
}
else echo '';
}
}
}
Product attributes is something very specific and more complex in WooCommerce than other taxonomies. Each product attribute is a taxonomy, has its own terms and can be used for variations on variable products...
The plugins Taxonomy Images and Category and Taxonomy Image allow to have images on all WooCommerce custom taxonomies terms as Product tag and Product attributes (product category has already this feature by default).
Here we use Category and Taxonomy Image and its dedicated function get_wp_term_image().
In the code below you can enable multiple product attributes defined in an array. if the option "Enable Archives?" is enable for the product attribute, you can optionally use the term links.
add_action('woocommerce_after_shop_loop_item','woo_new_product_tab_content');
function woo_new_product_tab_content() {
global $product;
// Define your product attribute labels in the array (label names)
$defined_pa_labels = array( 'Color' );
// Loop through WC_Product_Attribute Objects
foreach( $product->get_attributes() as $taxonomy => $product_attribute ) {
$taxonomy_name = $product_attribute->get_name(); // Slug
$taxonomy_label = wc_attribute_label( $taxonomy_name ); // Name (label name)
if( in_array( $taxonomy_label, $defined_pa_labels ) ) {
// Loop through product attribute WP_Term Objects
foreach( $product_attribute->get_terms() as $term ) {
$term_name = $term->name; // Term name
$term_slug = $term->slug; // Term slug
$term_id = $term->term_id; // Term ID
// Get product attribute term image
if( $image_url = get_wp_term_image( $term_id ) ) {
// Get product attribute term link (optional)
// if the product attribute is enabled on archives)
$term_url = get_term_link( $term, $taxonomy );
// Output
echo '<span style="text-align:center"><img src="'.esc_url( $image_url).'"/>'.$term->name.'</span>';
}
}
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Related
With the following code I populate a select dropdown item in Admin product page which I add, through a function (in my theme's functions.php). For instance, I managed to get the list of all my product attributes (taxanomy):
<?php
$attributes = wc_get_attribute_taxonomies();
if($attributes) {
foreach ( $attributes as $attribute ) {
echo '<option value="'. $attribute->attribute_id .'">' . $attribute->attribute_label . '</option>';
}
}
?>
Any idea how the get the term names (labels) and ids of all the terms under a specific product attribute (taxanomy), for example pa_test?
You can use function get_terms() to get all the terms of our product attribute taxonomies as follows (here for product attribute pa_test taxonomy):
$taxonomy = 'pa_test';
$terms = get_terms( array('taxonomy' => $taxonomy, 'hide_empty' => false) );
// Loop through the terms for 'pa_test' taxonomy
foreach ( $terms as $term ) {
$term_name = $term->name; // name
$term_slug = $term->slug; // slug
$term_id = $term->term_id; // Id
$term_link = get_term_link( $term ); // Link
}
So I'm working with the sinlge-download.php page and I'm trying to check if the specific product is in a specific category.
Here is what I tried but I only get the ELSE result even if the download is a book.
if( in_category( 'Books' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}
According to EDD docs, the category is: download_category Easy Digital Download Docs
For this... use function has_term since in_category refers to WordPress post type posts and not for custom post types like the downloads.
if( has_term( 'Books', 'download_category' ) ) {
echo 'This product is a book';
} else {
echo 'This product is not a book';
}
You can use this
if( has_term( $term = '', $taxonomy = '', $post = null ) ) {
// do something
}
// $term = Category OR Taxonomy name $taxonomy = Taxonomy Name. OR
// "category" if its default WP category $post = Post ID to check. Leave
// empty to pull this from global query
https://developer.wordpress.org/reference/functions/has_term/
In Woocommerce I have a Product Attribute called "Platform" the Value of the Attribute is "Steam":
So I am bulk importing the products and the Attributes are already there.
But now I have to set for every product manually the category.
Is it possible to set the Value automatically as Product Category in a function?
This Function is returning me the Attribute value right?
function get_attribute_value_from_name( $name ){
global $wpdb;
$name = 'Platform';
$attribute_value = $wpdb->get_var("SELECT attribute_value
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name LIKE '$name'");
return $attribute_value;
}
And now how to set the value for product category?
EDIT:
$product = wc_get_product($id); //LOAD PRODUCT
global $product_attribute; //VARIABLE
$product_attribute = $product->get_attribute( 'Platform' ); //GET ATTRIBUTE OF PLATFORM
wp_set_object_terms( $post_id, $product_attribute, 'product_cat' ); //WRITE IT AS CATEGORY
$product->save(); //SAVE PRODUCT
does this make sense?
Update 2 - To set an existing product category term in a product (using a defined product ID):
// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );
$term_names = $product->get_attribute( 'Platform' ); // Can have many term names (coma separated)
$term_names = explode( ',', $term_names);
$term_ids = [];
// Loop through the terms
foreach( $term_names as $term_name ) {
// Get the term ID and check if it exist
if( $term_id = term_exists( $term_name, 'product_cat' ) ) {
// Add each term ID in an array
$term_ids[] = $term_id;
}
}
// Append the product category terms in the product
if( sizeof($term_ids) > 0 ) {
$product->set_category_ids( $term_ids );
$product->save();
}
Here below is an example of a hooked function that will auto set the product category terms on product edit.
Note: the product category terms need to exist in woocommerce
// Backend product creation
add_action( 'woocommerce_admin_process_product_object', 'add_product_category_terms_to_product', 100, 1 );
function add_product_category_terms_to_product( $product ){
global $pagenow;
// Only on product Edit
if( $pagenow != 'post.php' ) return; // Exit
if( $term_names = $product->get_attribute( 'Platform' ) )
$term_names = explode( ',', $term_names);
else
return; // Exit
$term_ids = [];
// Loop through the terms
foreach( $term_names as $term_name ) {
// Get the term ID and check if it exist
if( $term_id = term_exists( $term_name, 'product_cat' ) ) {
// Add each term ID in an array
$term_ids[] = $term_id;
}
}
// replace the product categories terms in the product
if( sizeof($term_ids) > 0 ) {
$product->set_category_ids( $term_ids );
}
// save is not needed in the function as this hook does that
}
Code goes in function.php file of your active child theme (or active theme). It should works.
I want to show two attributes on the category pages, with the attribute name and value only on specific categories.
This code that I found displays the labels of the attributes, but is duplicating the value and I am really struggling with a show if categories variable. Any help is greatly appreciated.
The code:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_set', 'pa_team');
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute('pa_set');
if( ! empty($value) ){
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.':
'.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode( '<br>', $attr_output
).'</div>';
}
Updated - The problem comes from the following line, where the product attribute attribute value is always for the same product attribute:
$value = $product->get_attribute( 'pa_set' );
and it should be this instead:
$value = $product->get_attribute( $taxonomy );
The complete revisited code will be:
add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
global $product;
$product_attributes = array('pa_set', 'pa_team'); // Defined product attribute taxonomies.
$attr_output = array(); // Initializing
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
if( $value = $product->get_attribute($taxonomy) ){
// The product attribute label name
$label_name = wc_attribute_label($taxonomy);
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode('<br>', $attr_output).'</div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Targeting Product category archive pages:
You will use the conditional tag is_product_category() inside the function on an IF statement…
For specific product category archive pages, you can set them as explained here inside the function in an array, like:
if( is_product_category( array('chairs', 'beds') ) {
// Here go the code to be displayed
}
You will just need to set the right product categories slugs in the array…
Related: Show WooCommerce product attributes in custom home and product category archives
I want to display some specific product attributes of my choice on the store shop page for each product. It is necessary to display the name of the attribute and opposite its value. I started writing code, I wanted to print at least names, but I only show the name of the last attribute
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$weigth_val = $product->get_attribute('weight');
$quant_val = $product->get_attribute('quantity');
$length_val = $product->get_attribute('length');
echo $weigth_val;
echo $quant_val;
echo $length_val;
}
In woocommerce each product attribute is a custom taxonomy and are recorded in database adding pa_ to the beginning of their slugs…
That taxonomy name is to be used with WC_Product get_attribute() method.
So so your code should be instead:
add_action('woocommerce_after_shop_loop_item','displaying_product_attributes');
function displaying_product_attributes() {
global $product;
$weigth_val = $product->get_attribute('pa_weight');
$quant_val = $product->get_attribute('pa_quantity');
$length_val = $product->get_attribute('pa_length');
echo $weigth_val;
echo $quant_val;
echo $length_val;
}
It should work now…
To get the product attribute name label and with the corresponding name value for products you will use:
add_action('woocommerce_after_shop_loop_item','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
$attr_output = array();
// Loop through the array of product attributes
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute('pa_weight');
if( ! empty($value) ){
// Storing attributes for output
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
// Output attribute name / value pairs separate by a "<br>"
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.