We want to prevent add to cart for certain upcoming products.
We want to have a checkbox to select the specific product on which we want to prevent add to cart. We have right now the checkbox and save code.
I also found this: Remove add cart button in Woocommerce for a specific product category and https://wisdmlabs.com/blog/the-right-way-to-hide-add-to-cart-button-in-woocommerce/
I'm not sure, what is the best way to prevent add to cart for specific products. Does anyone have a suggestion what the best way would be?
// Add new checkbox to product edit page (General tab)
add_action( 'woocommerce_product_options_general_product_data', 'upcoming_checkbox_to_products' );
function upcoming_checkbox_to_products() {
woocommerce_wp_checkbox( array(
'id' => 'custom_upcoming',
'class' => '',
'label' => 'Prevent add to cart'
)
);
}
// -----------------------------------------
// Save checkbox via custom field
add_action( 'save_post', 'save_upcoming_checkbox_to_post_meta' );
function save_upcoming_checkbox_to_post_meta( $product_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( isset( $_POST['custom_upcoming'] ) ) {
update_post_meta( $product_id, 'custom_upcoming', $_POST['custom_upcoming'] );
} else delete_post_meta( $product_id, 'custom_upcoming' );
}
// -----------------------------------------
// Prevent add to cart