如何根据产品的SKU代码筛选产品?
orders中创建了一个新列,但我很难理解如何搜索它。
我有一个填充了产品SKU的列。如何根据该信息返回搜索结果?
// Add Column
add_filter( \'manage_edit-shop_order_columns\', \'custom_shop_order_column\',11);
function custom_shop_order_column($columns)
{
//add columns
$columns[\'SKU\'] = __( \'SKU\');
return $columns;
}
// Add Column Data
add_action( \'manage_shop_order_posts_custom_column\' , \'custom_orders_list_column_content\', 10, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case \'SKU\' :
$order = wc_get_order( $post_id );
$order_item = $order->get_items();
foreach ( $order_item as $item_id ) {
$product_id = $item_id[\'product_id\'];
$myVarOne = get_post_meta($product_id, \'_sku\', true);
echo $myVarOne;
break;
}
}
}
// Failed search functionality
add_filter( \'woocommerce_shop_order_search_fields\', \'woocommerce_shop_order_search_SKU\' );
function woocommerce_shop_order_search_SKU( $search_fields ) {
$search_fields[] = \'_sku\';
return $search_fields;
}
SO网友:Kiril Climson
我使用了一个插件“WooCommerce按产品过滤订单”,并修改了他们的ID搜索,添加了一个get\\u post\\u meta(),现在可以按ID过滤产品。
插件:https://en-ca.wordpress.org/plugins/woocommerce-filter-orders-by-product/
修改代码:
// Display dropdown
function foa_product_filter_in_order(){
global $typenow, $wpdb;
if ( \'shop_order\' != $typenow ) {
return;
}
$sql="SELECT ID,post_title FROM $wpdb->posts WHERE post_type = \'product\' AND post_status = \'publish\'";
$all_posts = $wpdb->get_results($sql, ARRAY_A);
$values = array();
foreach ($all_posts as $all_post) {
$values[$all_post[\'ID\']] = get_post_meta($all_post[\'ID\'], \'_sku\', true);
}
?>
<select name="foa_order_product_filter">
<option value=""><?php _e(\'All products\', \'foa\'); ?></option>
<?php
$current_v = isset($_GET[\'foa_order_product_filter\'])? $_GET[\'foa_order_product_filter\']:\'\';
foreach ($values as $label) {
printf
(
\'<option value="%s"%s>%s</option>\',
$value,
$value == $current_v? \' selected="selected"\':\'\',
$label
);
}
?>
</select>
<?php
}