How to add a checkbox to a WooCommerce product page

WooCommerce is a popular WordPress plugin that allows users to build powerful online stores. One of the common customizations store owners often need is the ability to add extra options to product pages, such as checkboxes. These checkboxes can be used for a variety of purposes, including collecting additional information from customers, offering product variations, or enabling customers to opt-in for additional services.

In this article, we’ll walk you through the steps to add a checkbox to a WooCommerce product page, including how to create custom fields, customize the product page template, and handle checkbox data in the cart.

Why Add a Checkbox to a WooCommerce Product Page?

Before diving into the technical aspects, let’s quickly discuss why you might want to add a checkbox to a WooCommerce product page. Some common use cases include:

  • Opt-in for additional services: You may want customers to opt-in for gift wrapping, warranties, or customization options.
  • Product customization: If you offer customizable products, a checkbox could allow customers to choose options like adding a logo or selecting a different color.
  • Additional information collection: Use a checkbox to collect information from the customer, such as agreeing to terms and conditions, or confirming they’ve reviewed a return policy.

Steps to Add a Checkbox to a WooCommerce Product Page

Here are the steps to add a simple checkbox to a WooCommerce product page:

1. Use a Plugin to Add Custom Fields (Optional)

For store owners who are not comfortable with coding, the easiest way to add a checkbox is by using a plugin. There are several plugins available that allow you to add custom fields to product pages.

  • Product Add-Ons for WooCommerce: This is a popular plugin that allows you to add extra fields (including checkboxes) to product pages. It’s user-friendly and requires no coding.
  • WooCommerce Custom Fields: Another great plugin that can help you add custom fields like checkboxes, text fields, and more.

For this tutorial, we’ll demonstrate how to add a checkbox using code, but if you prefer a plugin-based solution, you can skip to this step and install the plugin of your choice.

2. Add the Checkbox Using Code

If you want more control and are comfortable with code, you can manually add a checkbox by using WooCommerce hooks.

a. Add the Checkbox to the Product Page

The following code snippet will add a checkbox on the product page. You can add this code to your theme’s functions.php file or create a custom plugin to ensure it stays intact even when you update your theme.

// Add a checkbox to the product page
add_action( 'woocommerce_before_add_to_cart_button', 'add_checkbox_to_product_page' );

function add_checkbox_to_product_page() {
    echo '<div class="product-checkbox">
            <label for="extra_service_checkbox">
                <input type="checkbox" id="extra_service_checkbox" name="extra_service_checkbox" /> Add Gift Wrapping
            </label>
          </div>';
}

In this example, the checkbox is for the option of adding gift wrapping to the product.

b. Save the Checkbox Data

Next, you need to save the checkbox data when a customer adds the product to the cart. To do this, use the following code snippet to capture the checkbox value.

// Save the checkbox data when the product is added to the cart
add_filter( 'woocommerce_add_cart_item_data', 'save_checkbox_data_in_cart', 10, 3 );

function save_checkbox_data_in_cart( $cart_item_data, $product_id, $variation_id ) {
    if ( isset( $_POST['extra_service_checkbox'] ) ) {
        $cart_item_data['extra_service_checkbox'] = 'yes'; // Save 'yes' if the checkbox is checked
    }
    return $cart_item_data;
}

This code saves the checkbox data to the cart item when the customer adds the product to the cart.

c. Display the Checkbox Option in the Cart

Now that the checkbox value is stored in the cart, you need to display it to the customer in the cart and checkout pages. You can use the following code to show the checkbox option in the cart.

// Display the checkbox data in the cart
add_filter( 'woocommerce_get_item_data', 'display_checkbox_in_cart', 10, 2 );

function display_checkbox_in_cart( $item_data, $cart_item ) {
    if ( isset( $cart_item['extra_service_checkbox'] ) && $cart_item['extra_service_checkbox'] == 'yes' ) {
        $item_data[] = array(
            'name'  => 'Gift Wrapping',
            'value' => 'Yes'
        );
    }
    return $item_data;
}

This will display “Gift Wrapping: Yes” in the cart next to the item.

d. Process the Checkbox Option at Checkout

If you need to process the checkbox data during the checkout, you can do so by using the following code to include the checkbox option in the order details.

// Add checkbox data to the order
add_action( 'woocommerce_checkout_create_order_line_item', 'add_checkbox_data_to_order', 10, 4 );

function add_checkbox_data_to_order( $item, $cart_item_key, $values, $order ) {
    if ( isset( $values['extra_service_checkbox'] ) && $values['extra_service_checkbox'] == 'yes' ) {
        $item->add_meta_data( 'Gift Wrapping', 'Yes' );
    }
}

This code adds the checkbox data to the order when the customer proceeds to checkout.

3. Styling the Checkbox

You can also customize the appearance of the checkbox on the product page by adding some CSS. Here’s an example of how to style the checkbox:

/* Style for the checkbox */
.product-checkbox {
    margin-top: 10px;
}

.product-checkbox label {
    font-size: 16px;
}

.product-checkbox input[type="checkbox"] {
    margin-right: 10px;
}

Add this CSS to your theme’s style.css file or use the WordPress Customizer (Appearance > Customize > Additional CSS).

4. Testing the Checkbox

Once you’ve added the code, it’s important to test that everything works as expected. Here are the steps you should follow:

  1. Visit the product page and make sure the checkbox is visible.
  2. Select the checkbox and add the product to the cart.
  3. Go to the cart page and verify that the checkbox option is displayed correctly.
  4. Proceed to checkout and confirm that the checkbox data appears in the order.

Conclusion

Adding a checkbox to a WooCommerce product page can enhance the shopping experience by providing customers with more options and allowing you to collect important information. Whether you’re adding a simple opt-in for services like gift wrapping or offering product customization, the process can be straightforward using code or plugins. If you’re familiar with coding, adding the checkbox manually will give you more control over the design and functionality, while plugins provide an easier, no-code solution for those who prefer a more user-friendly approach.

By following the steps outlined above, you can quickly and effectively add checkboxes to your WooCommerce product pages, making your store more versatile and customizable for your customers.