Woocommerce Admin Product Search Not working - wordpress

I ran into the issue that woocommerce product search in admin not working and giving result as No product found though there are lots of products with my search input.
Also i haven't used relevancy search plugin so there will be no any issue.
I set debug true in wp-congig.php and its throwing one error related to wp_meta with printing whole query.
Also tried disable theme and try with default theme also tried to disable every plugin one by one but still no luck.

I was experiencing the same thing and adding the following code block to my functions.php file fixed it immediately:
function m_request_query( $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'] ) );
// Split the search term by comma.
$search_terms = explode( ',', $search_term );
// If there are more terms make sure we also search for the whole thing, maybe it's not a list of terms.
if ( count( $search_terms ) > 1 ) {
$search_terms[] = $search_term;
}
// Cleanup the array manually to avoid issues with quote escaping.
array_walk( $search_terms, 'trim' );
array_walk( $search_terms, 'esc_sql' );
$meta_key = '_sku';
$post_types = array( 'product', 'product_variation' );
$query = "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value IN ('" . implode( "','", $search_terms ) . "') AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC";
$search_results = $wpdb->get_results( $query );
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
$query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
}
return $query_vars;
}
add_filter( 'request', 'm_request_query', 20 );
Shout-out to mircian for the snippet.

Related

How to display a custom message during WooCommerce checkout to customers who previously purchase products with order status of 'processing'

I will like to display a message on the checkout page notifying the customers that they have purchased this product in the past (the product they are about to purchase), but this message should only run if these conditions are met.
Customer must be logged in
User role is administrator or customer
previously purchased product should still have order status of 'processing.'
So far, I have been able to get the first 2 conditions working fine:
function user_logged_in_product_already_bought() {
global $woocommerce;
if ( ! is_user_logged_in() ) return;
$items = $woocommerce->cart->get_cart();
$has_bought = false;
foreach($items as $item => $values) {
if ( wc_customer_bought_product( '', get_current_user_id(), $values['data']->get_id() ) ) {
$has_bought = true;
break;
}
}
$user = wp_get_current_user();
$allowed_roles = array( 'administrator', 'customer' );
if ( array_intersect( $allowed_roles, $user->roles ) ) {
if( $has_bought ){
wc_print_notice( "You purchased this in the past. Buy again?", 'success' );
}
}
}
add_action( 'woocommerce_before_checkout_form', 'user_logged_in_product_already_bought' );
Note: the reason I'm using the foreach is that users only purchase one product at a time (can't have more than a single appointment product in the cart)
But I don't know how to go about with the third condition. Any advice?
Your first 2 steps indeed work, for the 3rd step you will have to use a custom function which only checks for the order status 'processing'.
The advantage of this custom function is that it is much faster and lighter compared to going through all existing orders.
So you get:
function has_bought_items( $user_id = 0, $product_ids = 0 ) {
// When empty, return false
if ( empty ( $product_ids ) ) return false;
global $wpdb;
$product_ids = is_array( $product_ids ) ? implode( ',', $product_ids ) : $product_ids;
$line_meta_value = $product_ids != 0 ? 'AND woim.meta_value IN (' . $product_ids . ')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_status IN ( 'wc-processing' )
AND pm.meta_key = '_customer_user'
AND pm.meta_value = '$user_id'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
function action_woocommerce_before_checkout_form() {
// Customer must be logged in
if ( ! is_user_logged_in() ) return;
// Get current user
$user = wp_get_current_user();
// Allowed user roles
$allowed_roles = array( 'administrator', 'customer' );
// Compare
if ( array_intersect( $allowed_roles, $user->roles ) ) {
// WC Cart NOT null
if ( ! is_null( WC()->cart ) ) {
// Initialize
$product_ids = array();
// Loop through cart contents
foreach ( WC()->cart->get_cart_contents() as $cart_item ) {
// Get product ID and push to array
$product_ids[] = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
}
// Call function, and if true
if ( has_bought_items( $user->ID, $product_ids ) ) {
// Notice
wc_print_notice( __( 'You purchased this in the past. Buy again?', 'woocommerce' ), 'success' );
}
}
}
}
add_action( 'woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form' );
Result: a general message
Optional: Instead of displaying a general message, but showing this separately per product, you can use the woocommerce_checkout_cart_item_quantity hook
So you would get:
function has_bought_items( $user_id = 0, $product_ids = 0 ) {
// When empty, return false
if ( empty ( $product_ids ) ) return false;
global $wpdb;
$product_ids = is_array( $product_ids ) ? implode( ',', $product_ids ) : $product_ids;
$line_meta_value = $product_ids != 0 ? 'AND woim.meta_value IN (' . $product_ids . ')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_status IN ( 'wc-processing' )
AND pm.meta_key = '_customer_user'
AND pm.meta_value = '$user_id'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
function filter_woocommerce_checkout_cart_item_quantity( $item_qty, $cart_item, $cart_item_key ) {
// Customer must be logged in
if ( ! is_user_logged_in() ) return;
// Get current user
$user = wp_get_current_user();
// Allowed user roles
$allowed_roles = array( 'administrator', 'customer' );
// Initialize
$message = '';
// Compare
if ( array_intersect( $allowed_roles, $user->roles ) ) {
// Get product id
$product_id = $cart_item['variation_id'] > 0 ? $cart_item['variation_id'] : $cart_item['product_id'];
// Call function, and if true
if ( has_bought_items( $user->ID, $product_id ) ) {
$message = '<p>' . __( 'You purchased this in the past. Buy again?', 'woocommerce' ) . '</p>';
}
}
// Return
return $item_qty . $message;
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'filter_woocommerce_checkout_cart_item_quantity', 10, 3 );
Result: show separately by product
Note: the has_bought_items() function is based on Check if a user has purchased specific products in WooCommerce answer code
Related: Display message below product name on WooCommerce cart page if user has bought product before
according to this article:
you can implement such a login as below :
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
// LOOP THROUGH ORDERS AND GET PRODUCT IDS
if ( ! $customer_orders ) return;
$product_ids = array();
foreach($items as $item) {
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$orderItems = $order->get_items();
foreach ( $orderItems as $orderItem ) {
if ($orderItem->get_product_id() == $item->get_product_id() )
$has_bought = true;
break;
}
}
}
Regarding 3rd point you can get the customer processing orders easily using wc_get_orders()
$user = wp_get_current_user();
$processing_orders = wc_get_orders(
array(
'customer' => $user->ID,
'status' => 'wc-processing',
)
);
You can check if the processing orders are empty or not.
and you can loop over the orders products to get details about each product.
foreach ( $processing_orders as $p_order ) {
$order_products = $p_order->get_items();
foreach ( $order_products as $product_item ) {
$product_id = $product_item->get_id();
}
}

Promote featured products to top of category in WooCommerce [duplicate]

This question already has answers here:
Woocommerce - Display Featured Products at top of Category Page
(2 answers)
Woocommerce: Show Products in Alphabetic order
(5 answers)
WooCommerce - Show Random Products
(7 answers)
Closed 2 years ago.
I wanted to have featured products promoted and listed first within category archive pages.
I found a lot of people with similar needs, and no ideal solutions, so I'm posting this here for others looking for this solution.
The following code will add a "Recommended" option to the sorting options within the "Product Catalogue" section within the WooCommerce display settings (within the visual customizer). Set this new option as your default sorting option to have featured products promoted to the top, and listed first.
Add this code to your functions.php in your file (as always, use a child theme or code snippet plugin):
//add new sorting option
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['recommended'] = 'Recommended';
return $sortby;
}
//set default sorting for new option
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'recommended' == $orderby_value ) {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
$args['meta_key'] = '';
}
return $args;
}
//adjust order to allow for featured posts
add_filter('posts_orderby', 'show_featured_products_orderby',10,2);
function show_featured_products_orderby($order_by, $query){
global $wpdb ;
if( (!is_admin()) ){
$orderby_value = ( isset( $_GET['orderby'] ) ? wc_clean( (string) $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ) );
$orderby_value_array = explode( '-', $orderby_value );
$orderby = esc_attr( $orderby_value_array[0] );
$order = ( !empty($orderby_value_array[1]) ? $orderby_value_array[1] : 'ASC' );
$feture_product_id = wc_get_featured_product_ids();
//only apply to recommended sorting option
if ( $orderby == "recommended" && is_array( $feture_product_id ) && !empty($feture_product_id) ) {
if ( empty($order_by) ) {
$order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC ";
} else {
$order_by = "FIELD(" . $wpdb->posts . ".ID,'" . implode( "','", $feture_product_id ) . "') DESC, " . $order_by;
}
}
}
return $order_by;
}

Woocommerce recently viewed Products

I have created a recently viewed script which generated a shortcode which I then inserted into my home page.
The script is designed so that people who may have visited my website and left, once they come back can see instantly what products they had been viewing on their last visit.
I have placed the shortcode [woocommerce_recently_viewed_products]
and have generated the shortcode using the following script:
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// Get WooCommerce Global
global $woocommerce;
// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
// If no data, quit
if ( empty( $viewed_products ) )
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
// Create the object
ob_start();
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
// Get products per page
if( !isset( $per_page ) ? $number = 4 : $number = $per_page )
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ( $r->have_posts() ) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ( $r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products",
"rc_woocommerce_recently_viewed_products");
Everything seems to have registered. However,when I test this myself. I view a few products, go back to the homepage where the shortcode is registered and I see the text
You have not viewed any product yet!
I can not figure out what might be missing in order to register and show the products which I or a potential customer may have viewed.
Woocommerce only save the recently viewed cookie IF woocommerce_recently_viewed_products WIDGET is ACTIVE! See code in wc-product-functions.php wc_track_product_view() function.
Code to save the cookie always in functions.php:
/**
* Track product views. Always.
*/
function wc_track_product_view_always() {
if ( ! is_singular( 'product' ) /* xnagyg: remove this condition to run: || ! is_active_widget( false, false, 'woocommerce_recently_viewed_products', true )*/ ) {
return;
}
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // #codingStandardsIgnoreLine.
$viewed_products = array();
} else {
$viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // #codingStandardsIgnoreLine.
}
// Unset if already in viewed products list.
$keys = array_flip( $viewed_products );
if ( isset( $keys[ $post->ID ] ) ) {
unset( $viewed_products[ $keys[ $post->ID ] ] );
}
$viewed_products[] = $post->ID;
if ( count( $viewed_products ) > 15 ) {
array_shift( $viewed_products );
}
// Store for session only.
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
remove_action('template_redirect', 'wc_track_product_view', 20);
add_action( 'template_redirect', 'wc_track_product_view_always', 20 );
You need to set the cookie when you are viewing a single product page so use something like this where I set the cookie to equal the product ID I just viewed. In your case you'll need to get the cookie value if it exists then append the new product to the list of products.
function set_user_visited_product_cookie() {
global $post;
if ( is_product() ){
// manipulate your cookie string here, explode, implode functions
wc_setcookie( 'woocommerce_recently_viewed', $post->ID );
}
}
add_action( 'wp', 'set_user_visited_product_cookie' );
Below code to set cookie 'woocommerce_recently_viewed' worked for me. Hope it helps other
$Existing_product_id = $_COOKIE['woocommerce_recently_viewed'];
if ( is_product() )
{
$updated_product_id = $Existing_product_id.'|'.$post->ID;
wc_setcookie( 'woocommerce_recently_viewed', $updated_product_id );
}

WooCommerce Create a downloadable product's order programmatically

I use wc_create_order to create an order and everything is fine except the user can't have the downloadable product's download link in My Downloads section.
$def_args = array('customer_id' => $user_id, 'status' => 'completed');
$order = wc_create_order( $def_args );
$targs['totals']['subtotal'] = $ord['pay_amount'];
$targs['totals']['total'] = $ord['pay_amount'];
$targs['totals']['subtotal_tax'] = 0;
$targs['totals']['tax'] = 0;
$sku_ = $ord['sku'];
$order->add_product( get_product_by_sku( $sku_ ) , 1, $targs ); //(get_product with id and next is for quantity)
$order->set_address( $address, 'billing' );
$order->set_address( $address, 'shipping' );
$order->set_total( $ord['pay_amount'] );
$order->calculate_totals();
// I took get_product_by_sku function in stackoverflow but I don't remember which question.
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;
}
$ord variable has some information about the order.
Should I need to call a function or something like that to make order with download link?
I found the solution, when creating an order, make the status processing and after call update_status.
$def_args = array('customer_id' => $user_id, 'status' => 'processing');
...
$order->update_status('completed');
Then user have her/his download links.

pre_get_posts for products on category page - woocommerce

I would like to filter the products on a category products so that it shows only the products of a certain author or multiple authors.
I already have the following code. This works as it filters the products. The correct products are displayed. Except the Woocommerce filters on in the left sidebar are not affected by the filter. The filters on the left side are showing all the original products in the category (also from other users) so the count isn't correct and also attributes from products that are filtered are showing. This shouldn't be the case. Do I have to add another pre_get_posts for the filters?
<?php
function pre_get_posts_by_author( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
$cat_obj = $q->get_queried_object();
if($cat_obj->name == 'Nieuw')
{
$q->set( 'author_ids', '2086,2084');
}
}
add_action( 'pre_get_posts', 'pre_get_posts_by_author' );
add_filter( 'posts_where', 'author_posts_where', 10, 2 );
function author_posts_where( $where, &$wp_query )
{
global $wpdb;
if ( $wp_query->get( 'author_ids' ) ) {
$where .= ' AND ' . $wpdb->posts . '.post_author IN (' . $wp_query->get( 'author_ids' ) .')';
}
return $where;
}
?>
Thanks for helping out!
Probabaly a better way to do this is by setting 'author__in' argument in your query which will take an array of authors the query will return products of.
function pre_get_posts_by_author( $q ) {
if ( ! $q->is_main_query() || !$q->is_post_type_archive() ) return;
$cat_obj = $q->get_queried_object();
if( $cat_obj->name == 'Nieuw' ){
$q->set( 'author__in', array(2086,2084));
}
}
add_action( 'pre_get_posts', 'pre_get_posts_by_author' );
My updated solution: still dirty as it modifies Woocommerce's core files (2.2.4), but it works:
in the pre_get_posts-hook I retrieve the ids of the products I want to exclude with the following code:
$_SESSION['total_excluded'] = get_objects_in_term( $term_ids, $taxonomies, $args )`
(reference: http://codex.wordpress.org/Function_Reference/get_objects_in_term)
Then in woocommerce/includes/widgets/class-wc-widget-layered-nav.php I changed line 258 to:
$count = sizeof( array_diff(array_intersect( $_products_in_term, WC()->query->filtered_product_ids) , $_SESSION['total_excluded'] ) );
In woocommerce/includes/widgets/class-wc-widget-price-filter.php the new lines 121, 136 are:
%1$s.ID IN (' . implode( ',', array_map( 'absint', array_diff(WC()->query->layered_nav_product_ids, $_SESSION['total_excluded'] ) ) ) . ')
In woocommerce/includes/widgets/class-wc-widget-price-filter.php the new lines 123, 138 are:
%1$s.post_parent IN (' . implode( ',', array_map( 'absint', array_diff(WC()->query->layered_nav_product_ids, $_SESSION['total_excluded'] ) ) ) . ')

Resources