Search WooCommerce orders admin for custom fields - php

Probably a very simple question but it leaves me clueless..
On the web if found the following code for searching a custom field on the order page. What If I wanted to scope another field?
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields ) {
$search_fields[] = 'woochimp_field';
return $search_fields;
}
Thanks

$search_fields[] Is an Array as it turns out. Every time you use $search_fields[] = 'woochimp_field'; you add to the array and makes you able to search more custom fields.
add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
function woocommerce_shop_order_search_order_total( $search_fields ) {
$search_fields[] = 'woochimp_field';
$search_fields[] = 'woochimp_field_2'; //example
return $search_fields;
}

Related

Woocommerce - Include custom user meta in order 'filter by registered customer' Ajax search

I am trying to include a users custom meta named 'sageaccountnumber' within the 'filter by registered customer' section of the WooCommerce orders list as shown below:
I already have the custom meta field named 'sageaccountnumber' working with the following PHP code:
add_action( 'personal_options_update', 'sab_save_sageaccount_user_profile_fields' );
add_action( 'edit_user_profile_update', 'sab_save_sageaccount_user_profile_fields' );
function sab_save_sageaccount_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'sageaccountnumber', $_POST['sageaccountnumber'] );
}
When searching for a registered customer I would like to include the user meta 'sageaccountnumber' within the search and display matching results.
I understand this uses AJAX within the file class-wc-ajax.php. This is the function in question: https://wp-kama.com/plugin/woocommerce/function/WC_AJAX::json_search_customers
I do not know alot about AJAX and I have not been able to find a way to include a custom user meta value in this search. I have not found anyone else doing this.
Any guidance or suggestions would be much appreciated? Thank you.
After getting lost with trying to understand the class-wc-ajax.php file, I completely missed a more obvious and simple solution. Here is the code I am using which works perfectly. I hope this helps others looking for similar solution.
This filter-hook allows you to add an additional meta_query to the existing search parameters. This will also allow your custom user meta to display in AJAX searches from the Orders & Subscriptions pages.
If you have a custom meta field setup for your users, simply change 'sageaccountnumber' to the name of your meta and it will be included in the AJAX search results.
add_filter ('woocommerce_customer_search_customers', 'sab_sageaccount_order_subscription_search', 10, 4);
function sab_sageaccount_order_subscription_search ($filter, $term, $limit, $type){
if ($type == 'meta_query'){ $filter['meta_query'][] = array('key' => 'sageaccountnumber', 'value' => $term, 'compare' => 'LIKE');
}
return $filter;
}
If you need to include the custom meta field in the normal users search, you can use the following code, again changing 'sageaccountnumber' to your custom meta name.
add_action( 'pre_user_query', 'sab_sageaccount_user_search' );
function sab_sageaccount_user_search( $query ) {
global $wpdb;
global $pagenow;
if (is_admin() && 'users.php' == $pagenow) {
if( empty($_REQUEST['s']) ){return;}
$query->query_fields = 'DISTINCT '.$query->query_fields;
$query->query_from .= ' LEFT JOIN '.$wpdb->usermeta.' ON '.$wpdb->usermeta.'.user_id = '.$wpdb->users.'.ID';
$query->query_where = "WHERE 1=1 AND (user_login LIKE '%".$_REQUEST['s']."%' OR ID = '".$_REQUEST['s']."' OR (meta_value LIKE '%".$_REQUEST['s']."%' AND meta_key = 'sageaccountnumber'))";
}
return $query;
}

pull out data from database and display in Gravity form

I am using gravity forms plugin, and I'm trying to display the categories as a drop down list in the form I have already created.
If required, please here's a link to my website
I've been on this for too long, and no way out. Kindly help me out.
add_filter( 'gform_pre_render_1', 'populate_categories' );
add_filter( 'gform_pre_validation_1', 'populate_categories' );
add_filter( 'gform_pre_submission_filter_1', 'populate_categories' );
add_filter( 'gform_admin_pre_render_1', 'populate_categories' );
function populate_categories( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->id != 1 ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts)
$categories = get_categories ;
$choices = array();
foreach ( $categories as $categories ) {
$choices[] = array( 'text' => $categories->name, 'value' => $categories->name );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Category';
$field->choices = $choices;
}
return $form;
}
You can dynamically generate drop downs for gravity forms using the syntax provided below in this link. You have to take control over the functions.php file of the theme to retrieve your output as per your requirement.
Here is the clear documentation provided and you can create in a simple manner using this methods. There are two methods available refer to it.
https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/
If you face any problem with creation let me know we shall solve it.

Modify WooCommerce product columns

I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}

Wordpress updating screen_layout_post user meta for 1 column layout

I am trying to get Wordpress to show a 1 column layout when adding a new post using the following code in my functions file.
function wpsnippy_one_columns_posts_layout( $columns ) {
$columns['vehicle'] = 1;
return $columns; } add_filter( 'screen_layout_columns', 'wpsnippy_one_columns_posts_layout' ); function wpsnippy_screen_layout_posts() {
return 1; } add_filter( 'get_user_option_screen_layout_post', 'wpsnippy_screen_layout_posts' );
This is working fine if logged in as admin, however I have a user type 'seller' and it is not working for seller, I actually need it to be the other way around, 1 column if seller 2 if anything else.
Your input will be greatly appreciated, many thanks.
We can filter the function get_user_option() and force 1 column:
add_filter( 'get_user_option_screen_layout_post', function( $result, $option, $user )
{
if( in_array( 'seller', $user->roles ) )
$result = '1';
return $result;
}, 10, 3 );
Note that the option is defined like: "screen_layout_$page".

Modify WooCommerce Is_Purchasable

I was working on implementing my own pre-order system, where I set a is_preorder custom field for each product.
I was trying to modify the WooCommerce's Is_Purchasable option so that, if the product has pre-order status and it's already passed the pre-order deadline, it shouldn't be able to be purchased. I've tried a bunch of ways, but nothing seems working.
Here's something that I did (rough idea)
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable');
function preorder_is_purchasable() {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder');
if($is_preorder && "not yet passed deadline")
return true;
else
return false;
}
I don't just wanna disable the add_to_cart button, I also want to disable the functionality (should prompt error if user tried to add product by hardcoding in url).
How should I go on with this?
===========================================================================
Here's my final code:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
// if product is Pre-Order
if($is_preorder)
{
$today = date('Ymd');
// another field added using 'Advance Custom Fields' plugin
$preorder_deadline = get_field('preorder_deadline', $object->id);
if($today <= $preorder_deadline) // if not yet pass deadline
return true;
else
return false;
}
else
return $is_purchasable; // normal
Update 2019: please see dev_masta answer for correct solution nowadays.
Not sure if it solves the issue as this has to be tested on your own custom set up. But you're using get_field wrong: if it is not used inside a Loop, you should provide the post ID.
Analyzing the filter woocommerce_is_purchasable, we see that it takes two parameters, a boolean (is_purchasable) and an object (WC_Product).
Try this:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
if($is_preorder && $is_purchasable)
return true;
else
return false;
}
The accepted answer is a bit outdated today.
Instead of using $object->id you should use $object->get_id(), otherwise you'll get a PHP notice about incorrect use.
function disable_purchased_products( $is_purchasable, $object ){
// custom function to get the array of purchased products ID's
$already_purchased = get_purchased_products();
if( in_array( $object->get_id(), $already_purchased ) ){
return false;
} else {
return $is_purchasable;
}
}
add_filter( 'woocommerce_is_purchasable', 'disable_purchased_products', 10, 2 );
I hope this will help someone, I've seen this (outdated) code all around the net..

Categories