search multiple sku's on woocommerce admin side - wordpress

This sounds simple but if you have hundreds of products and need to search multiple sku's on woocommerce admin side is not available.
Lets say you need to verify 600 products, you have to:
manually add one sku to search bar
click search
get results
start again
you can't separate by , or by space, or dash.
I searched and no one have an answer or they are questions made are not answered.
How can people search multiple sku's, or products names on woocommerce admin side?

First.. you mention you have to verify many products (600), needing a multiple sku search. It looks to me you're going to do this manually?
I recommend creating a product loop in PHP where you do your verify stuff. This is probably gonna save you alot off time, and you can re-use it.
Now to the problem... Search woo products using multiple sku's.
I agree many info on the internet is kinda misleading. I've done this before, and i always used the pre_get_posts hook to change the WP (search) query before it runs.
After testing it appears the query is not being setup as a search, and the search value is empty...
So woocommerce has to do a custom search query. Luckily i quickly found mircian's post.
WooCommerce uses a Data Store for its post types ( products, orders, etc ) and the search is also done using a custom function. In this case it’s called ‘search_products’ and it does a custom query which basically returns an array of ids to be used for the results.
I modified his function to search with multiple sku's.
Insert the function in your (child) theme's functions.php.
Use '|' as a SKU delimiter. Example: '1234|1235|1236'
Tested on:
Wordpress 4.9.6
Woocommerce 3.3.5
/**
* Use multiple sku's to find WOO products in wp-admin
* NOTE: Use '|' as a sku delimiter in your search query. Example: '1234|1235|1236'
**/
function woo_multiple_sku_search( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
if (strpos($search_term, '|') == false) return $query_vars;
$skus = explode('|',$search_term);
$meta_query = array(
'relation' => 'OR'
);
if(is_array($skus) && $skus) {
foreach($skus as $sku) {
$meta_query[] = array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
);
}
}
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'meta_query' => $meta_query
);
$posts = get_posts( $args );
if ( ! $posts ) return $query_vars;
foreach($posts as $post){
$query_vars['post__in'][] = $post->ID;
}
}
return $query_vars;
}
add_filter( 'request', 'woo_multiple_sku_search', 20 );

Related

Get product variation ID from variation SKU [duplicate]

I'm working on a separate templates page, which page gets woocommece product sku using custom field of wordpress post. i need to get product id of that sku for create woocommece object and do further things, here is my code.
global $woocommerce;
//this return sku (custom field on a wordpress post)
$sku=get_field( "product_sku" );
if($sku!=''){
//need to create object, but using sku cannot create a object,
$product = new WC_Product($sku);
echo $product->get_price_html();
}
is there way to get product id before create object, then i can pass the product id to WC_Product class constructor and create object.thank you
WooCommerce 2.3 finally adds support for this in core.
If you are using this version, you can call
wc_get_product_id_by_sku( $sku )
You can use this function (found here). Google is your friend!
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}
WooCommerce product is a special post type. Because of this WP_Query might be used to find product by SKU and other parameters. This might be used as an alternative when you need to restrict your search by some other criterias.
For example you might want to specify language if you use Polylang (or other plugins) for site translations. Or you can restrict search by product type.
They do direct SQL query in the WooCommerce method get_product_id_by_sku which I think is perfectly fine in many cases. But might not work if you use translations, it will return random product but not the one in current language.
Example code to find product by SKU using WP_Query (with restriction by product type and language):
public function find( string $lang, string $sku ) {
$query = [
'lang' => $lang,
'post_type' => 'product',
'meta_query' => [
[
'key' => '_sku',
'value' => $sku,
'compare' => '='
]
],
'tax_query' => [
[
'taxonomy' => 'product_type',
'terms' => [ 'grouped' ],
'field' => 'name',
]
]
];
$posts = ( new WP_Query() )->query( $query );
return count( $posts ) > 0 ? $posts[0] : null;
}

Woocommerce URL to hide out of stock product

I create a catalog website using Woocommerce to display all the product. When the product is sell, i dont remove it from the website (because we dont have a lot of product and we want to show to the customer what we sell before).
So, when you go on "All the products" you see the Sell products and the products available. I want, on the sidebar create a button "Show only available product". I dont find a plugin who can do this..
Whis woocommerce, can i create a URL like "mywebsite.com/products&instock=true" for example or something like this ? or if you know another solution. Thanks
You can use pre_get_posts to achieve it. Set the tax_query to not get the term outofstock of the product_visibility taxonomy.
In addition to my code, you will of course need to create a link with the prefix_instock=true parameter. You also can store it in a cookie, so it will be easily persistent.
add_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
function prefix_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() || empty($_GET['prefix_instock'])) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) && $_GET['prefix_instock'] == 'true') {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
}

How to add short description in WooCommerce checkout page

Thanks for reading, Just had a issue regarding WooCommerce, I want to add a short description checkout page of below billing field.
How to add short description in WooCommerce checkout page of below billing field?
I tried add function, custom code but failed with error.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => 'description', 'value' => $post_data->post_excerpt );
return $other_data;
}
I was used this code but it is showing inner product info table.
There's no real reason to call get_post(). The $product object is stored in the $cart_item array and the $post object is stored inside the $product. This gets the product's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn't likely the make the description show up on the order received page, or in the my account area, or in emails, etc since the only place that the woocommerce_get_item_data filter appears is in the cart class.
One thing to take note of, WooCommerce 2.7 is a major rewrite of WooCommerce and $_product->post->post_excerpt will result in PHP notices about directly accessing product properties. So I've suggested both the 2.6 and 2.7 compatible approaches.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$_product = $cart_item['data'];
// Use this for WC2.7
//$other_data[] = array( 'name' => 'description', 'value' => $_product->get_short_description() );
// Use this for WC2.6
$other_data[] = array( 'name' => 'description', 'value' => $_product->post->post_excerpt );
return $other_data;
}

Wordpress: Can you assign the regular post categories to custom post types?

I am just using custom post types for the first time today, so please forgive my ignorance.
I am using a custom post type that is predefined by a plugin. It looks like almost every Event Calendar plugin uses custom post types to set up an "Events" post type.
I was wondering if there is a way to use the normal categories I assign to my regular posts, to assign to the custom events posts.
For example, I have regional categories, like "Southeast" that I have been using for regular posts, but I would also like to be able to assign this category to event posts, so that when people look at the "Southeast" category archive, they can see the regular posts and the events posts associated with that category.
Is this possible?
Thanks for any help in advance
Simple:
add_action( 'init', 'myfuncxx'); function myfuncxx() {
register_taxonomy_for_object_type( 'category', 'custom_postttt_typee' );
}
You would want to use the WordPress function register_taxonomy_for_object_type() Put the following into your theme's functions.php file:
function add_categories_to_events() {
register_taxonomy_for_object_type( 'post_tag', 'event' );
}
add_action( 'init', 'add_categories_to_events' );
I used the code and instructions here:
http://wp.miragearts.com/allinone-event-calendar-events-blog-home-categories-tags/
I found with this code added to the functions.php, that if I create two categories (one for regular posts and one for the event custom posts) with the exact same name and slug, then it is basically the same as having one category.
I think this may be slowing my site down a bit, but it's really too early to tell if it will cause problems.
Here's a copy of the code for the functions.php:
// Add this to your theme's functions.php
function edit_my_query($query) {
// Modify category and tag listings to include ai1ec events and all uses of the same term
// across event and post taxonomies
// ie live-music or arts whether they are event or post categories
// also include ai1ec events in blog home and feeds
if ( ( is_home() || is_feed() || is_category() || is_tag() )
&& empty( $query->query_vars['suppress_filters'] ) ) {
// The 'suppress_filters' test above keeps your menus from breaking
$post_type = get_query_var('post_type');
if($post_type && $post_type[0] != 'post') {
$post_type = $post_type;
} else {
$post_type = array('post','ai1ec_event'); // add custom post types here
}
$query->set('post_type',$post_type);
if (is_category() || is_tag()) {
// Add custom taxonomies to category and tag pages
if (is_category()) {
$taxonomy1 = 'category';
$taxonomy2 = 'events_categories';
}
if (is_tag()){
$taxonomy1 = 'post_tag';
$taxonomy2 = 'events_tags';
}
$queried_object = $query->get_queried_object();
$slug = $queried_object->slug;
$query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => $taxonomy1, 'field' => 'slug', 'terms' => $slug
),
array(
'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
)
));
}
}
}
add_action('pre_get_posts', 'edit_my_query');

Posts from category not displaying when searching for category name

I'm having trouble with my search results page in that it is not displaying posts that are a part of a category when searching for the category name. For Instance, If I search for "doors" (which is a cat) all Partners that are in the "doors" category should be displayed in the search results. Right now, only partners that have the word "doors" in their title or content is displayed.
I'm running a searchAll function so the the standard wp search will search everything.
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'the_search_query', 'searchAll' );
What am I missing?
your query is seraching for post_type, not category_name.
post_type is used for custom post types or taxonomies ..
your query should contain $query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
however, in some cases (and I do not know the reason) that would not work for sub-categories.
in that case, you should use category-slug (slug) insted.
Ive changed my string to this:
<?php
// Define what post types to search
function searchAll( $query ) {
if ( $query->is_search ) {
$query->set( 'category_name', array( 'post', 'page', 'feed', 'partner','project', 'press', 'review' ));
}
return $query;
}
// The hook needed to search ALL content
add_filter( 'pre_get_posts', 'searchAll' );
I do have custom taxonomies though. Basically I'm trying to create a "Search everything" function.

Resources