WC_Product_Variation::get_attributes PHP Method

get_attributes() public method

Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored.
public get_attributes ( string $context = 'view' ) : array
$context string
return array
    public function get_attributes($context = 'view')
    {
        return $this->get_prop('attributes', $context);
    }

Usage Example

 /**
  * Get the attributes for a product or product variation
  *
  * @since 2.1
  * @param WC_Product|WC_Product_Variation $product
  * @return array
  */
 private function get_attributes($product)
 {
     $attributes = array();
     if ($product->is_type('variation')) {
         // variation attributes
         foreach ($product->get_variation_attributes() as $attribute_name => $attribute) {
             // taxonomy-based attributes are prefixed with `pa_`, otherwise simply `attribute_`
             $attributes[] = array('name' => wc_attribute_label(str_replace('attribute_', '', $attribute_name)), 'slug' => str_replace('attribute_', '', str_replace('pa_', '', $attribute_name)), 'option' => $attribute);
         }
     } else {
         foreach ($product->get_attributes() as $attribute) {
             // taxonomy-based attributes are comma-separated, others are pipe (|) separated
             if ($attribute['is_taxonomy']) {
                 $options = explode(',', $product->get_attribute($attribute['name']));
             } else {
                 $options = explode('|', $product->get_attribute($attribute['name']));
             }
             $attributes[] = array('name' => wc_attribute_label($attribute['name']), 'slug' => str_replace('pa_', '', $attribute['name']), 'position' => (int) $attribute['position'], 'visible' => (bool) $attribute['is_visible'], 'variation' => (bool) $attribute['is_variation'], 'options' => array_map('trim', $options));
         }
     }
     return $attributes;
 }
All Usage Examples Of WC_Product_Variation::get_attributes