Title: Default Attribute
Last modified: December 17, 2021

---

# Default Attribute

 *  Resolved [loopforever](https://wordpress.org/support/users/loopforever/)
 * (@loopforever)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/default-attribute/)
 * Hi,
    I want to choose a random default attribute during the bulk product upload
   phase or in normal situations. There is such a resource, but it does not seem
   to be very efficient. Because it does a database query every time a page loads.
   Is there any other way to this?
 *     ```
       add_action('woocommerce_before_single_product_summary', 'quadlayers_product_default_attributes');
       function quadlayers_product_default_attributes() {
             global $product;
             if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) {
               $new_defaults = array();
               $product_attributes = $product->get_attributes();
               if (count($product_attributes)) {
                 foreach ($product_attributes as $key => $attributes) {
                   $values ​​= explode(',', $product->get_attribute($key));
                   if (isset($values[0]) && !isset($default_attributes[$key])) {
                     $new_defaults[$key] = sanitize_key($values[0]);
                   }
                 }
                 update_post_meta($product->get_id(), '_default_attributes', $new_defaults);
               }
             }
           }
       ```
   
 * Also, for products with variations, is there a way to initialize the global Attribute%
   d to 1 for each row during the batch product upload phase?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Margaret S. woo-hc](https://wordpress.org/support/users/margaretwporg/)
 * (@margaretwporg)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/default-attribute/#post-15173269)
 * Hi [@loopforever](https://wordpress.org/support/users/loopforever/)
 * This is a fairly complex development topic. I’m going to leave it open for a 
   bit to see if anyone is able to chime in to help you out.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there, as well.
 * This specific forum is more focused on the default WooCommerce core features.
 *  [Margaret S. woo-hc](https://wordpress.org/support/users/margaretwporg/)
 * (@margaretwporg)
 * [4 years, 5 months ago](https://wordpress.org/support/topic/default-attribute/#post-15193454)
 * We’ve not seen any activity on this thread for a while, so I’m marking this thread
   as resolved. If you have further questions, please feel free to open a new topic.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Default Attribute’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [Margaret S. woo-hc](https://wordpress.org/support/users/margaretwporg/)
 * Last activity: [4 years, 5 months ago](https://wordpress.org/support/topic/default-attribute/#post-15193454)
 * Status: resolved