WooCommerce permits you to add your personal {custom} cart merchandise knowledge through the use of hooks offered by WooCommerce. Right here’s a step-by-step information on learn how to add {custom} cart merchandise knowledge in WooCommerce:
1. Create a {custom} plugin or use your theme’s features.php file
Create a {custom} plugin or use your theme’s features.php file so as to add your {custom} code. It’s really helpful to make use of a {custom} plugin to make sure your modifications usually are not misplaced throughout theme updates. In my case, we’ve created a perform.php file and we add all code inside it.
<?php /** * Plugin Identify:WooCommerce Customized Cart Merchandise Information * Description: How To Add Customized Cart Merchandise Information in WooCommerce. * Plugin URI: https://webkul.com/ * Model: 1.0.0 * Writer: Webkul * Writer URI: https://webkul.com/ * Textual content Area: webkul */
2. Add a {custom} area to the product web page
Be aware that we’re inserting our {custom} area utilizing an motion Woocommerce_before_add_to_cart_button. As you may guess from its identify, this motion begins simply earlier than the ‘Add to Cart’ button is displayed on the product web page.
/** * Add a {custom} enter area to the product web page. */ perform wk_add_text_field() { ?> <div class="custom-field-wrap" model="margin: 10px;"> <label for="custom-field"><?php esc_html_e( 'Quote', 'webkul' ); ?></label> <enter kind="textual content" identify='custom-field' id='custom-field' worth=''> </div> <?php } add_action( 'woocommerce_before_add_to_cart_button', 'wk_add_text_field' );
3. Validating the {custom} area knowledge
Validating {custom} area knowledge with the assistance of woocommerce_add_to_cart_validation
filter.
/** * Validate {custom} enter area worth */ perform wk_add_to_cart_validation( $handed, $product_id, $amount, $variation_id = null ) { if ( empty( $_POST['custom-field'] ) ) { $handed = false; wc_add_notice( __( 'Quote is a required area.', 'webkul' ), 'error' ); } return $handed; } add_filter( 'woocommerce_add_to_cart_validation', 'wk_add_to_cart_validation', 10, 4 );
4. Including the {custom} area knowledge in WooCommerce
We’re utilizing one other filter, Woocommerce_add_cart_item_data, so as to add {custom} cart merchandise knowledge.
/** * Add {custom} cart merchandise knowledge */ perform wk_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) { if ( isset( $_POST['custom-field'] ) ) { $cart_item_data['pr_field'] = sanitize_text_field( $_POST['custom-field'] ); } return $cart_item_data; } add_filter( 'woocommerce_add_cart_item_data', 'wk_add_cart_item_data', 10, 3 );
5. Show the {custom} WooCommerce metadata within the cart
To show {custom} knowledge within the cart with the assistance of the wocommerce_get_item_data filter.
/** * Show {custom} merchandise knowledge within the cart */ perform wk_get_item_data( $item_data, $cart_item_data ) { if ( isset( $cart_item_data['pr_field'] ) ) { $item_data[] = array( 'key' => __( 'Quote', 'webkul' ), 'worth' => wc_clean( $cart_item_data['pr_field'] ), ); } return $item_data; } add_filter( 'woocommerce_get_item_data', 'wk_get_item_data', 10, 2 );
6. Add the {custom} cart metadata to the WooCommerce order
We use an motion known as Woocommerce_checkout_create_order_line_item which permits us to replace the road merchandise when the order is saved and likewise be certain that the {custom} metadata is displayed on the order web page within the admin.
/** * Add {custom} meta to order */ perform wk_checkout_create_order_line_item( $merchandise, $cart_item_key, $values, $order ) { if ( isset( $values['pr_field'] ) ) { $item->add_meta_data( __( 'Quote', 'webkul' ), $values['pr_field'], true ); } } add_action( 'woocommerce_checkout_create_order_line_item', 'wk_checkout_create_order_line_item', 10, 4 );
Viewing {custom} metadata on the order evaluation web page
Viewing {custom} metadata within the order admin web page
Help
For any technical help, please increase a ticket or attain us by mail at [email protected]
Kindly go to the WooCommerce Addons web page to see our addons and may also discover our WooCommerce Improvement Companies.
Have a Nice Day Forward!
See you within the subsequent publish. Preserve studying 🙂