根据分配的分类术语按商店经理过滤 WooCommerce 订单
Filter WooCommerce orders by shop manager based on assigned taxonomy terms
我想将带有特定产品标签的订单分配给后端的特定商店经理,并隐藏其余订单。
我目前正在使用 Order Splitter for WooCommerce 免费插件将订单按项目(产品)拆分为单独的订单。
我遇到了这个使用产品类型过滤订单的非常好的解决方案: 我试图对其进行编辑以仅显示特定的产品标签,然而,没有运气。我做错了什么?
产品标签名为“brand1”,店长帐号 ID 为 7。
add_action( 'restrict_manage_posts', 'admin_shop_order_by_product_type_filter' );
function admin_shop_order_by_product_type_filter(){
global $pagenow, $post_type;
$whoisin = get_current_user_id();
if( 'shop_order' === $post_type && 'edit.php' === $pagenow && ($whoisin == 7) ) {
$domain = 'woocommerce';
$filter_id = 'brand1';
$current = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
$query_args = ['taxonomy' => 'product_tag', 'fields' => 'names', 'orderby' => 'order'];
echo '<select name="'.$filter_id.'">
<option value="">' . __('Filter by Product Tag', $domain) . '</option>';
foreach ( get_terms($query_args) as $term_name ) {
printf( '<option value="%s"%s>%s</option>', $term_name,
$term_name === $current ? '" selected="selected"' : '', ucfirst($term_name) );
}
echo '</select>';
}
}
add_action( 'pre_get_posts','process_admin_shop_order_product_type_filter' );
function process_admin_shop_order_product_type_filter( $query ) {
global $pagenow, $post_type, $wpdb;
$filter_id = 'brand1';
$whoisin = get_current_user_id();
if ( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $post_type
&& isset( $_GET[$filter_id] ) && $_GET[$filter_id] != '' && ($whoisin == 7)) {
$order_ids = $wpdb->get_col( "
SELECT DISTINCT o.ID
FROM {$wpdb->prefix}posts o
INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
ON oi.order_id = o.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
ON oi.order_item_id = oim.order_item_id
INNER JOIN {$wpdb->prefix}term_relationships tr
ON oim.meta_value = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t
ON tt.term_id = t.term_id
WHERE o.post_type = '$post_type'
AND oim.meta_key = '_product_id'
AND tt.taxonomy = 'product_tag'
AND t.name = '{$_GET[$filter_id]}'
");
$query->set( 'post__in', $order_ids ); // Set the new "meta query"
$query->set( 'posts_per_page', 25 ); // Set "posts per page"
$query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
}
}
以下更改后的代码将根据分配给商店经理(用户 ID)的相关产品标签显示订单。
在第一个函数中,您将通过商店经理(用户 ID)分配产品标签。然后,每个商店经理的订单将被不同地过滤,只显示他们分配的订单。只有定义的用户 ID 才会过滤订单。
代码:
// Settings function
function orders_by_shop_manager_settings(){// Your settings below
// Your settings below
return array(
// Terms field type (can be 'name', 'slug' or 'term_id')
'field' => 'slug',
// The taxonomy for product tags (brands)
'taxonomy' => 'product_tag',
// Manager ID / product tags terms pairs (set the correct term field type as defined before)
'assignments' => array(
'7' => array('brand1','brand7', 'brand9'),
'12' => array('brand3','brand5', 'brand6', 'brand8'),
'189' => array('brand2','brand4'),
)
);
}
// Filtering orders by shop manager based on items product tag(s)
add_action( 'pre_get_posts','filter_orders_by_shop_manager' );
function filter_orders_by_shop_manager( $query ) {
global $pagenow, $typenow, $wpdb;
if ( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $typenow ) {
// Loading settings data
$data = orders_by_shop_manager_settings();
$user_id = get_current_user_id();
}
if( isset($data) && isset($data['assignments'][$user_id]) ) {
$taxonomy = $data['taxonomy'];
$field = $data['field'];
$terms = $data['assignments'][$user_id];
$order_ids = $wpdb->get_col( "
SELECT DISTINCT o.ID
FROM {$wpdb->prefix}posts o
INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
ON oi.order_id = o.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
ON oi.order_item_id = oim.order_item_id
INNER JOIN {$wpdb->prefix}term_relationships tr
ON oim.meta_value = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t
ON tt.term_id = t.term_id
WHERE o.post_type = '$typenow'
AND oim.meta_key IN ( '_product_id','_variation_id' )
AND tt.taxonomy = '$taxonomy'
AND t.$field IN ( '" . implode( "','", $terms ) . "' )
");
if ( count($order_ids) > 0 ) {
$query->set( 'post__in', $order_ids ); // Set the new "meta query"
$query->set( 'posts_per_page', 25 );
} else {
$query->set( 'author__in', 99999999 );
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
我想将带有特定产品标签的订单分配给后端的特定商店经理,并隐藏其余订单。
我目前正在使用 Order Splitter for WooCommerce 免费插件将订单按项目(产品)拆分为单独的订单。
我遇到了这个使用产品类型过滤订单的非常好的解决方案:
产品标签名为“brand1”,店长帐号 ID 为 7。
add_action( 'restrict_manage_posts', 'admin_shop_order_by_product_type_filter' );
function admin_shop_order_by_product_type_filter(){
global $pagenow, $post_type;
$whoisin = get_current_user_id();
if( 'shop_order' === $post_type && 'edit.php' === $pagenow && ($whoisin == 7) ) {
$domain = 'woocommerce';
$filter_id = 'brand1';
$current = isset($_GET[$filter_id])? $_GET[$filter_id] : '';
$query_args = ['taxonomy' => 'product_tag', 'fields' => 'names', 'orderby' => 'order'];
echo '<select name="'.$filter_id.'">
<option value="">' . __('Filter by Product Tag', $domain) . '</option>';
foreach ( get_terms($query_args) as $term_name ) {
printf( '<option value="%s"%s>%s</option>', $term_name,
$term_name === $current ? '" selected="selected"' : '', ucfirst($term_name) );
}
echo '</select>';
}
}
add_action( 'pre_get_posts','process_admin_shop_order_product_type_filter' );
function process_admin_shop_order_product_type_filter( $query ) {
global $pagenow, $post_type, $wpdb;
$filter_id = 'brand1';
$whoisin = get_current_user_id();
if ( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $post_type
&& isset( $_GET[$filter_id] ) && $_GET[$filter_id] != '' && ($whoisin == 7)) {
$order_ids = $wpdb->get_col( "
SELECT DISTINCT o.ID
FROM {$wpdb->prefix}posts o
INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
ON oi.order_id = o.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
ON oi.order_item_id = oim.order_item_id
INNER JOIN {$wpdb->prefix}term_relationships tr
ON oim.meta_value = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t
ON tt.term_id = t.term_id
WHERE o.post_type = '$post_type'
AND oim.meta_key = '_product_id'
AND tt.taxonomy = 'product_tag'
AND t.name = '{$_GET[$filter_id]}'
");
$query->set( 'post__in', $order_ids ); // Set the new "meta query"
$query->set( 'posts_per_page', 25 ); // Set "posts per page"
$query->set( 'paged', ( get_query_var('paged') ? get_query_var('paged') : 1 ) ); // Set "paged"
}
}
以下更改后的代码将根据分配给商店经理(用户 ID)的相关产品标签显示订单。
在第一个函数中,您将通过商店经理(用户 ID)分配产品标签。然后,每个商店经理的订单将被不同地过滤,只显示他们分配的订单。只有定义的用户 ID 才会过滤订单。
代码:
// Settings function
function orders_by_shop_manager_settings(){// Your settings below
// Your settings below
return array(
// Terms field type (can be 'name', 'slug' or 'term_id')
'field' => 'slug',
// The taxonomy for product tags (brands)
'taxonomy' => 'product_tag',
// Manager ID / product tags terms pairs (set the correct term field type as defined before)
'assignments' => array(
'7' => array('brand1','brand7', 'brand9'),
'12' => array('brand3','brand5', 'brand6', 'brand8'),
'189' => array('brand2','brand4'),
)
);
}
// Filtering orders by shop manager based on items product tag(s)
add_action( 'pre_get_posts','filter_orders_by_shop_manager' );
function filter_orders_by_shop_manager( $query ) {
global $pagenow, $typenow, $wpdb;
if ( $query->is_admin && 'edit.php' === $pagenow && 'shop_order' === $typenow ) {
// Loading settings data
$data = orders_by_shop_manager_settings();
$user_id = get_current_user_id();
}
if( isset($data) && isset($data['assignments'][$user_id]) ) {
$taxonomy = $data['taxonomy'];
$field = $data['field'];
$terms = $data['assignments'][$user_id];
$order_ids = $wpdb->get_col( "
SELECT DISTINCT o.ID
FROM {$wpdb->prefix}posts o
INNER JOIN {$wpdb->prefix}woocommerce_order_items oi
ON oi.order_id = o.ID
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
ON oi.order_item_id = oim.order_item_id
INNER JOIN {$wpdb->prefix}term_relationships tr
ON oim.meta_value = tr.object_id
INNER JOIN {$wpdb->prefix}term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
INNER JOIN {$wpdb->prefix}terms t
ON tt.term_id = t.term_id
WHERE o.post_type = '$typenow'
AND oim.meta_key IN ( '_product_id','_variation_id' )
AND tt.taxonomy = '$taxonomy'
AND t.$field IN ( '" . implode( "','", $terms ) . "' )
");
if ( count($order_ids) > 0 ) {
$query->set( 'post__in', $order_ids ); // Set the new "meta query"
$query->set( 'posts_per_page', 25 );
} else {
$query->set( 'author__in', 99999999 );
}
}
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。