/**
* Snippet Name: Add a Gift Box at Checkout
* Snippet Author: coding-bunny.com
* Description: Add a checkbox on the checkout page so you can add a gift box to your purchase.
*/
<?php
// Add a custom checkbox to the checkout page before the order total
add_action('woocommerce_review_order_before_order_total', 'cb_custom_checkbox_checkout_field');
function cb_custom_checkbox_checkout_field() {
// Get session value and product information
$value = WC()->session->get('cb_add_a_product');
$product_id = 6413; // Specific product ID
$product = wc_get_product($product_id);
$product_price = $product->get_price();
?>
<tr class="cart-addon-row">
<td class="product-name">
<?php
// Render the custom checkbox
woocommerce_form_field(
'cb_add_product',
array(
'type' => 'checkbox',
'label' => '<i class="fa fa-gift" style="color: #BD453F; margin-right: 5px;"></i>' .
__('Gift Wrapping') . ' - ' . wc_price($product_price),
'class' => array('form-row-wide', 'gift-box', '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
}
// Add jQuery for Ajax handling on the checkout page
add_action('wp_footer', 'cb_checkout_custom_jquery_script');
function cb_checkout_custom_jquery_script() {
// Ensure the script runs only on the checkout page
if (is_checkout() && !is_wc_endpoint_url()) {
// Clear session variables when the page loads
if (WC()->session->get('cb_add_a_product')) {
WC()->session->__unset('cb_add_a_product');
}
if (WC()->session->get('cb_product_added_key')) {
WC()->session->__unset('cb_product_added_key');
}
?>
<script type="text/javascript">
jQuery(function($) {
if (typeof wc_checkout_params === 'undefined') return false;
// Handle checkbox change event
$('form.checkout').on('change', '#cb_add_product', function() {
var value = $(this).prop('checked') === true ? 'yes' : 'no';
// Send Ajax request
$.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: {
'action': 'cb_add_a_product',
'add_a_product': value,
},
success: function(result) {
$('body').trigger('update_checkout'); // Refresh checkout totals
console.log(result);
}
});
});
});
</script>
<?php
}
}
// WordPress Ajax handler for updating the session
add_action('wp_ajax_cb_add_a_product', 'cb_checkout_ajax_add_a_product');
add_action('wp_ajax_nopriv_cb_add_a_product', 'cb_checkout_ajax_add_a_product');
function cb_checkout_ajax_add_a_product() {
if (isset($_POST['add_a_product'])) {
// Sanitize the input and store it in the session
$add_a_product = sanitize_text_field($_POST['add_a_product']);
WC()->session->set('cb_add_a_product', $add_a_product);
echo esc_html($add_a_product);
}
wp_die(); // Properly terminate the Ajax request
}
// Add or remove the specific product based on the checkbox value
add_action('woocommerce_before_calculate_totals', 'cb_adding_removing_specific_product');
function cb_adding_removing_specific_product($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
if (did_action('woocommerce_before_calculate_totals') >= 2) return;
$product_id = 6413; // Specific product ID
// Check session value and modify the cart accordingly
if (WC()->session->get('cb_add_a_product') === 'yes' && !WC()->session->get('cb_product_added_key')) {
$cart_item_key = $cart->add_to_cart($product_id);
WC()->session->set('cb_product_added_key', $cart_item_key);
} elseif (WC()->session->get('cb_add_a_product') === 'no' && WC()->session->get('cb_product_added_key')) {
$cart_item_key = WC()->session->get('cb_product_added_key');
$cart->remove_cart_item($cart_item_key);
WC()->session->__unset('cb_product_added_key');
}
}
?>
Add a Gift Box at Checkout
Add a checkbox on the checkout page so you can add a gift box to your purchase.