Cannot search products on wp-admin of woocommerce - php

Scenario: I have my searchbox on my front page of my website. On that searchbox, I can search the product by SKU and product name.
Problem: I can search a product on client's page/Front-end but not on wp-admin of the website.
I already tried this : Woocommerce cannot see products in wp-admin , but still no result found.
Something strange!
Does anybody know?

For the sake of those developers who may also encounter same problem, that's why I want to put it here for future reference.
I have been backtracking and searching answers on the internet but I found none. I tried to review my functions.php inside the theme and found this code below:
function __search_by_title_only( $search, &$wp_query ) {
global $wpdb;
$entry = isset($_GET['s']) ? $_GET['s'] : '';
if( $entry ){
$search = "MY SQL QUERY HERE.....";
}
return $search;
}
add_filter( 'posts_search', '__search_by_title_only', 500, 2 );
The above code was inserted inside the functions.php in order to overwrite the query.
posts_search filter, overrrides the product search on my back-end of search though my query was mistaken that's why the problem exists.
I just removed the code and everything works fine.

Related

Explanation about filter loop_shop_per_page with woocommerce

I needed to find a way to custom my pagination for a shop page only.
So I found this :
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );
function new_loop_shop_per_page( $cols ) {
// $cols contains the current number of products per page based on the value stored on Options -> Reading
// Return the number of products you wanna show per page.
$cols = 9;
return $cols;
}
This worked very well.
But I can't find where the hook/action is in all my woocommerce files.
I know this filter applies on something, but can't guess where.
Impossible to find an explanation on the web.
Does someone knows what it's about ?
As said in the comment : one must also search the woocommerce plugin also ( not only your plugin/theme files ).
loop_shop_per_page filter should be called here : wp-content/plugins/woocommerce/includes/class-wc-query.php

Wordpress - WooCommerce Custom Field Searching

I'm using Wordpress with WooCommerce. I've got this code which works (it was added to my 'functions.php' file).
/* add custom search options for Woocommerce */
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields )
{
$search_fields[] = 'SERIAL_NUMBER';
return $search_fields;
}
SERIAL_NUMBER is a custom field I enter per product sold manually.
The searching is working, but I have yet another serial that I sometimes search called "DATE_CODE".
How can I search for both?
I tried using:
$search_fields[] = 'SERIAL_NUMBER' + 'DATE_CODE';
But that didn't work because I assume it was searching for a serial number plus a date code in a single string combination (which isn't what I'm after). I'm after a method where I can search for either one :)
Thanks.

WP custom comment field only on certain page

I added an additional field to my comment section of my wordpress page with the help of a little plugin, based on a tutorial https://www.smashingmagazine.com/2012/05/adding-custom-fields-in-wordpress-comment-form/.
It's only one field I need (ZIP Code).
The plugin works fine, but it adds the custom field on every page. I just want it on one specific page.
I tried to wrap all the plugin code into one function that loads on the specific page:
add_action('template_redirect', 'load-on-certain-page');
function load-on-certain-page(){
if ( is_page(23) ) {
//Complete Plugin Code
}
}
Basically that works, but the check for the empty field isn't working anymore.
add_filter( 'preprocess_comment', 'verify_comment_meta_data' );
function verify_comment_meta_data( $commentdata ) {
if (empty( $_POST['title'] ) )
wp_die( __( 'Fehler: Bitte geben Sie Ihre Postleitzahl ein.' ) );
return $commentdata;
}
If I exclude the field check from my load-on-certain-page() function it checks the field everywhere so thats no solution. I also tried to add an additional condition to the if-statement (is_page()), but that does not work, too.
Can you point me into the right direction how to make my plugin functions work only on that certain page? And what "best practise" is to do it?
Thanks!
You can can possibly add an if/else statement to only show the field on that specific page...
if ( $post->ID === 23 ) {
// Input for zip code on page 23
} else {
// No input for zip code
}
Finally I got it.
The problem is, that is_page() etc. didn't work for the "preprocess_comment" filter.
But you can check the page ID the comment will be assigned to by using:
$post_id = $commentdata['comment_post_ID'];

wordpress functions.php - use different page template for each post category

I want to hook into the save_post function, find out what category the post is in, and then assign a different page template for posts in each category. I've tried about 30 different versions of this with no luck. Will someone please help point me in the right direction?
add_action( 'save_post', 'assign_custom_template' );
function assign_custom_template($post_id) {
$category = get_the_category($post_id);
$cat_id = $category->cat_ID;
if( $cat_id == 1 ) {
update_post_meta($post_id, "_wp_page_template", "template1.php");
}
if( $cat_id == 2 ) {
update_post_meta($post_id, "_wp_page_template", "template2.php");
}
}
You just need to create category-1.php which rendered as template1.php and category-2.php which rendered as template2.php in your theme root.
See template hierarchy for more info.
I tried to emulate the official WP hierarchy scheme among my posts & custom post types, but it just wasn't happening. I ended up using Custom Post Types so that I could assign templates to both the "list" pages and the "individual" pages. And then I wrote some javascript that looks for the post-type string in the URL, and if it's detected, it adds the current_page_parent/ancestor classes to the appropriate menu items. Not perfect or totally future-proof, but it gets the job done.
If someone comes up with a better solution, please post it!

Wordpress URL Routing, Multiple permalinks with different templates

This question is based on an unanswered question in Wordpress Development which has not gotten a solid answer.
I have a wordpress website which lists hotels. the url for a single hotel looks like:
/hotels/the-marriot-hotel
I also have a custom taxonomy for Locations, which allows me to browse the hotels in various locations, which works fine, the urls are like:
/Locations/Liverpool
For URL's like /hotels/* I would like to use a custom template, which I have done already and works fine.
The Problem
I also want to be able to drilldown the Locations taxonomy creating a breadcrumb type URL and also use a different template for the hotel page.
For Example, if a user is browsing /Locations/Liverpool and clicks the Marriot Hotel I would like it to click through to /Locations/Liverpool/the-marriot-hotel instead of /hotels/the-marriot-hotel and also use a slightly different template, which can also load a different sidebar and recommend other hotels in the area specific to the location slug in the URL
So basically I want two routes to a single post and a different template used based on the route used.
How would I go about implementing this?
What have I tried?
I've tried adding a new page and using a rewrite rule to point to it to be the locations hotel page.
I've tried adding a slug on the end of the /Locations/{location-slug} url and reading this in the page template and loading the hotel post instead of the list it doesn't seem to be working but also feels like a terrible hack anyway
An idea that I've had is to add a rewrite to the hotels/{slug} page and using code to detect the URL used and switch templates dynamically but I'm not sure this is the best approach
I have managed to get this working using the second method mentioned above (adding a rewrite to the locations landing page and checking for a query_var).
I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.
I used this online post for reference.
Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.
function prefix_locations_rewrite_rule() {
add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}
function prefix_register_query_var( $vars ) {
$vars[] = 'hotel';
return $vars;
}
function prefix_url_rewrite_templates() {
if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
add_filter( 'template_include', function() {
return get_template_directory() . '/taxonomy-locations.php';
});
}
}
add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );
In my template file for the hotels landing page:
$hotelSlug = get_query_var( 'hotel', false);
if ( have_posts() ) {
while (have_posts()) : the_post();
if ($post->post_name == $hotelSlug) {
break;
}
endwhile;
}
This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.
We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.
I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue

Categories