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".
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.
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Screenshot from checkout page:
Screenshot from Customer order: