Updated:与 WooCommerce 3+ 的兼容性
While woocommerce_shop_order_search_fields
过滤钩子扩展后端订单列表中的搜索,您需要添加一些 post_meta 字段,用于基于 id 或 sku 的订单项搜索。
作者有犯了一个错误产品单品 add_post_meta()
。您需要替换未定义的$sku
变量由$search_sku
反而,I think.
Update:经过一番思考和对这段代码片段中使用的 WordPress 函数的一些搜索后,我想我已经找到了解决方案。
这是更新后的相关代码添加_post_meta() 函数 https://codex.wordpress.org/Function_Reference/add_post_meta issue:
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'));
} );
缺少可选参数,该参数指定value不是一个数组,当设置为true
.
现在应该可以了。
参考:
在 Woocommerce 订单管理页面中按订单商品 SKU 或 ID 搜索 https://stackoverflow.com/questions/28063920/search-by-order-item-sku-or-id-in-woocommerce-orders-admin-page/28718266#28718266
https: WP 法典 add_post_meta() https://codex.wordpress.org/Function_Reference/add_post_meta