-
AuthorPosts
-
October 30, 2024 at 9:53 am #1470220
When in WooCommerce how do we add in the Category dropdown and search on mobile
October 31, 2024 at 3:53 am #1470296Hey whdsolutions,
Thank you for the inquiry.
There is no option for this by default, but you can try this code in the functions.php file:
function av_search_and_category_dropdown() { if (is_shop() || is_product_category() || is_product_taxonomy()) { echo '<div class="av-mobile-search-category-dropdown">'; ?> <form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url(home_url('/')); ?>"> <div style="display: flex; flex-direction: column;"> <select name="product_cat" id="product_cat" style="width: 100%;"> <option value=""><?php _e('Select Category', 'woocommerce'); ?></option> <?php $categories = get_terms('product_cat'); foreach ($categories as $category) { echo '<option value="' . esc_attr($category->slug) . '">' . esc_html($category->name) . '</option>'; } ?> </select> <input type="text" placeholder="<?php _e('Search products...', 'woocommerce'); ?>" name="s" id="s" /> <input type="hidden" name="post_type" value="product" /> <button type="submit"><?php _e('Search', 'woocommerce'); ?></button> </div> </form> <?php echo '</div>'; } } add_action('woocommerce_before_shop_loop', 'av_search_and_category_dropdown'); function av_prevent_single_product_redirect($query) { if (!is_admin() && $query->is_main_query() && $query->is_search() && is_search()) { $query->set('post_type', array('product')); $query->set('posts_per_page', 12); } remove_action('template_redirect', 'wc_template_redirect'); } add_action('pre_get_posts', 'av_prevent_single_product_redirect');
Then add this css code to only display the form on mobile view:
/* Hide search and category dropdown on larger screens */ .av-mobile-search-category-dropdown { display: none; } /* Show on mobile screens only */ @media (max-width: 768px) { .av-mobile-search-category-dropdown { display: block; padding: 10px; background-color: #f9f9f9; } }
Best regards,
IsmaelNovember 1, 2024 at 11:34 am #1470415That looks great, but selecting a category on mobile it does not take you to that category on the page.
November 1, 2024 at 12:08 pm #1470416These didn’t work for me – so I tried this and for somw reasoon adding it puts the sidebar on the top of the page on desktop also :-(
@media only screen and (max-width:767px) {
.responsive #top.woocommerce-page #main .sidebar {
display: block;
border-color: transparent;
margin-top: -30px;
margin-bottom: -60px; }
}.responsive #top.woocommerce-page #main .sidebar .inner_sidebar {
margin-left: 0 !important;
}.responsive #top.woocommerce-page #wrap_all .container_wrap_first .container {
display: flex;
flex-direction: column-reverse;
}
}
@media only screen and (max-width: 767px) {
.responsive #top .container .av-content-small, .responsive #top #wrap_all .flex_column, .responsive #top #wrap_all .av-flex-cells .no_margin {
margin: 0;
margin-bottom: 20px;
width: 100%;
}
}
@media only screen and (max-width: 767px) {
.responsive #top .container .av-content-small, .responsive #top #wrap_all .flex_column, .responsive #top #wrap_all .av-flex-cells .no_margin {
padding: 50px 0px 0px 0px;
}
}November 1, 2024 at 12:12 pm #1470417SURELY – Having a search bar on ALL devices should be ‘straight out of the box’ and not require ANY coding!!!
November 1, 2024 at 12:18 pm #1470420You can close this thread – sorted! :-)
November 1, 2024 at 1:32 pm #1470426Hi,
Thanks for the update. Please open a new thread if you should have any further questions or problems.
Best regards,
Rikard -
AuthorPosts
- The topic ‘Category dropdown and search on mobile’ is closed to new replies.