在后台订单列表中按id或sku扩展产品项目的搜索



我正试图使用Wooccommerce订单管理页面中按订单项目SKU或ID搜索的以下代码,以实现按天空和ID搜索Wooccommence订单,SKU是我的重要组成部分。

add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$posts = get_posts(array('post_type' => 'shop_order'));
foreach ($posts as $post) {
$order_id = $post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach($items as $item) {
$product_id = $item['product_id'];
$search_sku = get_post_meta($product_id, "_sku", true);
add_post_meta($order_id, "_product_sku", $sku);
add_post_meta($order_id, "_product_id", $product_id);
}
}
return array_merge($search_fields, array('_product_sku', '_product_id'));
});

当我将此添加到我的functions.php时,我会得到以下错误:

Array() expects parameter 1 to be a valid callback, function 'woocommerce_shop_order_search_order_total' not found or invalid function name in /var/sites/s/silverfx.co.uk/public_html/wp-includes/plugin.php on line 235
Warning: array_merge(): Argument #1 is not an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/themes/SilverFx-Theme/functions.php on line 156
Warning: array_map(): Argument #2 should be an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1533
Warning: array_map(): Argument #2 should be an array in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1557
Warning: implode(): Invalid arguments passed in /var/sites/s/silverfx.co.uk/public_html/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-post-types.php on line 1557

我假设,由于这是大约1年前的事了,而且wooccommerce在那段时间里经历了一些大的变化,这个代码需要以某种方式更新,但我对wooccommence的经验不足,无法识别出问题所在。

如果有人能够确认我走在正确的道路上,或者就我可能需要做什么提供指导/建议,那将是非常棒的。

感谢

更新:与WooCommerce 3+的兼容性

woocommerce_shop_order_search_fields过滤器挂钩扩展后端订单列表中的搜索时,您需要添加一些post_meta字段,用于基于id或sku的订单项搜索。

作者在产品skuadd_post_meta()中犯了一个错误。您需要将未定义的$sku变量替换为$search_sku,我认为。

更新:经过对这段代码中使用的wordpress函数的思考和搜索,我认为我已经找到了解决方案。

以下是与add_post_meta()函数问题相关的更新代码:

add_filter( 'woocommerce_shop_order_search_fields', function ($search_fields ) {
$orders = get_posts( array( 'post_type' => 'shop_order' ) );
foreach ($orders as $order_post) {
$order_id = $order_post->ID;
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach( $order->get_items() as $item_id => $item_values ) {
if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
$product_id = $item_values['product_id'];
} else {
$product_id = $item_values->get_product_id();
}
$search_sku = get_post_meta($product_id, "_sku", true);
add_post_meta($order_id, "_product_id", $product_id, true); //  <= ## Here ##
add_post_meta($order_id, "_product_sku", $search_sku, true); // <= ## Here ##
}
}
return array_merge($search_fields, array('_product_id', '_product_sku'));
} );

缺少一个可选参数,该参数指定在设置为true时不是数组。

现在应该可以了。

参考:

  • 在Woocommerce订单管理页面中按订单项目SKU或ID搜索

  • https:WP codex add_post_meta()

您可以在搜索结果过滤器中执行附加查询。此代码还将搜索产品类元属性等

function woocommerce_shop_order_search_results_with_product_info( $order_ids, $term, $search_fields ) {
global $wpdb;
if ( ! empty( $search_fields ) ) {
$order_ids = array_unique(
array_merge(
$order_ids,
$wpdb->get_col(
$wpdb->prepare(
"SELECT DISTINCT order_id
FROM kn_woocommerce_order_items
RIGHT JOIN (
SELECT kn_woocommerce_order_itemmeta.order_item_id, kn_postmeta.meta_value
FROM kn_woocommerce_order_itemmeta 
LEFT JOIN kn_postmeta ON kn_woocommerce_order_itemmeta.meta_value = kn_postmeta.post_id
WHERE kn_woocommerce_order_itemmeta.meta_key = '_product_id' AND kn_postmeta.meta_value LIKE %s
) AS order_items ON order_items.order_item_id = kn_woocommerce_order_items.order_item_id",
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
)
)
);
}
return $order_ids; 
}
add_filter( 'woocommerce_shop_order_search_results', 'woocommerce_shop_order_search_results_with_product_info', 999, 3 );

最新更新