Created
February 20, 2018 18:10
-
-
Save moccy/3d6346d4eed4652061a8dd52da8715ba to your computer and use it in GitHub Desktop.
Product Attribute List Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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