set_default_attributes() public method

Set default attributes.
Since: 2.7.0
public set_default_attributes ( array $default_attributes )
$default_attributes array List of default attributes.
 /**
  * Save default attributes.
  *
  * @since 2.7.0
  *
  * @param WC_Product      $product Product instance.
  * @param WP_REST_Request $request Request data.
  * @return WC_Product
  */
 protected function save_default_attributes($product, $request)
 {
     if (isset($request['default_attributes']) && is_array($request['default_attributes'])) {
         $attributes = $product->get_variation_attributes();
         $default_attributes = array();
         foreach ($request['default_attributes'] as $attribute) {
             $attribute_id = 0;
             $attribute_name = '';
             // Check ID for global attributes or name for product attributes.
             if (!empty($attribute['id'])) {
                 $attribute_id = absint($attribute['id']);
                 $attribute_name = wc_attribute_taxonomy_name_by_id($attribute_id);
             } elseif (!empty($attribute['name'])) {
                 $attribute_name = sanitize_title($attribute['name']);
             }
             if (!$attribute_id && !$attribute_name) {
                 continue;
             }
             if (isset($attributes[$attribute_name])) {
                 $_attribute = $attributes[$attribute_name];
                 if ($_attribute['is_variation']) {
                     $value = isset($attribute['option']) ? wc_clean(stripslashes($attribute['option'])) : '';
                     if (!empty($_attribute['is_taxonomy'])) {
                         // If dealing with a taxonomy, we need to get the slug from the name posted to the API.
                         $term = get_term_by('name', $value, $attribute_name);
                         if ($term && !is_wp_error($term)) {
                             $value = $term->slug;
                         } else {
                             $value = sanitize_title($value);
                         }
                     }
                     if ($value) {
                         $default_attributes[$attribute_name] = $value;
                     }
                 }
             }
         }
         $product->set_default_attributes($default_attributes);
     }
     return $product;
 }
 /**
  * Save default attributes.
  *
  * @since 2.7.0
  * @param WC_Product $product
  * @param array $request
  * @return WC_Product
  */
 protected function save_default_attributes($product, $request)
 {
     // Update default attributes options setting.
     if (isset($request['default_attribute'])) {
         $request['default_attributes'] = $request['default_attribute'];
     }
     if (isset($request['default_attributes']) && is_array($request['default_attributes'])) {
         $default_attributes = array();
         foreach ($request['default_attributes'] as $default_attr_key => $default_attr) {
             if (!isset($default_attr['name'])) {
                 continue;
             }
             $taxonomy = sanitize_title($default_attr['name']);
             if (isset($default_attr['slug'])) {
                 $taxonomy = $this->get_attribute_taxonomy_by_slug($default_attr['slug']);
             }
             if (isset($attributes[$taxonomy])) {
                 $_attribute = $attributes[$taxonomy];
                 if ($_attribute['is_variation']) {
                     $value = '';
                     if (isset($default_attr['option'])) {
                         if ($_attribute['is_taxonomy']) {
                             // Don't use wc_clean as it destroys sanitized characters.
                             $value = sanitize_title(trim(stripslashes($default_attr['option'])));
                         } else {
                             $value = wc_clean(trim(stripslashes($default_attr['option'])));
                         }
                     }
                     if ($value) {
                         $default_attributes[$taxonomy] = $value;
                     }
                 }
             }
         }
         $product->set_default_attributes($default_attributes);
     }
     return $product;
 }