Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattiasghodsian/14b9b9960bc3bc2103580e7300718175 to your computer and use it in GitHub Desktop.
Save mattiasghodsian/14b9b9960bc3bc2103580e7300718175 to your computer and use it in GitHub Desktop.
Woocommerce Get product attributes
/**
* Title: Woocommerce Get product attributes
* Author: Mattias Ghodsian
* Description: Get product attributes in a simple way (array)
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
*
* @param integer $postID
* return array
**/
function wooGetProductAttributes($postID = "") {
global $woocommerce, $post, $product;
$oldAttr = get_post_meta($product->id);
if (array_key_exists('_product_attributes', $oldAttr) && !empty($oldAttr["_product_attributes"][0])) {
$oldAttr = unserialize( $oldAttr["_product_attributes"][0] );
$newAttr = [];
if (empty($postID)) {
$postID = $post->ID;
}
foreach ($oldAttr as $key => $attr) {
foreach ( get_the_terms($postID, $attr['name']) as $key => $term) {
$term->url = get_term_link($term->term_id, $term->taxonomy);
$newAttr[$term->taxonomy][$term->slug] = (array)$term;
}
}
return $newAttr;
}else{
return false;
}
}
@CHINOBv
Copy link

CHINOBv commented Aug 13, 2020

Hola, disculpa, como podria obtener todos los terminos de un atributo, ejemplo, marcas y mostrarlos con un shortcode?
Hi, how to can get all terms of attribute, example, marks, and show with short code?
Sorry, i learning english :/

@mattiasghodsian
Copy link
Author

mattiasghodsian commented Aug 14, 2020

Hola, disculpa, como podria obtener todos los terminos de un atributo, ejemplo, marcas y mostrarlos con un shortcode?
Hi, how to can get all terms of attribute, example, marks, and show with short code?
Sorry, i learning english :/

This snippet returns all the attributes, you will need to create a shortcode with witch data you would like to display

Example (not tested):

function custom_get_woo_attr( $atts ) {
	$a = shortcode_atts( array(
		'product_id' => 32,
		'taxonomy' => '',
		'slug' => '',
	), $atts );

	$productAttrs = wooGetProductAttributes($a['product_id']);

	return $productAttrs[ $a['taxonomy'] ][ $a['slug'] ];
}
add_shortcode( 'cgwa', 'custom_get_woo_attr' );

And then call it something like this [cgwa product_id="32" taxonomy="" slug=""]

@AdnanSuara
Copy link

Hello!!! =D How can I get the specific value of an attribute to create a conditional later in the checkout page, what I want is that depending on the value it shows me some data or others

Thank you very much

@mattiasghodsian
Copy link
Author

Hello!!! =D How can I get the specific value of an attribute to create a conditional later in the checkout page, what I want is that depending on the value it shows me some data or others

Thank you very much

Haave a look at

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment