Save custom checkbox value from WooCommerce cart page

53 views Asked by At

I have created a custom checkbox asking if the "order is a gift?" in a cart page, but unable to figure out how to save its value when a user clicks on "Proceed to checkout". The price won't be affected. I have created a separate PHP file for this. The checkbox is visible, but I have no idea what to do further. Also, this value (if checked) will be shown in admin side of order in.

The checkbox is visible in cart page. Here is my code:

add_action('woocommerce_cart_totals_after_order_total', 'add_gift_checkbox_to_cart');
function add_gift_checkbox_to_cart() {
    echo '<tr class="gift-checkbox"><th>' . __('Is this order a gift?', 'your-theme-textdomain') . '</th><td>';
    woocommerce_form_field('is_gift', array(
        'type' => 'checkbox',
        'class' => array('input-checkbox'),
        'label' => __('Yes, this order is a gift', 'your-theme-textdomain'),
    ), WC()->checkout->get_value('is_gift'));
    echo '</td></tr>';
}

Can anyone give me some insights on how to "save custom checkbox value from WooCommerce cart page".

1

There are 1 answers

0
LoicTheAztec On

The following code, will display your custom checkbox in cart and checkout pages. The selected option will be set as a session variable via Ajax, and saved when the order will be placed. It will be displayed in admin orders, customer orders and email notifications.

// Display a custom checkbox on cart and checkout
add_action('woocommerce_cart_totals_after_order_total', 'display_gift_order_checkbox');
add_action('woocommerce_review_order_after_order_total', 'display_gift_order_checkbox');
function display_gift_order_checkbox() {
    echo '<tr class="gift_order">
    <th>' . __('Is this order a gift?', 'woocommerce') . '</th>
    <td>';

    woocommerce_form_field('gift_order', array(
        'type' => 'checkbox',
        'class' => array('input-checkbox'),
        'label' => __('Yes, this order is a gift', 'woocommerce'),
    ), WC()->session->get('gift_order') === 'Yes' );

    echo '</td></tr>';
}

// Remove "(optional)" text from custom fields
add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_checkbox_field', 10, 2 );
function remove_optional_txt_from_checkbox_field( $field, $key ) {
    if( $key === 'gift_order' ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field    = str_replace( $optional, '', $field );
    } 
    return $field;
}

// AJAX REQUEST SENDER
add_action( 'wp_footer', 'gift_order_checkbox_script_js' );
function gift_order_checkbox_script_js() {
    if ( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ) :
    ?>
    <script>
    jQuery(function($){
        const eventToTrigger = '<?php echo is_cart() ? "added_to_cart" : "update_checkout"; ?>';
        $(document.body).on('change', 'input[name=gift_order]', function(){
            $.ajax({
                type: 'POST',
                url: woocommerce_params.ajax_url,
                data: {
                    'action':      'gift_order_checkbox',
                    'gift_order':   $(this).prop('checked') ? 'Yes' : 'No',
                },
                success: function (response) {
                    $(document.body).trigger(eventToTrigger);
                },
            });
        });
    });
    </script>
    <?php
    endif;
}

// Get Ajax request and saving checkbox value to a WC Session variable
add_action( 'wp_ajax_gift_order_checkbox', 'gift_order_checkbox_to_wc_session' );
add_action( 'wp_ajax_nopriv_gift_order_checkbox', 'gift_order_checkbox_to_wc_session' );
function gift_order_checkbox_to_wc_session() {
    if ( isset($_POST['gift_order']) && isset($_POST['gift_order']) ) {
        WC()->session->set('gift_order', esc_attr($_POST['gift_order']));
        wp_die(esc_attr($_POST['gift_order']));
    }
    wp_die('Something went wrong');
}

// Save checkbox selected value as order meta data
add_action( 'woocommerce_checkout_create_order', 'save_gift_order_session_variable_as_meta_data' );
function save_gift_order_session_variable_as_meta_data( $order ) {
    $order->add_meta_data( 'gift_order', WC()->session->get('gift_order') === 'Yes' ? 'Yes' : 'No', true );
}

// Remove WC Session variable related to the checkbox
add_action( 'woocommerce_checkout_order_created', 'remove_gift_order_session_variable' );
function remove_gift_order_session_variable() {
    WC()->session->__unset('gift_order');
}

// Display Gift order selected value in admin orders
add_action('woocommerce_admin_order_data_after_billing_address', 'admin_order_displays_gift_order_value');
function admin_order_displays_gift_order_value($order) {
    $value =  $order->get_meta('gift_order') ;
    echo '<p><strong>' . __("Is a gift order", "woocommerce") . '</strong>: ' . $value . '</p>';
}

// Display on customer orders and email notifications
add_filter( 'woocommerce_get_order_item_totals', 'display_delivery_on_order_item_totals', 10, 3 );
function display_delivery_on_order_item_totals( $total_rows, $order, $tax_display ){
    if( $gift_order = $order->get_meta( 'gift_order' ) ) {
        $new_total_rows = [];

        // Loop through order total rows
        foreach( $total_rows as $key => $values ) {
            $new_total_rows[$key] = $values;

            // Inserting before payment method
            if( $key === 'payment_method' ) {
                $new_total_rows['gift_order'] = array(
                    'label' => __("Is a gift order", "woocommerce") . ':',
                    'value' => $gift_order,
                );
            }
        }
        return $new_total_rows;
    }
    return $total_rows;
}

Code goes in functions.php file of your child theme (or in a plugin). Tested and works.


Screenshot from checkout page:

enter image description here


Screenshot from Customer order:

enter image description here