首先显示带有图像的产品 - WooCommerce 目录订购参数

问题描述 投票:0回答:1

我正在尝试在产品类别页面上创建一个自定义排序选项,该选项将在没有图像的产品之前显示带有图像的产品。

在子主题functions.php中

function custom_woocommerce_images_first_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 ( 'images_first' == $orderby_value ) {
        $args['meta_key'] = '_thumbnail_id';
        $args['orderby'] = 'meta_value';
        $args['order'] = 'DESC';
    }
    return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_images_first_ordering_args' );

function custom_woocommerce_catalog_orderby_images_first( $sortby ) {
    $sortby['images_first'] = 'Default';
    return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby_images_first' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby_images_first' );

这似乎只显示带有图像的产品。不仅如此,如果多个产品共享同一张图片,它只会显示其中一个。

我假设这必须通过元查询来完成,例如:

$args['meta_query'] = array(  'relation' => 'OR', array(
            'orderby' => 'meta_value',
            'order' => 'DESC',
            'key' => '_thumbnail_id',
            'compare' => 'EXISTS',
            //  'value' => '0'       

            ),
            array(
                'orderby' => 'meta_value',
                'order' => 'DESC',
                'key' => '_thumbnail_id',
                'compare' => 'NOT EXISTS',
            )
        );

删除其他参数并仅添加元查询也不起作用。只要

$args['meta_key'] = '_thumbnail_id';

已添加,无图片的产品将被删除。

php sorting woocommerce product catalog
1个回答
0
投票

我的工作完全归功于这篇文章

这是我的示例的工作代码:

function custom_woocommerce_images_first_ordering_args( $q ) {
    $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'images_first' == $orderby_value ) {
        $q->get( 'meta_key', '_thumbnail_id' );
        $q->set('orderby', 'meta_value');
        $q->set('order', 'DESC');
        $q->set( 'meta_query', array(  'relation' => 'OR', array(

            'orderby' => 'meta_value',
            'order' => 'DESC',
            'key' => '_thumbnail_id',
            'compare' => 'NOT EXISTS',
            //  'value' => '0'       

            ),
            array(
                'key' => '_thumbnail_id',
                'compare' => '>=',
                'value' => '0',       
                'orderby' => 'meta_value',
                'order' => 'DESC'
            )),
        ); 
    }
    return $q;
}
add_filter( 'woocommerce_product_query', 'custom_woocommerce_images_first_ordering_args' );

function custom_woocommerce_catalog_orderby_images_first( $sortby ) {
    $sortby['images_first'] = 'Default';
    return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby_images_first' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby_images_first' );
© www.soinside.com 2019 - 2024. All rights reserved.