AI Verified

Name

WooCommerce Add Custom Checkout Field If Specific Product Is In Cart

About

This snippet will show a custom checkout field and save the data as order meta ONLY IF a specific product is in the cart.

Language

PHP

Rating

Voted: 1 by 1 user(s)

How to Setup Snippet

Simply copy the pre-coded solution to your active child theme’s functions.php or preferably the Code Snippets Plugin.

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.ecommercehints.com/woocommerce-add-custom-checkout-field-if-specific-product-is-in-cart

Codevault

ecommercehints

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

Website/ Profile URL:

https://www.ecommercehints.com/

History

Last modified:

06/07/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

WooCommerce Add Custom Checkout Field If Specific Product Is In Cart

 
                    
1/**
2 * Snippet Name: WooCommerce Add Custom Checkout Field If Specific Product Is In Cart
3 * Snippet Author: ecommercehints.com
4 */
5 
6// Create the custom checkout text field in the billing section of the checkout form
7add_action( 'woocommerce_after_checkout_billing_form', 'ecommercehints_conditional_checkout_field' );
8function ecommercehints_conditional_checkout_field($checkout) {
9 if(WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id(282))) { // The Product ID to trigger the new custom field
10 woocommerce_form_field( 'team_name', array(
11 'type' => 'text',
12 'class' => array('form-row-wide'),
13 'required' => 'true',
14 'label' => 'Pub Quiz Team Name',
15 ), $checkout->get_value('team_name') );
16 }
17}
18 
19// Show an error message of field is not populated
20add_action('woocommerce_checkout_process', 'ecommercehints_custom_checkout_field_validation');
21function ecommercehints_custom_checkout_field_validation() {
22 if(WC()->cart->find_product_in_cart(WC()->cart->generate_cart_id(282))) { // The Product ID to trigger the new custom field
23 if (empty( $_POST['team_name'] ) ) {
24 wc_add_notice( 'Please enter a team name for the pub quiz.', 'error' );
25 }
26 }
27}
28 
29// Save the custom field data as order meta
30add_action( 'woocommerce_checkout_update_order_meta', 'ecommercehints_save_custom_checkout_field' );
31function ecommercehints_save_custom_checkout_field( $order_id ){
32 if( !empty( $_POST['team_name'] ) ) {
33 update_post_meta( $order_id, 'team_name', sanitize_text_field( $_POST['team_name'] ) );
34 }
35}

1

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Disable WooCommerce Ajax Cart Fragments On Homepage

Added: 1 year ago

Last Updated: 1 year ago

WooCommerce

AI Verified

0

Add an Empty Cart Button to WooCommerce Cart

Added: 9 months ago

Last Updated: 9 months ago

By default, WooCommerce allows users to remove items from their cart individually. This tutorial demonstrates how to implement a single-button solution on the cart page, enabling users to clear the en...

WooCommerce

AI Verified

1

Add WooCommerce Product Type as Admin Column

Added: 6 months ago

Last Updated: 4 months ago

Make it easier to spot if a Product is a Simple or Variable Product.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WooCommerce

AI Verified

7

WooCommerce Show Number Of People Viewing Product

Added: 1 year ago

Last Updated: 1 month ago

We want to make this solution absolutely clear from the outset. This snippet does NOT generate the actual number of people viewing the product. It shows a randomly generated number in the range of two...

WooCommerce

AI Verified

5

WooCommerce Show Number Of Units Sold On Product Page

Added: 1 year ago

Last Updated: 2 months ago

Showing the number of product units sold on the single product page is a great way to introduce Fear Of Missing Out (FOMO). You can highlight how many products are sold to let customers know a product...

WooCommerce

AI Verified

3

WooCommerce Add Content Under The Proceed To Checkout Button On The Cart Page

Added: 1 year ago

Last Updated: 2 months ago

How many times have you bought something online and seen trust symbols near the proceed to checkout button on the cart page? “30 Day Money Back Guarantee” or “Secure Checkout” are popular examples. By...