Display specific product attributes on Woocommerce archives pages - php

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.

Related

Shortcode that display all product attributes set for a WooCommerce product

I keep searching for a way to do this, but I can't find anything unfortunately.
I an trying to display all the product's attributes and values, separated by a pipe, in a custom place on the single-product (so for that I was thinking to create a shortcode, so I can place it anywhere I want). the output would be something like this:
BRAND: RENAULT | MODEL: 12 | YEAR: 1973
The code on the Woocommerce template product-attributes.php lists the attributes of the current product on single-product page, but it will list it with some styles I don't want in a place I don't want.
I want to create a shortcode with that code, which is:
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<?php echo wp_kses_post( $product_attribute['label'] ); ?>: <?php echo wp_kses_post( $product_attribute['value'] ); ?> |
<?php endforeach; ?>
How can I create a shortcode with it? I know the general code for a shortcode, but I don't know how to actually integrate the above one in it:
function custom_attributes_product_page() {
// integrate the required code
// Output needs to be return
return
}
// register shortcode
add_shortcode('custom-attributes', 'custom_attributes_product_page');
Would be great if this shortcode would list the attributes and their values separated by a column, like I said above (how to do that?)
Any help is highly appreciated.
Try the following shortcode that will display all product attribute(s) set for a product and their value(s), handling custom attributes too:
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
USAGE: [display-attributes] or with a defined product Id [display-attributes id="254"]
You will get a display like: BRAND: RENAULT | MODEL: 12 | YEAR: 1973
If you don't want the linked terms, replace:
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
by this:
$option_values[] = $term->name;

Display linked product attribute term names in Woocommerce

This is my code for display an atributte below a product title. How can I display it like a link to archive page of this attributte?
add_action( 'woocommerce_single_product_summary', 'custom_template_single_title', 5 );
function custom_template_single_title() {
global $product;
$brand_name = $product->get_attribute('Autor');
echo '<div class ="author-product">';
if( $brand_name )
echo $brand_name;
echo '</div>';
}
First, $product->get_attribute('Autor') can give multiple coma separated term names.
Below, we add the term link to each term name (if there is more than one):
add_action( 'woocommerce_single_product_summary', 'custom_template_single_title', 5 );
function custom_template_single_title() {
global $product;
$taxonomy = 'pa_autor'; // <== The product attribute taxonomy
$linked_terms = []; // Initializing
if ( $term_names = $product->get_attribute($taxonomy) ) {
// Loop through the term names
foreach( explode(', ', $term_names) as $term_name ) {
$term_id = get_term_by('name', $term_name, $taxonomy)->term_id; // get the term ID
$term_link = get_term_link( $term_id, $taxonomy ); // get the term link
$linked_terms[] = '' . $term_name . '';
}
// Output
echo '<div class ="author-product">' . implode(', ', $linked_terms) . '</div>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Display custom taxonomy term images for WooCommerce product attributes

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.

Display product attributes on specific Woocommerce product category archives page

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

Display specific product attributes under product title in Woocommerce archive pages

In woocommerce, I would like to show some product attributes on shop page under the product titles. This product attributes are "year", "model" and "oil".
This is what I have for now:
add_action('woocommerce_shop_loop_item_title', 'wh_insertAfterShopProductTitle', 15);
function wh_insertAfterShopProductTitle()
{
global $product;
$abv = $product->get_attribute('pa_year');
if (empty($abv))
return;
echo __($abv, 'woocommerce');
}
Any help is appreciated.
To display "year", "model" and "oil" product attributes under the product titles in Woocommerce archive pages as shop, use the following:
add_action('woocommerce_shop_loop_item_title', 'display_attributes_after_product_loop_title', 15);
function display_attributes_after_product_loop_title(){
global $product;
$output = array(); // Initializing
// The year
if( $year = $product->get_attribute('pa_year') ){
// Save the value in the array
$output[] = $year;
}
// The model
if( $model = $product->get_attribute('pa_model') ){
// Save the value in the array
$output[] = $model;
}
// The type of oil
if( $oil = $product->get_attribute('pa_oil') ){
// Save the value in the array
$output[] = $oil;
}
// Output
if( sizeof($output) > 0 ){
// Display product attributes coma separated values (you can change the separator by something else below).
echo implode( ', ', $output);
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Categories