我在我的wordpress主题的function.php中使用以下代码。而且,它仅对单个产品进行分类和显示,而不对折扣/销售中的可变产品进行分类/列出。
add_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' );
function wcs_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'on_sale' == $orderby_value ) {
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
$args['meta_key'] = '_sale_price';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'wcs_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'wcs_catalog_orderby' );
function wcs_catalog_orderby( $sortby ) {
$sortby['on_sale'] = 'Хямдралын хувиар';
return $sortby;
}
因此,我该如何对打折/特价的简单和可变产品进行排序/显示
答案 0 :(得分:0)
选中此一项。在我的情况下对我来说很有效。
function cw_add_postmeta_ordering_args( $args_sort_cw ) {
$cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :
apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $cw_orderby_value ) {
case 'points_awarded':
$args_sort_cw['orderby'] = 'meta_value_num';
$args_sort_cw['order'] = 'desc';
$args_sort_cw['meta_key'] = 'points';
break;
case 'location':
$args_sort_cw['orderby'] = 'meta_value';
$args_sort_cw['order'] = 'asc';
$args_sort_cw['meta_key'] = 'location';
break;
}
return $args_sort_cw;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args' );
function cw_add_new_postmeta_orderby( $sortby ) {
$sortby['location'] = __( 'Sort By Location', 'woocommerce' );
$sortby['points_awarded'] = __( 'Sort By Sale Point', 'woocommerce' );
return $sortby;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'cw_add_new_postmeta_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'cw_add_new_postmeta_orderby' );
FYR:-https://www.cloudways.com/blog/woocommerce-product-sort-and-display/