Backend Wordpress Product Search without spaces - woocommerce

Please!! I'm having trouble with a website woocommerce backend product search. I work with products with alfa numeric titles so I'm trying to look for a code that let me search without spaces, for example Item: Antenna-RRT-4000 (I want to search RRT4000 and it's not letting me) It shows no products but if I type RRT 4000, shows the items I'm looking for. I try with diferent plugins (relevansi and others) and nothing.. Thnanks in advance!!!
Is it posible to do it with code like this in the fuctions.php?? this one is broken is it posible to fix it?
add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$posts = get_posts( $terms ); {
$new_terms = array();
if ( $terms ) {
foreach( $terms as $term ) {
if ( preg_match( "/[\d]+/i", $term ) ) {
if ( preg_match( "/\b([^\d\W]+)([\d]+)\b/i", $term, $matches ) || preg_match( "/\b([\d]+)([^\d\W]+)\b/i", $term, $matches ) ) {
if ( strlen( $matches[1] ) > 1 ) {
$new_terms[] = $matches[1];
}
if ( strlen( $matches[2] ) > 1 ) {
$new_terms[] = $matches[2];
}
}
}
}
if ( ! empty( $new_terms ) ) {
$terms = array_merge( $terms, $new_terms );
}
}
return $terms;
}

Related

Can't add to cart (custom function) when not logged

this function was working fine but since few days (and few updates Wp 5.9 to 6.X And others plugins) it only work when i'm logged in!.. For normal Users. the form post but the Cart is empty.. This function is used to add in bulk one ore more variation of the same product..
The function is in my child theme and was working for 1 year.. unfortunately.. i can't find a backup from before my updates!?! SHAME ON ME!!
function cps_add_multiple_products_to_cart() {
if ( empty( $_REQUEST['add-to-cart-manual'] )) {
return;
}
global $woocommerce;
$active_step = active_step();
$step_urls = step_urls();
//'add-to-cart' should be in this format - product_id1|variation_id1|variation_attribute1|variation_value1,product_id2|variation_id2|variation_attribute2|variation_value2 ... this is the format - product_id1|variation_id1|variation_attribute1|variation_value1,product_id2|variation_id2|variation_attribute2|variation_value2
$product_var_ids = explode( ',', $_REQUEST['add-to-cart-manual'] ); //product with variation ids that needs to be added
foreach ( $product_var_ids as $product_var_id ) {
$pdt_var_temp = explode( '|', $product_var_id );
$product_id = $pdt_var_temp[0];
$variation_id = $pdt_var_temp[1];
$variation_attribute = $pdt_var_temp[2];
$variation_attribute_value = $pdt_var_temp[3];
$variation_qte = $pdt_var_temp[4];
$pdt_in_cart = false;
foreach ( $woocommerce->cart->cart_contents as $key => $values ) {
if( in_array( $values['variation_id'], $variation_id ) ) {
$pdt_in_cart = true;
}
}
if ( !$pdt_in_cart ) {
$quantity = $variation_qte;
if ($quantity > 0) {
$variation = array( $variation_attribute => $variation_attribute_value );
$woocommerce->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation );
}
}
}
}
add_action( 'woocommerce_before_main_content', 'cps_add_multiple_products_to_cart', 15 );

Remove duplicate values from custom column on WooCommerce admin orders list

I have the following code that adds a custom column on WooCommerce admin orders list
add_action( 'manage_shop_order_posts_custom_column', 'inforder_add_new_order_admin_list_column_content' );
function inforder_add_new_order_admin_list_column_content( $column ) {
global $post;
if ( 'product_order' === $column ) {
$order = wc_get_order( $post->ID );
$items = $order->get_items();
$count = count($items);
$i = 1;
foreach($items as $item) {
if($i==$count)
echo $item->get_name();
else
echo $item->get_name() .', ';
$i++;
}
}
}
But this shows duplicate values. I've tried to avoid this using
array_unique(), but unfortunately without the desired result.
Any advice to avoid this?
Your code contains some mistakes
The manage_edit-shop_order_columns is missing, which is for adding columns
The manage_shop_order_posts_custom_column has not 1 but 2 parameters, the 2nd contains the $post_id so the use of global $post is not necessary
You can use in_array(), which checks if a value exists in an array. If this is not the case, we will add this value to the array, this way we avoid duplicate values
So you get:
// Add header
function filter_manage_edit_shop_order_columns( $columns ) {
$columns['product_order'] = __( 'Product', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
// Display (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'product_order' ) {
// Get order
$order = wc_get_order( $post_id );
// Initialize
$names = array();
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get items
$items = $order->get_items();
// Loop through
foreach ( $items as $key => $item ) {
// Get name
$name = $item->get_name();
// NOT in array
if ( ! in_array( $name, $names, true ) ) {
// Push one or more elements onto the end of array
array_push( $names, $name );
}
}
// NOT empty, print result
if ( ! empty( $names ) ) {
// Use implode() function to join comma in the array
$list = implode( ', ', $names );
// Output
echo $list;
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

WooCommerce Parent Order Prefix

I am trying to achieve an order prefix between "Parent Order" and "Sub Orders". Currently I have the order prefix's working for products in specific categories, so 'Merchandise' Categories and products within have the order number prefix 'M' and the Categories and products within 'Coffee' have the prefix 'C', this all works fine, brilliantly see the image attached.
I do have subscription products and single products within the store but I mainly just want the one 'P' to show up for the main order regardless of the category the product is within.
I need and would like help adding for the parent order a 'P' so that I can distinguish between Parent and sub orders, (please see attached image) I cannot for the life of me figure out this function currently my code is this:
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$order = wc_get_order( $order_id );
$category = array();
if ( ! empty($order) ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item['product_id'];
$terms = get_the_terms ( $product_id, 'product_cat' );
$parent_id = $order->get_parent_id( $parent, 'product_id' );
if ( ! empty($terms) ) {
foreach ( $terms as $term ) {
$category[] = $term->term_id;
}
}
}
}
if ( $parent ) {
$order_id = 'P - '. $order_id;
} else {
if ( in_array( 23, $category ) ) {
$order_id = 'M - '. $order_id;
} else {
if ( in_array( 16, $category ) ) {
$order_id = 'C - '. $order_id;
}
}
return $order_id;
}
}
The 'M' & 'C' work perfectly... just the parent order 'P' does not so instead of a 'P' I have what seems to be a random M or C in the parent order section. All my orders split in categories and this still works as expected (please see image). I mainly just need the 'P' Main Parent order on the left from the image attached
Parent Order Prefix 'P' Location
Thanks in advance.
You are currently returning the order number only within the else statement.
A value should always be returned at the end of the function.
Also the $parent variable is not set and you are getting the parent id the wrong way.
Try this, hope it works.
The get_parent_id() method returns 0 if the function has no parent order.
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$order = wc_get_order( $order_id );
$category = array();
if ( ! empty($order) ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item['product_id'];
$terms = get_the_terms ( $product_id, 'product_cat' );
if ( ! empty($terms) ) {
foreach ( $terms as $term ) {
$category[] = $term->term_id;
}
}
}
}
$parent_id = $order->get_parent_id();
if ( $parent_id > 0 ) {
if ( in_array( 23, $category ) ) {
$order_id = 'M - '. $order_id;
} else {
if ( in_array( 16, $category ) ) {
$order_id = 'C - '. $order_id;
}
}
} else {
$order_id = 'P - '. $order_id;
}
return $order_id;
}
Also you could optimize the function like this:
// change the WooCommerce order number
add_filter( 'woocommerce_order_number', 'change_woocommerce_order_number' );
function change_woocommerce_order_number( $order_id ) {
$order = wc_get_order( $order_id );
$prefix = '';
$has_cat_id_23 = false;
$has_cat_id_16 = false;
// get the parent order id
$parent_id = $order->get_parent_id();
// if it does not have a parent order it returns the order number with the prefix "P - "
if ( $parent_id == 0 ) {
$prefix = 'P - ';
return $prefix . $order_id;
// otherwise set the prefix to the child orders
} else {
foreach ( $order->get_items() as $item_id => $item ) {
// if the product belongs to the product category with ID 23
if ( has_term( 23, 'product_cat', $item['product_id'] ) ) {
$has_cat_id_23 = true;
break;
}
// if the product belongs to the product category with ID 16
if ( has_term( 16, 'product_cat', $item['product_id'] ) ) {
$has_cat_id_16 = true;
break;
}
}
// if the product belongs to the product category with ID 23 set the prefix "M - "
if ( $has_cat_id_23 ) {
$prefix = 'M - ';
}
// if the product belongs to the product category with ID 16 set the prefix "C - "
if ( $has_cat_id_16 ) {
$prefix = 'C - ';
}
}
return $prefix . $order_id;
}
The code has been tested and works (I didn't install the plugin but actually only used Wordpress and WooCommerce functions and methods). The result will look like:
Try it out and let me know if it works for you too.

Hide shipping message until product is added to cart in WooCommerce

I have this code which tells the customer how much is left to spend until shipping is free. But, it shows as soon as you visit a product page no matter if the product is added to the cart or not.
I would like it to show (see the various hooks) only after a product is added to cart. Any ideas?
Here is the code:
add_action( 'woocommerce_before_single_product', 'free_shipping_cart_notice_zones' );
add_action( 'woocommerce_checkout_before_order_review', 'free_shipping_cart_notice_zones' );
add_action( 'woocommerce_cart_totals_after_order_total', 'free_shipping_cart_notice_zones' );
function free_shipping_cart_notice_zones() {
global $woocommerce;
if ( WC()->cart->get_cart_contents_count() != 0 ) {
$default_zone = new WC_Shipping_Zone(0);
$default_methods = $default_zone->get_shipping_methods();
foreach( $default_methods as $key => $value ) {
if ( $value->id === "free_shipping" ) {
if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
}
}
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ( $delivery_zones as $key => $delivery_zone ) {
foreach ( $delivery_zone['shipping_methods'] as $key => $value ) {
if ( $value->id === "free_shipping" ) {
if ( $value->min_amount > 0 ) $min_amounts[] = $value->min_amount;
}
}
}
if ( is_array($min_amounts) ) {
$min_amount = min($min_amounts);
$current = WC()->cart->subtotal;
if ( $current < $min_amount ) {
$shipping_text = esc_html__('You are ', 'woocommerce' ) . wc_price( $min_amount - $current ) . esc_html__(' away from FREE SHIPPING!', 'woocommerce' );
echo '<p class="delivery-message">' . $shipping_text . '</p>';
}
}
}
}
Any advice is highly appreciated.
Just inside the hook to display message check if cart empty
global $woocommerce;
if ( WC()->cart->get_cart_contents_count() != 0 ) {
// other codes here
}

Woocommerce custom product search in product category

I am using a custom woocommerce product search in my wordpress site where it searchs only in product titles. I would like to include the search in category names too. So if someone search a text, and if that's exists in product category name, all those products under that category also need to be listed.
Here is code I am using:
function wc_filter_search_by_title_only( $search, &$wp_query ) {
global $wpdb;
$not_allowed_post_types = apply_filters( 'wc_filter_search_not_allowed_array', array(
'product', //Default WooCommerce products post type
'shop_webhook', //MyStyle Custom post type
) );
if ( empty( $search ) || ! in_array( $wp_query->query_vars['post_type'], $not_allowed_post_types ) ) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search =
$searchand = '';
foreach ( (array) $q['search_terms'] as $term ) {
$term = esc_sql( $wpdb->esc_like( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = '') ";
}
return $search;
}
add_filter( 'posts_search', 'wc_filter_search_by_title_only', 500, 2 );
Kindly let me know if anyway I can implement this.
Thank you in advance.
The easiest way really is to use SearchWP with its WooCommerce extension. Besides, it gives you a lot more functionality than just that.

Resources