按 id 或 sku 扩展后端订单列表中的产品项目搜索
Extending search in backend orders list for product items by id or by sku
我正在尝试使用 中的以下代码来启用按天空和 ID 搜索 woocommerce 订单,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 年的历史了,而且 woocommerce 在那段时间经历了一些重大变化,所以这段代码需要以某种方式更新,但是我对 woocommerce 的经验不足,无法识别出了什么问题。
如果有人能够确认我在正确的道路上,或者就我可能需要做什么提供指导/建议,那就太好了。
谢谢
Updated: compatibility with WooCommerce 3+
虽然 woocommerce_shop_order_search_fields
过滤器钩子在后端订单列表中扩展搜索,但您需要添加一些 post_meta 字段以根据 id 或 sku 搜索订单商品。
作者 在 产品 sku add_post_meta()
中犯了一个错误。您需要用 $search_sku
替换未定义的 $sku
变量,我认为.
更新: 经过一番思考并搜索了此代码段中使用的 wordpress 函数后,我想我找到了解决方案。
这是与 to add_post_meta() function 问题相关的更新代码:
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
.
时指定 value 不是数组
现在应该可以了。
参考:
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 );
我正在尝试使用
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 年的历史了,而且 woocommerce 在那段时间经历了一些重大变化,所以这段代码需要以某种方式更新,但是我对 woocommerce 的经验不足,无法识别出了什么问题。
如果有人能够确认我在正确的道路上,或者就我可能需要做什么提供指导/建议,那就太好了。
谢谢
Updated: compatibility with WooCommerce 3+
虽然 woocommerce_shop_order_search_fields
过滤器钩子在后端订单列表中扩展搜索,但您需要添加一些 post_meta 字段以根据 id 或 sku 搜索订单商品。
作者 在 产品 sku add_post_meta()
中犯了一个错误。您需要用 $search_sku
替换未定义的 $sku
变量,我认为.
更新: 经过一番思考并搜索了此代码段中使用的 wordpress 函数后,我想我找到了解决方案。
这是与 to add_post_meta() function 问题相关的更新代码:
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
.
现在应该可以了。
参考:
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 );