I have a Woocommerce search form which works fine however I want it to find products not just by what is written in the short description but also by their category name.
For example we have 300 products in the vegetarian category but when you search for "vegetarian" only 6 products show up as only 6 have this keyword in the short description so is giving a poor user experience.
Is there a way to include the category title in the search box query?
// custom product search form
add_filter( 'get_product_search_form' , 'me_custom_product_searchform' );
function me_custom_product_searchform( $form ) {
$form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<input type="text" value="' . get_search_query() . '" name="s" id="s" placeholder="' . __( 'Search for your favourite crisps', 'woocommerce' ) . '" />
<input type="hidden" name="post_type" value="product" />
<button type="submit" id="searchsubmit" />
<span class="icon"><img src="https://onepoundcrisps.com/wp-content/uploads/2022/02/search-icon.png" alt="search icon"></span>
</button>
</form>';
return $form
}
Related
I am attempting to sanitize user input for a search form that I am building in WordPress (in searchform.php file).
I used a built-in WordPress function, sanitize_text_field():
function wpdocs_my_search_form( $form ) {
$search_query = sanitize_text_field( get_search_query() );
$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . home_url( '/' ) . '" >
<div><label class="screen-reader-text" for="s">' . __( 'Search for:' ) . '</label>
<input type="text" value="' . $search_query . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'. esc_attr__( 'Search' ) .'" />
</div>
</form>';
return $form;
}
add_filter( 'get_search_form', 'wpdocs_my_search_form' );
This however, is still not properly sanitizing input such as <script></script>
I also attempted to add additional code to detect if it has been submitted however, that is also not working. I would appreciate any direction/pointers.
Custom Wordpress Search Result Page not working and redirects to 404.
Target Url: https://example.com/blog/search/?post_type=post&s=a
// CUSTOM BLOG POST SEARCH FORM
function blog_search_form( $blog_form ) {
$blog_form = '
<form class="form-inline" id="search" action="'.home_url( "/blog/search/" ).'" method="get">
<div class="form-group mx-sm-1 mb-2 w-100">
<input type="hidden" name="post_type" value="post" />
<input class="form-control w-100" id="s" name="s" type="text" placeholder="Search blog" value="' . get_query_var('s') . '" required />
</div>
<input id="searchsubmit" class="main-btn btn mb-2" type="submit" value="Search" />
</form>';
return $blog_form;
}
add_filter( 'get_search_form', 'blog_search_form' );
// CHANGE URL FUNCTION
/function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
I had added three buttons to the cart page, one for each payment method. Clicking on one of the buttons led to the checkout page where the corresponding payment method used to be pre-selected. It used to work, but somehow has stopped working after updating to the latest Woocommerce version. Any other way of doing this?
Here is the code in cart.php template for the buttons:
<form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<input type="submit" class="checkout-button button alt wc-forward" name="proceed" value="<?php _e( 'Order with Purchase Order', 'woocommerce' ); ?>" />
<input type="submit" class="checkout-button button alt wc-forward" name="proceed" value="<?php _e( 'Order with PayPal', 'woocommerce' ); ?>" />
<input type="submit" class="checkout-button button alt wc-forward" name="proceed" value="<?php _e( 'Order with Credit Card', 'woocommerce' ); ?>" />
<?php /*do_action( 'woocommerce_proceed_to_checkout' );*/ ?>
<?php wp_nonce_field( 'woocommerce-cart' ); ?>
</form>
I want to add an extra button after "add to cart".
It will open a form with only the email field.
After submitting an email id, it will send product name, image, description to that email id.
Here is the graphical representation of the above problem:
First you can hook a action that would add the button after add to cart. You can achieve this by pasting the code below in functions.php file
add_action( 'woocommerce_after_add_to_cart_button', 'send_via_email_btn' );
function send_via_email_btn(){
$btn = '<div class="btn-wrap"><a href="Javascript:void(0)"
onclick="openpopup">Send Via Email</a></div>';
echo $btn;
}
Next you need to create a form where you will have hidden input fields that will post along with email id. You can add this popup form in your footer.php file
<form action="index.php">
<input type="email" name="email" placeholder="email">
<input type="hidden" name="post_title" value="<?php echo get_the_title(); ?>">
<input type="hidden" name="post_content" value="<?php echo get_the_excerpt(); ?>">
<input type="hidden" name="post_image" value="<?php echo get_the_post_thumbnail_url(); ?>">
<input type="hidden" name="form_type" value="mailp_info">
<button type="submit">Send</button>
</form>
if(isset($_POST['form_type']) && !empty($_POST['form_type']) && $_POST['form_type'] == "mail_info") {
$post_title = $_POST['post_title'];
$shortcontent = $_POST['post_content'];
$img = $_POST['post_image'];
$headers = array('Content-Type: text/html; charset=UTF-8','From: My Site Name <support#example.com');
$to = 'emailsendto#example.com';
$subject = 'Product Info';
$body = 'Product Name: '.$post_title.'<br>Product Description: '.$shortcontent;
$attachments = array( $img );
wp_mail( $to, $subject, $body, $headers, $attachments );
}
I hope this works for you. Let me know if any questions
I need to set the search that's part of the theme, to search for Products only. At the moment it searches the whole platform.
It's not a custom form I need just the ability to change the default search operation. So rather than going to /s?green, it includes the 'product' code in the URL so it searches only for products.
add_filter( 'pre_get_posts', 'custom_pre_get_posts' );
function custom_pre_get_posts( $query ) {
if ( is_search() ) {
$query->set('post_type', 'product');
}
return $query;
}
We just want it to show products in their product tiles, rather that blog results.
If you only will ever be searching the product post type on your site, you can create your own searchform.php template to include this hidden field:
<input type="hidden" name="post_type" value="product" />
You can place that anywhere within the form HTML markup. Below is the what WP outputs by default if there is no searchform.php file and you're using the HTML5 form support.
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="hidden" name="post_type" value="product"/>
<label>
<span class="screen-reader-text"><?php _e( 'Search for:', 'label' ); ?></span>
<input type="search" class="search-field" placeholder="<?php esc_attr_e( 'Search …', 'placeholder' ); ?>" value="<?php echo get_search_query(); ?>" name="s"/>
</label>
<input type="submit" class="search-submit" value="<?php esc_attr_e( 'Search', 'submit button' ); ?>"/>
</form>
You can add the above code to your own searchform.php and add that to the root of your THEME directory.
It was within the functions file in the root theme area. We were directed to it, them took it out, placed it in child theme, edited the Form tag in the code, and it worked.