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.
Related
I'm figuring out a filter but can't be figured :)
Just want to change PrimaryImageOfPage value with an og:image value in CollectionPage for Taxonomies.
It may be that I have to use woocommerce_structured_data_product to filter here and look for the image $markup['image'] = $image;
I have created some filters, but it isn't woocommerce_structured_data_product I guess.
One of them was like that but it... nah.
add_filter( 'woocommerce_structured_data_product', function( $markup ) {
$markup['image'] = ('rank_math/opengraph/{$network}/image');
return $markup;
});
What am I doing wrong? I have been doing this whole day -_-
If You inspect the code of the class WC_Structured_Data, You would see that woocommerce_structured_data_product filter is executed inside function generate_product_data
WC Complete Source of the class WC_Structured_Data : https://github.com/woocommerce/woocommerce/blob/b88b868ab8919b7c854173098b7d6d4ab227f9ee/includes/class-wc-structured-data.php
$this->set_data( apply_filters( 'woocommerce_structured_data_product', $markup, $product ) );
And generate_product_data function is executed on action woocommerce_single_product_summary
add_action( 'woocommerce_single_product_summary', array( $this, 'generate_product_data' ), 60 );
So, your code would work on product single page only and not archive/list pages, such as the taxonomy page.
The feature you want to achieve is not feasible out of the box in WC, You will need to customize yourself.
Logically, structured data on a single product page is feasible due to the fact there is only one product and product information is available, on the other hand, product list has more than one product (you might need to consider like you want to show details of first product) so it is ambiguous and not feasible out of the box for WC.
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
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.
I am trying to AJAXify my Woocommerce cart but I am failing hard and I really can't find any help or documentation (I searched for days). So any help is desperately appreciated.
I tried two approaches:
Custom AJAX Function
When changing the quantity (on the cart page), a AJAX call is fired (-> admin-ajax.php) which triggers a function in my functions.php:
function setQty() {
global $woocommerce;
WC()->cart->set_quantity( $_POST['itemKey'], $_POST['quantity'], true);
echo json_encode(array('totalCount'=>WC()->cart->get_cart_contents_count(), 'total'=>$woocommerce->cart->total)); }
The echoed JSON String contains the correct number of items in cart but the total amount is 0. When I reload the page the altered quantity is not saved.
Same with
WC()->cart->add_to_cart($product_id, $quantity) or adding WC()->cart->calculate_totals()
I tried it this way so i can format the values I need to update my cart how i want.
What am I missing here in order to get the cart update saved?
The woocommerce built-in AJAX update function doesn't trigger, because I have a custom design and I think it needs a specific HTML-structure in order to work properly but i can't find any documentation or examples. Also I need the updated values (total amount, total count etc.) in a custom format. So far it just passes me "updated fragments" as predefined html code.
How do i need to build the HTML code of my cart.php that the ajax update will work? And how do i alter the returning values?
Thank you very much in advance.
I'm not sure what you'd like to update, but I guess you want to update the totals amount of products in the basket.
This example replaces and updates the HTML
Let's say you have a div where your total amount is in:
<div class="cart-totals"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
Add this to your functions.php:
// Mini Cart update with AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'custom_cart_count_fragments', 10, 1 );
function custom_cart_count_fragments( $fragments ) {
$fragments['div.cart-totals'] = '<div class="cart-totals">' . WC()->cart->get_cart_contents_count() . '</div>';
return $fragments;
}
Note: You are replacing not adding
Is it a mini cart/counter you are trying to create or a full page to review/remove products?
If it is a mini-cart/cart counter you wanted, I've created a repo under https://github.com/samisonline/ajax_cart for you that has the basic functionality baked in. This may not be the exact format you're looking for, but it shows the woocommerce functionality at work, as well as the needed markup for AJAX to work!
I've added meta boxes to my posts using the action "add_meta_boxes" to add/change custom settings like background-color, etc.
When I enable the custom fields in my screen options, all values of my meta boxes are displayd in these custom fields!
There are also showing up in the selectbox to add a new "custom field".
If you want to hide your post meta data from custom fields metabox, you should start your meta keys with underscore. Example _background-color
Added:
Also you can use is_protected_meta filter, which return boolean value (true - hide, false - show).
Filter parameters: $protected, $meta_key. See wp-includes/meta.php file. function is_protected_meta()
Normally WP hides meta keys that start with an underscore/_ from the Custom Fields (default/core) MetaBox.
Now imagine that you do not want to give the user of your plugin the possibility to alter Post Meta Data through the ugly and user unfriendly Custom Fields meta box. And therefore you build a custom meta box and prefix your meta key with an underscore/_. Then the user changes his mind and deactivates or uninstalls your plugin. What happens now is that the user has exactly no access to any UI to alter the (still present) meta data. This is a really, really bad situation for the user.
So we need a switch to turn off the Custom Fields MetaBox access as long as your plugin is activated. Therefore WP Core got the is_protected_meta() function. It basically consists out of two lines of code:
$protected = ( '_' == $meta_key[0] );
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
As it's not nice to only offer a filter to handle that, WordPress today has a simple function that you can use:
register_meta( $meta_type, $key, $sanitize_callback, $auth_callback );
And the last argument, the $auth_callback does the following inside this function:
if ( empty( $auth_callback ) ) {
if ( is_protected_meta( $meta_key, $meta_type ) )
$auth_callback = '__return_false';
else
$auth_callback = '__return_true';
}
if ( is_callable( $auth_callback ) )
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
As you can see, you want to just add '__return_false' as $auth_callback to deactivate Custom Fields MetaBox access as long as your plugin is active. When the user removes or deactivates your plugin, he instantly has access to the meta field via the standard Custom Fields MetaBox.
Notes: WP core at v4.0.1 while writing this question. Make use of the $sanitize_callback! Thanks to Trepmal for posting about the is_protected_meta filter on her blog. Else I would have never stumbled upon the use case for that.