• Resolved loopforever

    (@loopforever)


    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)
  • Hi @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 or the #developers channel of the WooCommerce 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.

    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.