add_action( 'woocommerce_review_order_before_order_total', 'custom_checkbox_checkout_field' );
function custom_checkbox_checkout_field() {
$value = WC()->session->get( 'add_a_product' );
$product_id = 6413;
$product = wc_get_product( $product_id );
$product_price = $product->get_price();
?>
<tr class="cart-addon-row">
<td class="product-name">
<?php
woocommerce_form_field(
'cb_add_product',
array(
'type' => 'checkbox',
'label' => ' <i class="fa fa-gift" style="color: #BD453F; margin-right: 5px;"></i>' . __( 'Confezione regalo' ) . ' - ' . wc_price( $product_price ),
'class' => array( 'form-row-wide', 'gift-box', 'checkbox' ), // Aggiunta della classe checkbox
),
$value == 'yes' ? true : false
);
?>
</td>
</tr>
<style>
.optional {
display: none;
}
.checkbox {
color: #BD453F;
font-weight: bold;
text-transform: none;
text-align: left;
}
</style>
<?php
}
// Codice jQuery per la richiesta Ajax
add_action( 'wp_footer', 'checkout_custom_jquery_script' );
function checkout_custom_jquery_script() {
// Solo nella pagina di checkout
if ( is_checkout() && ! is_wc_endpoint_url() ):
// Rimuovi le variabili di sessione personalizzate di WC al caricamento
if ( WC()->session->get( 'add_a_product' ) ) {
WC()->session->__unset( 'add_a_product' );
}
if ( WC()->session->get( 'product_added_key' ) ) {
WC()->session->__unset( 'product_added_key' );
}
// Codice jQuery per Ajax
?>
<script type="text/javascript">
jQuery( function($) {
if ( typeof wc_checkout_params === 'undefined' )
return false;
$('form.checkout').on( 'change', '#cb_add_product', function() {
var value = $(this).prop('checked') === true ? 'yes' : 'no';
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'add_a_product',
'add_a_product': value,
},
success: function (result) {
$('body').trigger('update_checkout');
console.log(result);
}
});
});
});
</script>
<?php
endif;
}
// Ricevitore PHP Ajax di WordPress
add_action( 'wp_ajax_add_a_product', 'checkout_ajax_add_a_product' );
add_action( 'wp_ajax_nopriv_add_a_product', 'checkout_ajax_add_a_product' );
function checkout_ajax_add_a_product() {
if ( isset( $_POST['add_a_product'] ) ) {
WC()->session->set( 'add_a_product', esc_attr( $_POST['add_a_product'] ) );
echo $_POST['add_a_product'];
}
die();
}
// Aggiungi o rimuovi il prodotto specifico
add_action( 'woocommerce_before_calculate_totals', 'adding_removing_specific_product' );
function adding_removing_specific_product( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// ID del prodotto specifico
$product_id = 6413;
if ( WC()->session->get( 'add_a_product' ) == 'yes' && ! WC()->session->get( 'product_added_key' ) ) {
$cart_item_key = $cart->add_to_cart( $product_id );
WC()->session->set( 'product_added_key', $cart_item_key );
} elseif ( WC()->session->get( 'add_a_product' ) == 'no' && WC()->session->get( 'product_added_key' ) ) {
$cart_item_key = WC()->session->get( 'product_added_key' );
$cart->remove_cart_item( $cart_item_key );
WC()->session->__unset( 'product_added_key' );
}
}
Copia il codice nel file functions.php del tuo tema child attivo o utilizza il plugin Code Snippets.