user14580373
user14580373

Reputation:

How to add checkbox in cart, save and display checkbox value in edit order page?

I am looking for a hook to display on the cart page a check box that mentions is it a gift?. I found woocommerce_after_checkout_billing_form but obviously, it shows on the billing and address page, is it any solution?

Update: Add after table of current product in the basket.

Upvotes: 2

Views: 595

Answers (1)

Bhautik
Bhautik

Reputation: 11282

You can use woocommerce_after_cart_table hook. check the below code. code goes in your active theme functions.php file

function show_checkbox_for_gift(){
    ?>
    <label><input type="checkbox" name="gift" value="yes">is it a gift?</label>
    <?php
}
add_action( 'woocommerce_after_cart_table', 'show_checkbox_for_gift', 20, 1 );

Tested and works.

enter image description here

Updated (related to OP comment).

// The jQuery Ajax request
add_action( 'wp_footer', 'checkout_custom_jquery_script' );
function checkout_custom_jquery_script() {
    // Only cart page
    if( is_cart() ){ ?>
        <script type="text/javascript">
        (function($){
            $(document).on( 'change', 'input[name="gift"]', function(){
                var value = $(this).prop('checked') === true ? 'yes' : 'no';
                $.ajax({
                    type: 'POST',
                    url: '<?php echo admin_url('admin-ajax.php'); ?>',
                    dataType: 'json',
                    data: {
                        'action': 'set_cart_as_gift',
                        'gift': value,
                    },
                    success: function (result) {
                        console.log(result);
                    }
                });
            });
        })(jQuery);
        </script>
    <?php
    }
}

add_action( 'wp_ajax_set_cart_as_gift', 'set_cart_as_gift' );
add_action( 'wp_ajax_nopriv_set_cart_as_gift', 'set_cart_as_gift' );
function set_cart_as_gift() {
    if ( isset($_POST['gift'] ) ){
        WC()->session->set( 'gift', esc_attr( $_POST['gift'] ) );
        wp_send_json_success( WC()->session->get('gift') );
    }
    wp_send_json_error();
}

add_action('woocommerce_checkout_create_order', 'update_is_it_a_gift_in_meta_before_checkout_create_order', 20, 2);
function update_is_it_a_gift_in_meta_before_checkout_create_order( $order, $data ) {
    if( WC()->session->get('gift') == 'yes' ){
        $order->update_meta_data( 'is_it_a_gift', 'yes' );
    }else{
        $order->update_meta_data( 'is_it_a_gift', 'no' );
    }
}

// display the extra data in the order admin panel
function display_is_it_a_gift_order_edit_in_admin( $order ){  ?>
    <div class="form-field form-field-wide">
        <h4><?php _e( 'Extra Details:' ); ?></h4>
        <?php 
            $label = ( get_post_meta( $order->id, 'is_it_a_gift', true ) == 'yes' ) ? ' Put in gift box.' : ' Put in standard box.';
            echo '<p><strong>' . __( 'is it a gift?' ) . ':</strong>' . $label . '</p>';
        ?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'display_is_it_a_gift_order_edit_in_admin' );

add_action('woocommerce_thankyou', 'reset_gift_session', 10, 1);
function reset_gift_session( $order_id ) {
    WC()->session->__unset( 'gift' );
}

Tested and works.

enter image description here

Upvotes: 1

Related Questions