WooCommerce is the most Widely used and well-known e-commerce plugin for WordPress.WooCommerce creates the products custom post type and sometimes you want own custom fields in Product type as per your site requirement.
WordPress provides plugin using which you can modify/create own custom fields in Admin panel and display on the front too.But today I am going to explain how to create custom fields into product data section by dynamically customizing WooCommerce using Action Hooks provided in Woocommerce plugin.
Let’s start step by step. Here I will explain for checkbox field. You can use same code for any field like Text, Textarea, Dropdown:
First of all, we will write action hook which will be used to display newly created field into the admin panel of product post type.The following code is used for same
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | // Display Fields using WooCommerce Action Hook add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_general_product_data_custom_field' ); global $woocommerce, $post; woocommerce_wp_checkbox( array( 'id' => 'product_checkbox', 'wrapper_class' => 'checkbox_class', 'label' => __('Checkbox for Product', 'woocommerce' ), 'description' => __( 'Description about checkbox', 'woocommerce' ) ) ); echo ''; } |
Next, we will see how we can save newly created field information into WordPress database by using woo commerce action hook.
1 2 3 4 5 6 7 8 | // Save Fields using WooCommerce Action Hook add_action( 'woocommerce_process_product_meta', 'woocommerce_process_product_meta_fields_save' ); function woocommerce_process_product_meta_fields_save( $post_id ){ $woo_checkbox = isset( $_POST['product_checkbox'] ) ? 'yes' : 'no'; update_post_meta( $post_id, 'product_checkbox', $woo_checkbox ); } |
That’s it. You are done with adding custom fields into product page and save the value of that field into the database.Now, I will tell you how to add created field into product listing page and how to apply sorting on that field.
So, Here I am going to use one action hook of WordPress used for managing columns of particular post type and you need to add newly created field and then will call custom column action hook to get and display value of that field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | //Add custom column into Product Page function my_columns_into_product_list($defaults) { $defaults['product_checkbox'] = 'Checkbox for Product'; return $defaults; } //Add rows value into Product Page add_action( 'manage_product_posts_custom_column' , 'my_custom_column_into_product_list', 10, 2 ); function my_custom_column_into_product_list($column, $post_id ){ switch ( $column ) { case 'product_checkbox': echo get_post_meta( $post_id , 'product_checkbox' , true ); break; } } |
In this step, I will explain about to sort custom field into product listing page.First of all, will write filter hook which will add sorting action then code for sorting field by values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | add_filter( "manage_edit-product_sortable_columns", "sortable_columns" ); // Make these columns sortable function sortable_columns() { return array( 'product_checkbox' => 'product_checkbox' ); } add_action( 'pre_get_posts', 'event_column_orderby' ); function event_column_orderby( $query ) { if( ! is_admin() ) return; $orderby = $query->get( 'orderby'); if( 'product_checkbox' == $orderby ) { $query->set('orderby','meta_value_num'); } } |
That’s it. Place above code into your theme’s function.php file and run the code into browser :).
Also Read:
Cookie Free domain in WordPress
To create extra field into category or Custom Taxonomy in WordPress
Pagination when getting posts from category in WordPress
Hope this helps someone else out.As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (12)