How to Add a Customizable Field to a WooCommerce Product

Posted On: January 16, 2018

If you are looking to add custom field at backorder products, use the below functions that will Add a Customizable Field to a WooCommerce Product. For example you add notes field in back-order product. If notes will blank than product will not allow adding into cart. After checkout you will find submitted notes in woo commerce order panel.

Below function used to check back-order products:

// backorder product data

add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_option_for_back_order_products_change_backorder_button', 10, 2 );
function custom_option_for_back_order_products_change_backorder_button( $text, $product ){
	if ( $product->is_on_backorder( 1 ) ) {
		$text = __( 'Pre Order', 'woocommerce' );
		
		// hooks for check only backorder product
		add_action( 'woocommerce_after_add_to_cart_button', 'custom_option_for_back_order_products', 9 );

		// hooks for check only backorder product end hook 
		
	}
	return $text;
}

Hooks and Filter for custom field add on product page:

add_filter( 'woocommerce_add_to_cart_validation', 'back_order_products_add_to_cart_validation', 10, 3 );
add_filter( 'woocommerce_add_cart_item_data', 'back_order_products_add_cart_item_data', 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session', 'back_order_products_get_cart_item_from_session', 20, 2 );
add_action( 'woocommerce_add_order_item_meta', 'back_order_products_add_order_item_meta', 10, 2 );
add_filter( 'woocommerce_get_item_data', 'back_order_products_get_item_data', 10, 2 );
add_filter( 'woocommerce_order_item_product', 'back_order_products_order_item_product', 10, 2 );
add_filter('woocommerce_email_order_meta_fields', 'back_order_products_email_order_meta_fields');
add_filter( 'woocommerce_order_again_cart_item_data', 'back_order_products_order_again_cart_item_data', 10, 3 );

Below function used to show custom field on single product and validate those fields:

function custom_option_for_back_order_products(){
//Display input on single product page
	
    $value = isset( $_POST['_custom_option_for_back_order_products'] ) ? sanitize_text_field( $_POST['_custom_option_for_back_order_products'] ) : '';
    printf( '<label>%s</label><textarea name="_custom_option_for_back_order_products" >%s</textarea>', __( 'Enter Your Notes:'), esc_attr( $value ) );
}
function back_order_products_add_to_cart_validation($passed, $product_id, $qty){
//Validate when adding to cart
 
    if( isset( $_POST['_custom_option_for_back_order_products'] ) && sanitize_text_field( $_POST['_custom_option_for_back_order_products'] ) == '' ){
        $product = wc_get_product( $product_id );
        wc_add_notice( sprintf( __( '%s cannot be added to the cart until you enter some notes.' ), $product->get_title() ), 'error' );
        return false;
    }
 
    return $passed;
 
}

Below function add custom field data into current cart item:

function back_order_products_add_cart_item_data( $cart_item, $product_id ){
//Add custom data to the cart item
 
    if( isset( $_POST['_custom_option_for_back_order_products'] ) ) {
        $cart_item['custom_option_for_back_order_products'] = sanitize_text_field( $_POST['_custom_option_for_back_order_products'] );
    }
 
    return $cart_item;
 
}

Below function used for add custom field into cart session:

function back_order_products_get_cart_item_from_session( $cart_item, $values ) {
//Load cart data from session
 
    if ( isset( $values['custom_option_for_back_order_products'] ) ){
        $cart_item['custom_option_for_back_order_products'] = $values['custom_option_for_back_order_products'];
    }
 
    return $cart_item;
 
}

Below function add custom fields into cart item:

function back_order_products_add_order_item_meta( $item_id, $values ) {
//Add meta to order item
 
    if ( ! empty( $values['custom_option_for_back_order_products'] ) ) {
        woocommerce_add_order_item_meta( $item_id, 'custom_option_for_back_order_products', $values['custom_option_for_back_order_products'] );           
    }
}

Below function used for show custom fields into cart:

function back_order_products_get_item_data( $other_data, $cart_item ) {
// Get item data to display in cart
 
    if ( isset( $cart_item['custom_option_for_back_order_products'] ) ){
 
        $other_data[] = array(
            'name' => __('Enter Your Notes:'),
            'value' => sanitize_text_field( $cart_item['custom_option_for_back_order_products'] )
        );
 
    }
 
    return $other_data;
 
}

Below function used for overview of custom fields:

function back_order_products_order_item_product( $cart_item, $order_item ){
//Show custom field in order overview
 
    if( isset( $order_item['custom_option_for_back_order_products'] ) ){
        $cart_item_meta['custom_option_for_back_order_products'] = $order_item['custom_option_for_back_order_products'];
    }
 
    return $cart_item;
 
}

Below function used to add custom field in woocommerce order email:

function back_order_products_email_order_meta_fields( $fields ) { 
//Add the field to order emails 

    $fields['custom_field'] = __( 'Enter Your Notes:'); 
    return $fields; 
} 

Below function used when re-order the same product from customer id:

function back_order_products_order_again_cart_item_data( $cart_item, $order_item, $order ){
	
//Order Again
 
    if( isset( $order_item['custom_option_for_back_order_products'] ) ){
        $cart_item_meta['custom_option_for_back_order_products'] = $order_item['custom_option_for_back_order_products'];
    }
 
    return $cart_item;
 
}

// backorders product end

If you have any question related to payment gateways, you can contact us or ask your question with us. For more tutorials, you can visit our other articles or visit questions page.

Also Read :   WooCommerce Integration Services

You can join us on twitter or subscribe at YouTube for more web design & development tutorials.

Related Post

X
Subscribe to our newsletter
Subscribe to our newsletter today to receive updates on the latest news and offers.
100% Privacy. No spam guaranteed
webe tutorial
Menu