WC_Product::get_default_attributes PHP Method

get_default_attributes() public method

Get default attributes.
Since: 2.7.0
public get_default_attributes ( string $context = 'view' ) : array
$context string
return array
    public function get_default_attributes($context = 'view')
    {
        return $this->get_prop('default_attributes', $context);
    }

Usage Example

 /**
  * Get default attributes.
  *
  * @param WC_Product $product Product instance.
  * @return array
  */
 protected function get_default_attributes($product)
 {
     $default = array();
     if ($product->is_type('variable')) {
         foreach (array_filter((array) $product->get_default_attributes(), 'strlen') as $key => $value) {
             if (0 === strpos($key, 'pa_')) {
                 $default[] = array('id' => wc_attribute_taxonomy_id_by_name($key), 'name' => $this->get_attribute_taxonomy_label($key), 'option' => $value);
             } else {
                 $default[] = array('id' => 0, 'name' => str_replace('pa_', '', $key), 'option' => $value);
             }
         }
     }
     return $default;
 }
WC_Product