Show specific multiple attributes in WooCommerce product page - php

I am trying to show multiple attributes on WooCommerce product page. I found this code but it is for only one attribute. I am not a PHP developer so I can't make the code to get more attributes. Can anyone help me?
function get_product_attributes_shortcode( $atts ) {
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') ) {
$styletypes = $product->get_attribute( 'styletypes' );
return '<div class="product-attributes"><strong>Styletype</strong>: ' . $styletypes . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );

function get_product_attributes_shortcode($atts) {
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')) {
$attr_str = '<div class="product-attributes">';
foreach ($product->get_attributes() as $attr_key => $attr) {
$attr_str.= '<strong>'.$attr['name'].'</strong>: ';
foreach ($attr['options'] as $attr_opt) {
$attr_str.= $attr_opt .' | ';
}
$attr_str = rtrim($attr_str, ' | ');
$attr_str.= '<br/>';
}
$attr_str.= '</div>';
return $attr_str;
}
}
add_shortcode('display-attributes', 'get_product_attributes_shortcode');

Related

Adding meta fields to attribute list on WooCommerce single product page

Based on Get product custom attributes to display them in WooCommerce product loop answer code.
I am displaying specific product attributes on the single product page with this:
add_action('woocommerce_single_product_summary', 'display_custom_attributes', 36 );
function display_custom_attributes() {
global $product;
$attributes_names = array('Brand', 'Color', 'Size');
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<h4>Details</h4><ul><li>' . implode( '</li><li>', $attributes_data ) . '</ul>';
}
Now I also need to add two custom meta fields ('Serial_Number' and 'MPN') to this list.
How can I add these?
To add two custom meta fields:
Serial_Number
MPN
to this list, you can use:
function action_woocommerce_single_product_summary() {
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
// NOT empty
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}
// Get meta
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// NOT empty
if ( ! empty ( $sn ) ) {
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
}
// NOT empty
if ( ! empty ( $mpn ) ) {
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
Add this code on the function.php theme file
function call_product_meta_data()
{
global $post;
if ($post) {
$post_meta_value = get_post_meta($post->ID, 'post_meta_key', true);
if ($post_meta_value != null) {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: $post_meta_value</p></div>";
} else {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: N/A</p></div>";
}
}
}
add_action('woocommerce_single_variation', 'call_product_meta_data');

Wordpress Woocommerce Show attributes on shop page

I want to add some attributes to the shop page of wordpress.
This code i found on Stackoverflow, is shows all attribute labes but on all the same attribute names.
add_action('woocommerce_after_shop_loop_item_title','add_attribute');
function add_attribute() {
global $product;
$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );
$attr_output = array();
foreach( $product_attributes as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = get_taxonomy( $taxonomy )->labels->singular_name;
$value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');
if( ! empty($value) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}}
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
current state
I just need a bit help to get it to show all the right attributes.
There a some little mistakes in your code. Try the following instead:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
Code goes in functions.php file of your active child theme (or active theme). tested and works.
For simple products only you will use the following instead:
add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
global $product;
// Only for simple products
if ( ! $product->is_type( 'simple' ) ) return;
// Define you product attribute taxonomies in the array
$product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
$attr_output = array(); // Initializing
// Loop through your defined product attribute taxonomies
foreach( $product_attribute_taxonomies as $taxonomy ){
if( taxonomy_exists($taxonomy) ){
$label_name = wc_attribute_label( $taxonomy, $product );
$term_names = $product->get_attribute( $taxonomy );
if( ! empty($term_names) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
}
}
}
// Output
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
Code goes in functions.php file of your active child theme (or active theme). tested and works.
I solved the problem. its not pretty but it works.
add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_country';
echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_class';
echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_faction';
echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
if ( $product->is_type('simple') ) {
$taxonomy = 'pa_gender';
echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}

Adding an attributes column to WooCommerce admin products list

I was given code from someone on the Wordpress forum, but it isn't quite right.
It has created a column in my Product Admin called Attributes, and it is bringing in the name of the attributes, but not options. i.e. it looks like
"coloursizeyearcountry"
And I would like
"Colour = Red, Size = large, Year = 2020, Country = UK"
or something like it.
The code I have so far is:
function add_product_column( $columns ) {
//add column
$columns['new_column'] = __( 'New column', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
function add_product_column_content( $column, $postid ) {
if ( $column == 'new_column' ) {
// Get product object
$product = wc_get_product( $postid );
// Get Product Variations
$product_attributes = $product->get_attributes();
foreach ( $product_attributes as $product_attribute ) {
$attribute_name = $product_attribute->get_name();
echo str_replace( 'pa_', '', $attribute_name );
}
}
}
add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );
The following code will help you get what you want. Explanation via the comment tags added in the code
function add_product_column( $columns ) {
//add column
$columns['new_column'] = __( 'New column', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
function add_product_column_content( $column, $postid ) {
if ( $column == 'new_column' ) {
// output variable
$output = '';
// Get product object
$product = wc_get_product( $postid );
// Get Product Variations - WC_Product_Attribute Object
$product_attributes = $product->get_attributes();
// Not empty, contains values
if ( !empty( $product_attributes ) ) {
foreach ( $product_attributes as $product_attribute ) {
// Get name
$attribute_name = str_replace( 'pa_', '', $product_attribute->get_name() );
// Concatenate
$output = $attribute_name . ' = ';
// Get options
$attribute_options = $product_attribute->get_options();
// Not empty, contains values
if ( !empty( $attribute_options ) ) {
foreach ($attribute_options as $key => $attribute_option ) {
// WP_Term Object
$term = get_term($attribute_option); // <-- your term ID
// Not empty, contains values
if ( !empty( $term ) ) {
$term_name = $term->name;
// Not empty
if ( $term_name != '' ) {
// Last loop
end($attribute_options);
if ( $key === key($attribute_options) ) {
// Concatenate
$output .= $term_name;
} else {
// Concatenate
$output .= $term_name . ', ';
}
}
}
}
}
echo $output . '<br>';
}
}
}
}
add_action( 'manage_product_posts_custom_column', 'add_product_column_content', 10, 2 );

Display Variable Product Attributes and Terms on Woocommerce Archives

I am trying to accomplish a attribute and term list on the shop page using the hook woocommerce_shop_loop_item_title. The goal is to get the attribute(s) and term(s) for the product and then to display it like this example:
Color: Red, Blue, Green
Size: Small, Medium, Large
Dimensions: 90*90, 100*100 and 120*120
but without the spaces between the rows.
It should "fetch" all the attributes used with the product and the attributes terms.
I've tried this but got fatal error.
add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
function variable_att_and_terms_on_loop() {
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {
$taxonomy_label = wc_attribute_label( $taxonomy, $product );
foreach($terms_slug as $term) {
$term_name = get_term_by('slug', $term, $taxonomy)->name;
$attributes_and_terms_names[$taxonomy_label][$term] = $term_name;
}
}
foreach ( $attributes_and_terms_names as $attribute_name => $terms_name ) {
$terms_string = implode( ', ', $terms_name );
echo '<p>' . $attribute_name . ': ' . $terms_string . '</p>';
}
}
I've also tried this:
add_action('woocommerce_shop_loop_item_title','add_attribute', 5);
function add_attribute() {
global $product;
$product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
$attr_output = array();
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) ){
$attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
}
}
}
echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}
without any result.
After trying the new result below from LoicTheAztec, this is what I get:
Uppdate 2020 - Removed an error when trying to get the term name from a term slug.
In your first code snippet there are some mistakes:
the $product variable was not defined
The function needed to be restricted to variable products only
the $attributes_and_terms_names variable was not initialized…
Here is the revisited code (without the spaces between the rows):
add_action( 'woocommerce_shop_loop_item_title', 'variable_att_and_terms_on_loop');
function variable_att_and_terms_on_loop() {
global $product;
if( ! $product->is_type('variable') ) return; // Only for variable products
$variation_attributes = $product->get_variation_attributes();
if( sizeof($variation_attributes ) == 0 ) return; // Exit if empty
$attributes = array(); // Initializing
foreach( $product->get_variation_attributes() as $taxonomy => $terms_slug ) {
$taxonomy_label = wc_attribute_label( $taxonomy, $product );
$terms_name = array();
foreach($terms_slug as $term_slug ) {
// We try to get the term name when it's a term slug
$term = get_term_by('slug', $term_slug, $taxonomy);
$terms_name[] = ! is_a($term, 'WP_Term') ? $term_slug : $term->name;
}
$attributes[] = $taxonomy_label . ': ' . implode( ', ', $terms_name );
}
echo '<div class="product-attributes">';
echo '<span>' . implode('</span><br><span>', $attributes) . '</span>';
echo '</div>';
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Display atributes value and name in cart woocomerce

Simple question how to display product atributes in cart woocomerce: for example color:red, not sure if there is some code to add like hook or some code to fundctions.php or it can be done through woocomerce settings, have not found any useful information online, any help is appreciated.
Just do a simple things as follows, you will get all in your cart_item -
add_filter('woocommerce_cart_item_name', 'add_variations_in_cart', 10, 3);
function add_variations_in_cart($name, $cart_item, $item_key){
$product_variation = '';
if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0 ){
if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){
foreach ($cart_item['variation'] as $key => $value) {
$product_variation .= '<br>'.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value);
}
}
}
echo $name.$product_variation;
}
as simple as that. Thank you.
tyr this plugin https://wordpress.org/plugins/woocommerce-show-attributes/
OR
WooCommerce: show all product attributes listed below each item on Cart page
add this below code in function.php
/**
* WooCommerce: show all product attributes listed below each item on Cart page
*/
function sm_woo_cart_attributes($cart_item, $cart_item_key){
$item_data = $cart_item_key['data'];
$attributes = $item_data->get_attributes();
if ( ! $attributes ) {
return $cart_item;
}
$out = $cart_item . '<br />';
foreach ( $attributes as $attribute ) {
if ( $attribute['is_taxonomy'] ) {
// skip variations
if ( $attribute['is_variation'] ) {
continue;
}
// backwards compatibility for attributes which are registered as taxonomies
$product_id = $item_data->id;
$terms = wp_get_post_terms( $product_id, $attribute['name'], 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ($tax_object->labels->name) ) {
$tax_label = $tax_object->labels->name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
}
foreach ( $terms as $term ) {
$out .= $tax_label . ': ';
$out .= $term->name . '<br />';
}
} else {
// not a taxonomy
$out .= $attribute['name'] . ': ';
$out .= $attribute['value'] . '<br />';
}
}
echo $out;
}
add_filter( 'woocommerce_cart_item_name', sm_woo_cart_attributes, 10, 2 );

Categories