Default Attributes for WooCommerce

How to set default attribute for variable products automatically if only 1 option is in-stock?
In the plugin settings set the ‘Max variation for by-stock default attributes calculation’ option.

How to disable the plugin’s cache (suitable for testing its features)?

add_filter( 'daw_transients_enabled', '__return_false' );

How to set the plugin’s cache expiration time (default is 1 minute)?
Update the number of seconds (3600 seconds- suitable for production):

add_filter( 'daw_transient_expiration', 'daw_disable_transients' );
function daw_disable_transients() {
	return '3600';
}

How to apply the defaults on all the pages and not just the single product page?

add_filter( 'daw_defaults_enabled', '__return_true' );

How to exclude some attribute from the stock-based default attributes calculation?
Update the attribute id:

add_filter( 'daw_max_variations', 'daw_exclude_attribute', 10, 2 );
function daw_exclude_attribute( $max_variations, $attribute ) {
	$excluded_attribute = 25;
	return $excluded_attribute === $attribute['id'] ? 0 : $max_variations;
}

How to force in-stock default variation when no default attributes are set?

add_filter( 'daw_product_get_default_attributes', 'daw_product_force_default_attributes', 999, 2 );
function daw_product_force_default_attributes( $default_attributes, $product ) {
	if ( empty( $default_attributes ) ) {
		foreach ( $product->get_available_variations() as $variation ) {
			if ( $variation['is_in_stock'] || $variation['backorders_allowed'] ) {
				foreach ( $variation['attributes'] as $key => $value ) {
					$default_attributes[ str_replace( 'attribute_', '', $key ) ] = $value;
				}
				break;
			}
		}
	}
	return $default_attributes;
}

Leave a Reply

Your email address will not be published. Required fields are marked *