Add a Gift Box at Checkout

Add a checkbox on the checkout page so you can add a gift box to your purchase.

PHP
/**
 * 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_action('woocommerce_review_order_before_order_total', 'cb_custom_checkbox_checkout_field');

function cb_custom_checkbox_checkout_field() {
    $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
            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_action('wp_footer', 'cb_checkout_custom_jquery_script');

function cb_checkout_custom_jquery_script() {
    if (is_checkout() && !is_wc_endpoint_url()) {
        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;

                $('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': 'cb_add_a_product',
                            'add_a_product': value,
                        },
                        success: function(result) {
                            $('body').trigger('update_checkout');
                            console.log(result);
                        }
                    });
                });
            });
        </script>
        <?php
    }
}

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'])) {
        $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();
}

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

    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');
    }
}
?>

How To Implement This Solution?

Leave a Reply