Customize Autoship Product Frequency by Product Categories
Note: Backwards compatibility supported for legacy custom frequency options display in 1.2.47 and forward.
Customize Autoship Product Frequency by Product Categories
In the example below, a developer sets their product frequency by the WooCommerce Product Category (Monthly Products, Weekly Products, Daily Products). By using the product frequencies the user is able to create different frequency options based on product attributes.
Important: Always test customizations to your site on a staging environment before making changes to your live / production site. If you are not experienced with testing changes on a staging site, here is a good article to review.
/** * Filter the available frequency options for a product based on cat * * @param array $default_options The current drop down options. * @param int $product_id The Simple or variation product id. * @return array the filtered frequency options. */ function xx_filter_frequency_options_by_category( $default_options, $product_id ){ // Does this product have the Monthly Products category assigned? if ( has_term( 'Monthly Products', 'product_cat', $product_id ) ){ $default_options = array( array( 'frequency_type' => 'Months', 'frequency' => 1, 'display_name' => 'Every Month' ), array( 'frequency_type' => 'Months', 'frequency' => 6, 'display_name' => 'Every 6 Months' ), array( 'frequency_type' => 'Months', 'frequency' => 12, 'display_name' => 'Every 12 Months' ) ); // Does this product have the Weekly Products category assigned? } else if ( has_term( 'Weekly Products', 'product_cat', $product_id ) ){ $default_options = array( array( 'frequency_type' => 'Weeks', 'frequency' => 1, 'display_name' => 'Every Week' ), array( 'frequency_type' => 'Weeks', 'frequency' => 2, 'display_name' => 'Every 2 Weeks' ), array( 'frequency_type' => 'Weeks', 'frequency' => 3, 'display_name' => 'Every 3 Weeks' ) ); // Does this product have the Daily Products category assigned? } else if ( has_term( 'Daily Products', 'product_cat', $product_id ) ){ $default_options = array( array( 'frequency_type' => 'Days', 'frequency' => 1, 'display_name' => 'Every Day' ), array( 'frequency_type' => 'Days', 'frequency' => 2, 'display_name' => 'Every Other Day' ) ); } return $default_options; } add_filter( 'autoship-product-frequency-options', 'xx_filter_frequency_options_by_category', 10, 2 );