Skip to content

Instantly share code, notes, and snippets.

@moccy
Created February 20, 2018 18:10
Show Gist options
  • Save moccy/3d6346d4eed4652061a8dd52da8715ba to your computer and use it in GitHub Desktop.
Save moccy/3d6346d4eed4652061a8dd52da8715ba to your computer and use it in GitHub Desktop.
Product Attribute List Code
// Returns an array of product attributes, each with a name and an array of values.
public function get_attribute_list() {
// Assumes $this is a WC_Product object.
$attributes = $this->get_attributes();
$spec_items = array();
foreach ($attributes as $attribute) {
array_push($spec_items, array(
'name' => wc_attribute_label($attribute->get_name()),
'values' => $this->get_attribute_term_names($attribute)
));
}
return $spec_items;
}
private function get_attribute_term_names(WC_Product_Attribute $attribute) {
$term_names = array();
foreach($attribute->get_terms() as $term) {
array_push($term_names, $term->name);
}
return $term_names;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment