-
-
Save FrancoStino/a791465839f24a15bbfde93db91af4d2 to your computer and use it in GitHub Desktop.
Disable purchase and add to cart specific product with checkbox into inventory setting product admin page and replace with required inquiry (Richiedi un preventivo) contact form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Disable purchase and add to cart specific product with checkbox into inventory setting product admin page and replace with required inquiry (Richiedi un preventivo) contact form | |
*/ | |
// Add checkbox | |
function action_woocommerce_product_options_inventory_product_data() { | |
// Checkbox | |
woocommerce_wp_checkbox( array( | |
'id' => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not) | |
'label' => __( 'Richiedi un preventivo', 'woocommerce' ), // Text in the editor label | |
'desc_tip' => false, // true or false, show description directly or as tooltip | |
'description' => __( 'Si', 'woocommerce' ) // Provide something useful here | |
) ); | |
} | |
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data', 10, 0 ); | |
// Save Field | |
function action_woocommerce_admin_process_product_object( $product ) { | |
// Isset, yes or no | |
$checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no'; | |
// Update meta | |
$product->update_meta_data( '_prevent_add_to_cart_button', $checkbox ); | |
} | |
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); | |
// Is_purchasable (simple) | |
function filter_woocommerce_is_purchasable( $purchasable, $product ) { | |
// Get meta | |
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' ); | |
// Compare | |
if ( $hide_add_to_cart_button == 'yes' ) { | |
$purchasable = false; | |
} | |
return $purchasable; | |
} | |
add_filter( 'woocommerce_is_purchasable', 'filter_woocommerce_is_purchasable', 10, 2 ); | |
// Is_purchasable (variable) | |
function filter_woocommerce_variation_is_purchasable( $purchasable, $product ) { | |
$hide_add_to_cart_button = get_post_meta( $product->get_parent_id(), '_prevent_add_to_cart_button', true ); | |
// Compare | |
if ( $hide_add_to_cart_button == 'yes' ) { | |
$purchasable = false; | |
} | |
return $purchasable; | |
} | |
add_filter( 'woocommerce_variation_is_purchasable', 'filter_woocommerce_variation_is_purchasable', 10, 2 ); | |
// Add request a quote button with form if request checkbox is true | |
function button_request_a_quote($purchasable){ | |
global $product; | |
// Compare | |
if ( ! $product->is_purchasable() ) { | |
echo '<button type="submit" id="trigger_cf" class="single_add_to_cart_button button alt">Richiedi un preventivo</button>'; | |
echo '<div id="product_inq" style="display:none">'; | |
echo do_shortcode('[contact-form-7 id="4542" title="Richiedi un preventivo"]'); | |
echo '</div>'; | |
wc_enqueue_js( " | |
$('#trigger_cf').on('click', function(){ | |
if ( $(this).text() == 'Richiedi un preventivo' ) { | |
$('#product_inq').css('display','block'); | |
$('#trigger_cf').html('Chiudi'); | |
} else { | |
$('#product_inq').hide(); | |
$('#trigger_cf').html('Richiedi un preventivo'); | |
} | |
}); | |
" ); | |
} | |
} | |
add_action('woocommerce_single_product_summary', 'button_request_a_quote', 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment