Woocommerce - 按管理员中的特色产品过滤

时间:2017-09-28 13:18:57

标签: php woocommerce

自3.0版以来,Woocommerce删除了后端特色产品的排序选项。我在这里尝试了这个解决方案,但它似乎不再有效https://wordpress.stackexchange.com/questions/104537/woocommerce-filter-by-featured-products-in-admin

它创建了不错的下拉过滤器,但遗憾的是没有过滤特色产品。

由于我们有超过2000种产品,因此逐一寻找它们是非常可靠的。有没有办法对我们的特色产品进行排序或过滤?

由于

2 个答案:

答案 0 :(得分:2)

好的,这里的代码似乎正在运作,作为奖励,您可以过滤缺货产品和评级产品

add_action('restrict_manage_posts', 'featured_products_sorting');
function featured_products_sorting() {
    global $typenow;
    $post_type = 'product'; // change to your post type
    $taxonomy  = 'product_visibility'; // change to your taxonomy
    if ($typenow == $post_type) {
        $selected      = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
        $info_taxonomy = get_taxonomy($taxonomy);
        wp_dropdown_categories(array(
            'show_option_all' => __("Show all {$info_taxonomy->label}"),
            'taxonomy'        => $taxonomy,
            'name'            => $taxonomy,
            'orderby'         => 'name',
            'selected'        => $selected,
            'show_count'      => true,
            'hide_empty'      => true,
        ));
    };
}
add_filter('parse_query', 'featured_products_sorting_query');
function featured_products_sorting_query($query) {
    global $pagenow;
    $post_type = 'product'; // change to your post type
    $taxonomy  = 'product_visibility'; // change to your taxonomy
    $q_vars    = &$query->query_vars;
    if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
        $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
        $q_vars[$taxonomy] = $term->slug;
    }
}

答案 1 :(得分:0)

/**
 * Add custom sorting options (Featured)
 */
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'random_list' == $orderby_value ) {
        $args['orderby'] = 'featured';
        $args['order'] = '';
        $args['meta_key'] = '';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['featured'] = 'Featured';
    return $sortby;
}