right now i am using below code
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
// Product Select
?>
<p class="form-field product_field_type">
<label for="product_field_type"><?php _e( 'Product Select', 'woocommerce' ); ?></label>
<select style="width:100%" id="product_field_type" name="product_field_type[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e( 'Search for a product…', 'woocommerce' ); ?>">
<?php
$product_field_type_ids = get_post_meta( $post->ID, '_product_field_type_ids', true );
$product_ids = ! empty( $product_field_type_ids ) ? array_map( 'absint', $product_field_type_ids ) : null;
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = get_product( $product_id );
$product_name = woocommerce_get_formatted_product_name( $product );
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . esc_html( $product_name ) . '</option>';
}
}
?>
</select> <img class="help_tip" data-tip='<?php _e( 'Your description here', 'woocommerce' ) ?>' src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" height="16" width="16" />
</p>
<?php
echo '</div>';
}
which shows below product field but not working properly
but i wants product search like below pic
I am using below website for code
http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
I was trying to figure out the same problem for a client of mine recently. I had originally used that same tutorial to add a custom product search input field for him on the back end and it broke when we got him updated to WooCommerce 2.5+.
To add the product search widget/field to the backend:
To Save: (saved in the same id int array format as the existing field so you don’t have to start from scratch with your existing database entries)
To display on the front end: (same as before)
I copied this code from up-sells. This is only the search form.
This answer is a great starting point but there are some changes required to make this work with the latest WooCommerce releases where the product search field has changed from a
input
withtype=hidden
to aselect
with amultiple
attribute.Backend input field
You can use the backend HTML of the WooCommerce upsells field, which you can find here on GitHub, as a template for your own backend field.
Saving the backend input field
You can use the following code to retrieve the selected items and save there ids as post meta.
A more general solution (not only on product page but also on all other pages) is described here: https://www.binarycarpenter.com/create-woocommerce-products-search-input-for-your-plugins/
and the code is here:
https://github.com/datmt/WooCommerce-Ajax-Product-Search
Minimum working example based on the above mentioned code:
1.Append the jquerys select2 script and your own my_product_search_script.js like this. Put this next to your function:
2.Add your own js file called my_product_search_script.js next to the php file you are working in with this code:
3.Finally, you can use in your php function this code which creates the products search select fields:
3.1including presetting the field, and retrieving the value after form submition the select can be included in your php function like this:
You are using woocommerce plugin. To filter the products with price range, color, size and other fields you can use another woocommerce plugin ‘woocommerce products filter’. You can user yith plugin to search the products. If you want to provide a search works for whole site not only for products only user dave’s live search plugin.
Dave’s live search plugin
Yith products search plugin