Product Page: Default to Autoship & Hide One-Time Purchase Option

In this example, a user hides the one-time purchase option on the product page and cart for a specific product id and defaults the radio option for that product to Autoship. See how only the Autoship option displays and is selected by default in the image below?

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.

Example

/*
* Output CSS to hide the One Time Purchase radio option
* @param WC_Product $product The WC Product
*/

function xx_hide_standard_purchase( $product ){

 // Only hide the options for product id 2222
 if ( 2846 != $product->get_id() )
 return;

 // Note to hide all radio options remove the ':first-of-type' rule.
 ?><style> .autoship-schedule-options > .autoship-type:first-of-type { display: none; } </style><?php

}

/**
* Output CSS to hide the One Time Purchase radio option
*
* @param array $cart_item The current cart item data.
* @param string $cart_item_key The current cart item key
*
*/
function xx_hide_standard_purchase_in_cart( $cart_item, $cart_item_key ) {
 $product = wc_get_product( $cart_item['product_id'] );
 xx_hide_standard_purchase( $product );
}

/**
* Default to Autoship option for a specific product
*
* @param string $default The default option
* @param WC_Product $product The WC Product
*
*/
function xx_default_autoship_option( $default, $product ) {
 return 2846 == $product->get_id() ? 'yes' : $default;
}
add_filter( 'autoship_before_schedule_options', 'xx_hide_standard_purchase', 10, 1 );
add_filter( 'autoship_before_schedule_options_variable', 'xx_hide_standard_purchase', 10, 1 );
add_action( 'woocommerce_after_cart_item_name', 'xx_hide_standard_purchase_in_cart', 10, 2 );
add_filter( 'autoship_default_product_schedule_options_choice_value', 'xx_default_autoship_option', 10, 2 );

Result

Still need help? Contact Us Contact Us