//add custom checkbox for removal of add to cart button on product pages
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => '_custom_product',
'wrapper_class' => '',
'label' => __('No Add to Cart Button', 'woocommerce' ),
'description' => __( 'Product can not be added to cart for purchase online', 'woocommerce' )
)
);
echo '</div>';
}
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
// Checkbox
$woocommerce_custom_product_checkbox = isset( $_POST['_custom_product'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_custom_product', $woocommerce_custom_product_checkbox );
}
add_filter('woocommerce_is_purchasable', 'mmi_custom_is_purchasable', 10, 2);
function mmi_custom_is_purchasable( $is_purchasable, $object ) {
// get the product id first
$product_id = $object->get_id();
// get the product meta data
$is_custom = get_post_meta($product_id, '_custom_product', true);
if ($is_custom == "yes"){
return false;
}
else {
return true;
}
}
//remove add to cart and add contact form for non-purchasable products
add_action( 'woocommerce_single_product_summary', 'mmi_custom_product_cta', 10);
function mmi_custom_product_cta()
{
global $product;
// get the product id first
$product_id = $product->get_id();
// get the product meta data
$is_custom = get_post_meta($product_id, '_custom_product', true);
// Show the Form if product is Custom
if ($is_custom == "yes"){
get_template_part( 'template-parts/content', 'noaddtocartform' );
}
}