We are using Advanced Custom Fields, WP All Export Pro, and WordPress with Woocommerce.
I have an ACF field created for our accountant in users area of WordPress called "traverse_customer_id". This is for her to know what customer this order is for.
I have this ACF field showing in the orders export but the data comes back blank on each export. I think it's because I'm not relating the order with the customer or user ID. I've attempted to use a code snippet from WP All Exports' support without any luck. Could anyone with knowledge help me see what I'm doing wrong?
This is what I have in the "Custom export field" area: (I think this is running the function on each line of the import.)
[sf_helper_meta_query_lookup("traverse_customer_id",{Customer User ID},"user")]
Then I have this below in the Function Editor:
function sf_helper_meta_query_lookup( $meta_key = '', $meta_value = '', $object_type = 'post', $taxonomy = '', $return_field = 'ID', $compare = '=', $custom_args = array() ) {
if ( empty( $object_type ) || empty( $meta_key ) || empty( $meta_value ) ) return;
$func = 'get_posts';
switch ( $object_type ) {
case 'user':
$func = 'get_users';
break;
case 'term':
$func = 'get_terms';
if ( $return_field == 'ID' ) {
$return_field = 'term_id';
}
break;
default:
$func = 'get_posts';
break;
}
if ( ! empty( $custom_args ) ) {
$objects = $func( $custom_args );
if ( ! empty( $objects ) ) {
return $objects[0]->$return_field;
} else {
return false;
}
}
$args = array();
$meta_query = array( array(
'key' => $meta_key,
'value' => $meta_value,
'compare' => $compare
) );
if ( $func == 'get_terms' ) {
$args = array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'meta_query' => $meta_query
);
} elseif ( $func == 'get_users' ) {
$args = array(
'meta_query' => $meta_query
);
} else {
$args = array(
'post_type' => $object_type,
'meta_query' => $meta_query
);
}
if ( $objects = $func( $args ) ) {
return $objects[0]->$return_field;
} else {
return false;
}
}
Any help would be greatly appreciated!
Related
Using the answer from search multiple sku's on woocommerce admin side I adapted my code to replace $sku with the value of the custom field within my product, which is $winner.
When I search by separating product custom fields with a delimiter, it does not work. How can I edit my code so I can search products multiple by custom field?
function woo_multiple_title_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;
$names = explode('|',$search_term);
$meta_query = array(
'relation' => 'OR'
);
if(is_array($winners) && $winners) {
foreach($winners as $winner) {
$meta_query[] = array(
'key' => '_winner',
'value' => $winner,
'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_title_search', 20 );
I have a customer service area where the user can view orders from the front-end (if user has shop_manager role).
The orders are retrieved via the wc_get_orders function. I am attempting to sort these orders, via ajax from this page, but it seems that the order query is being cached? I can get one ajax sort to happen, but then that seems to be cached and can't be bypassed. I could be wrong, but I'm using basically the exact same setup with retrieving/sorting users, using WP_User_Query, and it will sort all day.
User orders endpoint
add_action( 'woocommerce_account_user-orders_endpoint', 'child_account_user_orders_endpoint_template' );
function child_account_user_orders_endpoint_template() {
$user_id = get_current_user_id();
if ( $user_id <= 0 ) {
return;
}
$args = array();
$qvar = get_query_var( 'only_completed', 0 );
$qvar = $qvar === 0 ? false : true;
if ( $qvar === true ) {
$args['status'] = array( 'wc-completed' );
$args['only_completed'] = true;
}
wc_get_template( 'myaccount/form-user-orders.php', $args );
}
form-user-orders.php
...
// Orders statuses
$statuses = wc_get_order_statuses();
unset( $statuses['wc-completed'], $statuses['wc-cancelled'] );
$orderby = 'date';
$order = 'DESC';
$default_args = array(
'status' => array_keys( $statuses ),
'limit' => $per_page,
'paginate' => true,
'paged' => $paged,
'date_after' => $date,
'orderby' => $orderby,
'order' => $order
);
$args = ! empty( $args ) ? $args : null;
$args = ( null !== $args ) ? array_merge( $default_args, $args ) : $default_args;
$results = wc_get_orders( $args );
Ajax to happen after clicking sort arrows
$.ajax({
type: 'post',
url: localized.ajax_url,
data: {
'action': 'user_management_sorting',
'orderby': orderBy,
'order': order,
'only_completed': only_completed,
'endpoint': shop_managers_form.managersForm.data( 'endpoint' )
},
dataType: 'json',
beforeSend: function(response){
shop_managers_form.managersForm.find( 'th.sortable' ).removeClass( 'sorted' );
shop_managers_form.globalAjaxLoader.fadeIn(400);
},
complete: function(response){
shop_managers_form.globalAjaxLoader.fadeOut(400);
},
success: function(response){
console.log( response );
shop_managers_form.managersForm.find( '.response-area' ).html('').html( response.html );
$( '#' + sortingHeader.attr( 'id' ) ).addClass( 'sorted' ).attr( 'data-order', shop_managers_form.next_order( order ) );
}
});
Ajax action
add_action( 'wp_ajax_user_management_sorting', 'child_user_management_sorting_response' );
add_action( 'wp_ajax_nopriv_user_management_sorting', 'child_user_management_sorting_response' );
function child_user_management_sorting_response() {
ob_start();
$orderby = $_POST['orderby'];
$order = ! empty( $_POST['order'] ) ? $_POST['order'] : 'ASC';
$only_completed = json_decode( $_POST['only_completed'] );
$base = wc_get_endpoint_url( $_POST['endpoint'], '', get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
$args = array(
'orderby' => $orderby,
'order' => $order,
'from_ajax' => true,
'base_url' => $base
);
if ( $only_completed === true ) {
$args['status'] = array( 'wc-completed' );
$args['only_completed'] = true;
}
if ( 'user-management' === $_POST['endpoint'] ) {
$uses_meta = array( '_quickbooks_account_no', 'billing_company', 'authorized_user_status' );
if ( in_array( $orderby, $uses_meta ) ) {
$args['orderby'] = 'meta_value';
$args['meta_key'] = $orderby;
}
}
wc_get_template( 'myaccount/form-' . $_POST['endpoint'] . '.php', $args );
$html = ob_get_clean();
if ( ! empty( $html ) ) {
$message = 'success';
} else {
$html = '';
$message = 'failure';
}
echo json_encode( array(
'message' => $message,
'html' => $html
));
wp_die();
}
I can make things semi-work by accessing these straight from the database via $wpdb->get_results() but I don't really want to figure out the whole raw sql for joining all the tables, then trying to access all the order info that I need this way..
Any ideas to get around the caching of wc_get_orders or alternative methods to accomplish this?
I wanted to use the default search functionality using this :
The problem is I've made this table using this line of code
function waiting_list_column_content_callback($column_name, $post_ID) {
$guestID = get_post_meta( $post_ID, 'guest', true );
$user_info = get_userdata( $guestID );
$floorplanID = get_post_meta( $post_ID, 'floorplan', true );
$notes = get_post_meta( $post_ID, 'notes', true );
$waiting_list = wpr_get_waiting_list( $floorplanID );
$list_location = wpr_get_list_location( $post_ID, wpr_get_waiting_list( $floorplanID ) );
if ( $column_name == 'fname' ) {
echo ''.$user_info->first_name.'';
}elseif ( $column_name == 'lname' ) {
echo ''.$user_info->last_name.'';
}elseif( $column_name == 'floorplan' ){
echo get_the_title($floorplanID);
}
elseif( $column_name == 'logs_print' ){
echo 'Print';
}elseif( $column_name == 'list_location' ){
echo $list_location;
}
}
I've tried this and this. It work, but I can only search the user by "id".Since "id" is the only custom_field available to use.
This is the code I'm using to get results by id. "guest" is the "id" of the user. Which I use to display the last name and the first name of the user.
function extend_admin_search( $query ) {
//Extend search for document post type
$post_type = 'wpr_waiting_list';
// Custom fields to search for
$custom_fields = array(
"guest",
);
if( ! is_admin() )
return;
if ( $query->query['post_type'] != $post_type )
return;
$search_term = $query->query_vars['s'];
// Set to empty, otherwise it won't find anything
$query->query_vars['s'] = '';
if ( $search_term != '' ) {
$meta_query = array( 'relation' => 'OR' );
foreach( $custom_fields as $custom_field ) {
array_push( $meta_query, array(
'key' => $custom_field,
'value' => $search_term,
'compare' => 'LIKE'
));
}
$query->set( 'meta_query', $meta_query );
};
}
add_action( 'pre_get_posts', 'extend_admin_search' );
What I've wanted to do was search this custom post type by "first name" or "last name" .
I've tried some sql codes but I'm getting some "headers" error I don't understand.
I have a category with products not visible to everyone.
I already have a script in place, that removes this category from the sidebar widget, when no items in the category are visible to the user.
I created a function that puts the exclude term_id in a global variable.
Now I need something that excludes them from view in the shop.
$GLOBALS['cat_exclude'] = NULL;
function getExcludedCats( ) {
//if( ! is_admin() && (is_product_category() || is_shop())){
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$parentid = $term->term_id;
$args = array(
'hide_empty' => true,
'parent' => $parentid
);
$product_categories = get_terms( 'product_cat', $args );
$exclude = array();
foreach ( $product_categories as $category ) {
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new WC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
if ( false === $show_category ) {
$exclude[] = $category->term_id;
}
}
if ( ! empty( $exclude ) ) {
$GLOBALS['cat_exclude'] = implode( ',', $exclude );
}
//}
}
add_action('wp_head', 'getExcludedCats');
However, in the shop view itself, the category is still visible. How can I remove it there, when no items in this category are visible to the user.
I have tried:
https://gist.github.com/rynaldos/a9d357b1e3791afd9bea48833ff95994
But it removes the category ALWAYS, in both widget and shop.
Products are being displayed by group membership of the customer:
https://wordpress.org/plugins/groups/
I think I managed to create a script that made this possible:
https://gist.github.com/DarkAllMan/cffb114eb97c6f26882e54793e023587
<?php
/**
Plugin Name: WooCommerce - Hide categories where no products are visible to user
Plugin URI: https://www.randall.nl
Description: Excludes categories with no visible products from the WooCommerce category overview in shop
Version: 0.1
Author: Randall Kam
Author URI: https://www.randall.nl
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( !class_exists( 'ExcludeCats' ) ) :
class ExcludeCats {
public $version = '0.1',
$exclude = array();
protected static $_instance = null;
/**
* Main Plugin Instance
*
* Ensures only one instance of plugin is loaded or can be loaded.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*/
public function __construct() {
// CHECK CATEGORIES FOR VISIBLE PRODUCTS
add_action('wp_head', array( $this, 'get_excluded_cats' ), 10, 3 );
// ADD THE WIDGET SIDEBAR FILTER
add_filter( 'woocommerce_product_categories_widget_args', array( $this, 'kfg_exclude_categories_from_widget'), 10, 1 );
// ADD THE SHOP FILTER
add_filter( 'woocommerce_product_subcategories_args', array( $this, 'filter_woocommerce_product_subcategories_args'), 10, 1 );
}
// GET CATEGORIES WITH NO VISIBLE PRODUCTS AND PUT IN GLOBAL IF GLOBAL FALSE
public function get_excluded_cats( $terms, $taxonomies, $args ) {
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$args = array(
'parent' => $term_id,
'hide_empty' => false,
'hierarchical' => false,
);
$product_categories = get_terms( 'product_cat', $args );
// if a product category and on the shop page
//if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
if ( ! is_admin() && is_shop() ) {
foreach ( $product_categories as $key => $term ) {
unset($this->exclude);
if($term->taxonomy=='product_cat'){
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $term->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new WC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
}
if ( false === $show_category ) {
$this->exclude[] = $term->term_id;
}
}
}
}
public function get_parent_cats ($cat_termid, $found = array()) {
array_push ($found, $cat_termid);
$term =get_term_by( 'term_id', $cat_termid, 'product_cat');
if($term->parent > 0){
return get_parent_cats($term->parent, $found);
}
return $found;
}
// ADD FILTERS FOR CATEGORIES AND EXCLUDE EMPTY
public function filter_woocommerce_product_subcategories_args( $temp_args = array() ) {
$current_tax = get_query_var( 'product_cat' );
$term =get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$temp_args = array(
'parent' => $term_id,
'menu_order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'product_cat',
'pad_counts' => 1,
'include' => NULl,
'exclude' => $this->exclude,
);
return $temp_args;
}
public function kfg_exclude_categories_from_widget( $category_list_args ) {
$current_tax = get_query_var( 'product_cat' );
$term = get_term_by( 'slug', $current_tax, 'product_cat');
$term_id = $term->term_id;
$parents = $this->get_parent_cats($term_id);
$args = array(
'hide_empty' => false,
'hierarchical' => true,
);
$product_categories = get_terms( 'product_cat', $args );
$wexclude = array();
foreach ( $product_categories as $category ) {
$posts = get_posts( array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => $category->slug, 'fields' => 'ids' ) );
$show_category = false;
foreach ( $posts as $post ) {
$product = new wC_Product( $post );
$visible_product = $product->is_visible();
if ( true === $visible_product ) {
$show_category = true;
break;
}
}
if ( false === $show_category || ( $category->parent > 0 && !in_array($category->parent,$parents) ) ) {
$wexclude[] = $category->term_id;
}
}
if ( ! empty( $wexclude ) ) {
$category_list_args['exclude'] = implode( ',', $wexclude );
unset( $category_list_args['include'] );
}
return $category_list_args;
}
} // class ExcludeCats
endif; // class_exists
/**
* Returns the main instance of the plugin class to prevent the need to use globals.
*
* #since 2.0
* #return WooCommerce_PostcodeAPInu
*/
function ExcludeCats() {
return ExcludeCats::instance();
}
ExcludeCats(); // load plugin
I have a function that displays the post count of any terms for its taxonomy anywhere i want to show it and it works great but i would like to change this up a bit to completely exclude the post count of any post that has a certain term added to it, let's call that "state". So if a post has "state" added to it, i would like the function to not include the count of that post in any terms within the taxonomy.
function get_city_count( $taxonomy = 'countries', $term = '', $args = [] )
{
if ( !$term )
return false;
if ( $term !== 'all' ) {
if ( !is_array( $term ) ) {
$term = filter_var( $term, FILTER_VALIDATE_INT );
} else {
$term = filter_var_array( $term, FILTER_VALIDATE_INT );
}
}
if ( $taxonomy !== 'countries' ) { $taxonomy = filter_var( $taxonomy, FILTER_SANITIZE_STRING );
if ( !taxonomy_exists( $taxonomy ) ) return false;
}
if ( $args ) { if ( !is_array ) return false; } $defaults = [ 'posts_per_page' => 1, 'fields' => 'ids' ];
if ( $term !== 'all' ) { $defaults['tax_query'] = [ [ 'taxonomy' => $taxonomy, 'terms' => $term ] ]; }
$combined_args = wp_parse_args( $args, $defaults );
$q = new WP_Query( $combined_args );
return $q->found_posts;
}