Connect with us

Blog

How to Add a Customizable Field to a WooCommerce Product

mm

Published

on

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 :   What Can You Do About Slow Loading Website?

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

Hi, Thank you for reading my article. I am Victor, a professional blogger from Jaipur, India.I started webetutorial for blogging & sharing solution for developer questions, and now it’s empowering globally by helping them to make money.I am writing about starting & managing a blogs, WordPress, Magento, Social Media, SEO, Marketing, making money online, Investment, Finance, Gadgets, Fitness and more.

Continue Reading
Advertisement
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

12 + 18 =

Blog

How to Reduce DNS Lookups and Speed Them Up

mm

Published

on

How to Reduce DNS Lookups and Speed Them Up

There are many optimization steps to reduce the server response time. Some time we neglect the some important thing which can reduce more response time. Here we are going to show you some recommendations on how to reduce DNS lookups and speed them up to increase the performance of your WordPress site.

What is DNS Lookup?

DNS is a domain name system which is the backbone of the internet. DNS query is executed by ISP to request the nameservers with associated domain.

After browsing a website, DNS will look up for domain which is requested.

Use a Fast DNS Provider

To reduce dns lookup choose hosting those are faster. The free DNS provided by GoDaddy but that is very slow and Amazon, Cloudflare and Dyn are large dns providers.

Change TTL Values to acquire Advantage of DNS Cache

DNS Cache lookups happening on every single page load when first time requests. This cache works similarly to your WordPress site cache and served until it expires.

Also Read :   Porsche Design's Desktop Drive Shares Power With 12-inch MacBook

TTL values can be changed and depend on your DNS cache times. The TTL have following values: 300 seconds: 5 minutes, 1800 seconds: 30 minutes, 3600 seconds: 1 hour, 43200 seconds: 12 hours, 86400 seconds: 24 hours

Reduce Number of Domains

To reduce DNS lookups simply get rid of requests that query different hostnames. Run your WordPress website through a tool like Pingdom ot Gtmatrix which determine your website each really necessary request.

Use Alternative Services from Faster DNS

If your server provider much faster DNS then you need to use other alternative DNS which is faster than your DNS provider. For example if your DNS lookup in a 2 sec and other DNS provider provide 1 sec than your DNS provide is slow and you need to find out other faster DNS provider.

Move and Host Resources on a CDN

One of the easiest ways to reduce DNS lookups is use CDN which lookup your content fast and reduce load time. CDN does not reduce the request but it increases the requests response faster than normal requests.

Also Read :   WooCommerce Development Company

Defer Loading of JavaScript

Defer the loading of JavaScript prevent things from loading until after the article has completely loaded. This won’t decrease the number of DNS lookups but it will stop them from being called immediately to speed up the user experience. To defer javascript you can use Autoptimize or other free or paid plugin.

To get more news on wordpress or offer from us subscribe or join us.

Continue Reading

Subscribe to our newsletter

Enter your email address to subscribe to blog and receive notifications of new articles by email.
100% Privacy. No spam guaranteed

Trending