Woocommerce /按价格分类相关产品

时间:2018-09-18 17:28:00

标签: sorting woocommerce

我将此过滤器添加到functions.php中,但不幸的是它不起作用。可能有人请帮忙找出原因

    add_filter( 'woocommerce_grouped_children_args', 'custom_grouped_children_args', 18 );
function custom_grouped_children_args( $args ){
    $args = array(
    'post_type' => 'product',
    'posts_per_page' => 4,
    'orderby'   => 'meta_value_num',
    'meta_key'  => '_price',
    'order' => 'desc'
    );
}

1 个答案:

答案 0 :(得分:1)

只是找出解决方案,以防有人需要。我只需要删除默认过滤器,然后添加我的

function custom_remove_hook(){
    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    add_action( 'woocommerce_after_single_product_summary', 'custom_function_to_sort_related', 22 );
}
add_action( 'woocommerce_after_single_product_summary', 'custom_remove_hook' );

function custom_function_to_sort_related( $args = array() ) {
        global $product;

        if ( ! $product ) {
            return;
        }

        $defaults = array(
            'posts_per_page' => 4,
            'columns'        => 4,
            'orderby'        => 'price', // @codingStandardsIgnoreLine.
            'order'          => 'desc'
        );

        $args = wp_parse_args( $args, $defaults );

        // Get visible related products then sort them at random.
        $args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );

        // Handle orderby.
        $args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );

        // Set global loop values.
        wc_set_loop_prop( 'name', 'related' );
        wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );

        wc_get_template( 'single-product/related.php', $args );
    }