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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Default Attribute’ is closed to new replies.