Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:38
Show Gist options
  • 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;
}
}
@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