Get Custom Product Attributes in Woocommerce

Get custom product attributes in Woocommerce

Edited: The woocommerce_get_product_terms is deprecated since Woocommerce version 3

Go with the following as @datafeedr wrote in his answer:

global $product;
$koostis = array_shift( wc_get_product_terms( $product->id, 'pa_koostis', array( 'fields' => 'names' ) ) );

or even more compact:

global $product;
$koostis = $product->get_attribute( 'pa_koostis' );

Original answer:

$result = array_shift(woocommerce_get_product_terms($product->id, 'pa_koostis', 'names'));

Get product custom attributes to display them in WooCommerce product loop

Update 3

With product attributes (custom or not), you can use the WC_Product method get_attribute() where you can input an attribute name, slug or taxonomy. So in your code:

add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;

$value1 = $product->get_attribute('Square meters');
$value2 = $product->get_attribute('Rooms');
$value3 = $product->get_attribute('Toilets');

if ( ! empty($value1) || ! empty($value2) || ! empty($value3) ) {

echo '<div class="items" style="color: red;"><p>';

$attributes = array(); // Initializing

if ( ! empty($value1) ) {
$attributes[] = __("Square meters:") . ' ' . $value1;
}

if ( ! empty($value2) ) {
$attributes[] = __("Rooms:") . ' ' . $value2;
}

if ( ! empty($value3) ) {
$attributes[] = __("Toilets:") . ' ' . $value3;
}

echo implode( '<br>', $attributes ) . '</p></div>';
}
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.



Automate the code from a simple array of product attribute names:

The code below will gives the same output, but it's compacted, optimized and requires just an array of your desired product attributes:

add_action('woocommerce_after_shop_loop_item_title', 'display_custom_product_attributes_on_loop', 5 );
function display_custom_product_attributes_on_loop() {
global $product;

// Settings: Here below set your product attribute label names
$attributes_names = array('Square meters', 'Rooms', 'Toilets');

$attributes_data = array(); // Initializing

// Loop through product attribute settings array
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}

if ( ! empty($attributes_data) ) {
echo '<div class="items" style="color: red;"><p>' . implode( '<br>', $attributes_data ) . '</p></div>';
}
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

WooCommerce - Get custom product attribute

Update: Added compatibility for Woocommerce 3+

As attributes are always prepend with pa_ in DB, for getting them with wc_get_product_terms() function, you will need to use pa_pdfs instead of pdfs, this way:

global $product;

$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support

$myPdf = array_shift( wc_get_product_terms( $product_id, 'pa_pdfs', array( 'fields' => 'names' ) ) );

Reference: How to get a products custom attributes from WooCommerce

Get product attributes in Woocommerce 3

Updated: Compacting your code for testing:

$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_names = $attribute;
// testing output
var_dump($attribute_name);
endforeach;

The var_dump($attribute_name); raw output gives you indications about the objects which are WC_Product_Attribute objects, meaning that you have to use the available methods for this class.

THERE IS 2 WAYS:

1) You can access properties in an unprotected array using the get_data() method this way:

$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_data = $attribute->get_data();
// testing pre-formatted output
echo '<pre>'; print_r($attribute_data); echo '</pre>';
// We stop the loop to get the first object only (for testing)
break;
endforeach;

That will gives you a raw output like:

Array (
[id] => 1
[name] => pa_color
[options] => Array (
[0] => 8
[1] => 9
)
[position] => 0
[visible] =>
[variation] => 1
[is_visible] => 0
[is_variation] => 1
[is_taxonomy] => 1
[value] =>
)

And then you can use it this way:

$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_data = $attribute->get_data(); // Get the data in an array

$attribute_name = $attribute_data['name']; // The taxonomy slug name
$attribute_terms = $attribute_data['options']; // The terms Ids
endforeach;

2) You can use the WC_Product_Attribute methods like:

$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_name = $attribute->get_taxonomy(); // The taxonomy slug name
$attribute_terms = $attribute->get_terms(); // The terms
$attribute_slugs = $vaattributeues->get_slugs(); // The term slugs
endforeach;

Display WooCommerce Custom Product Attributes and all terms on single products

The problem comes from the following in your code:

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

foreach ( $terms as $term_name )
$term_names['names'] = $term_name;

$out .= implode(', ', $term_names);

That you can replace with this:

            $terms = wp_get_post_terms( $product->get_id(), $taxonomy, array('fields' => 'names') );

$term_names = []; // Initializing

// Loop through each terms
foreach ( $terms as $term_name ) {
$term_names[] = $term_name;
}

$out .= implode(', ', $term_names);

Or even better with one line of code:

            $out .= $product->get_attribute( $taxonomy );

Display custom product attribute value in WooCommerce product page

You could use the following using WC_Product get_attribute() method:

global $product; // if the WC_Product object is not defined

if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( get_the_ID() );
}

echo '<p>' . $product->get_attribute( 'secimg' ) . '</p>';

Its should work.

How to get custom product attributes on custom product template in woocommerce

You can try the following

1.get_post_meta to get the product attributes

$attr = get_post_meta( 123, '_product_attributes' ); // replace 123 with the actual product id 
print_r( $attr );

2.Or you can create a product object and then using the get_attributes method

$p = new WC_Product( 123 ); // // replace 123 with the actual product id 
print_r( $p->get_attributes() );

Display specific custom product attributes on single product pages in Woocommerce

In the following code you will define first the desired product attributes slugs in an array, that will get displayed in single product pages:

add_action( 'woocommerce_single_product_summary', 'display_some_product_attributes', 25 );
function display_some_product_attributes(){
// HERE define the desired product attributes to be displayed
$defined_attributes = array('fyllighet', 'carrier', 'billing-e-number');

global $product;
$attributes = $product->get_attributes();

if ( ! $attributes ) {
return;
}

$out = '<ul class="taste-attributes">';

foreach ( $attributes as $attribute ) {

// Get the product attribute slug from the taxonomy
$attribute_slug = str_replace( 'pa_', '', $attribute->get_name() );

// skip all non desired product attributes
if ( ! in_array($attribute_slug, $defined_attributes) ) {
continue;
}

// skip variations
if ( $attribute->get_variation() ) {
continue;
}

$name = $attribute->get_name();

if ( $attribute->is_taxonomy() ) {

$terms = wp_get_post_terms( $product->get_id(), $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->singular_name ) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}

$out .= '<li class="' . esc_attr( $name ) . '">';
$out .= '<p class="attribute-label">' . esc_html( $tax_label ) . ': </p> ';
$tax_terms = array();

foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
// Insert extra code here if you want to show terms as links.
array_push( $tax_terms, $single_term );
}

$out .= '<span class="attribute-value">' . implode(', ', $tax_terms) . '</span><progress value="' . implode(', ', $tax_terms) .
'" max="10"><div class="progress-bar"><span style="width:'
. implode(', ', $tax_terms) . '0%">'
. implode(', ', $tax_terms) . '</span></div></progress></li>';

} else {
$value_string = implode( ', ', $attribute->get_options() );
$out .= '<li class="' . sanitize_title($name) . ' ' . sanitize_title( $value_string ) . '">';
$out .= '<p class="attribute-label">' . $name . ': </p> ';
$out .= '<progress value="' . esc_html( $value_string ) . '" max="10"></progress></li>';
}
}

$out .= '</ul>';

echo $out;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.



Related Topics



Leave a reply



Submit