我有常规/简单产品和数据库中存在差异的产品。常规产品和变体具有与其关联的产品宽度(\\u pm\\u width\\u inches)。我尝试根据宽度进行排序,我发现排序会产生随机结果,而不是升序或降序。
关于meta\\u键=“\\u pm\\u width\\u inches”,它的值用双引号存储,例如:-12”,并且在为几个post\\u id选择全部后,它的值为空,原因是它是方差的父post\\u id。我在此附上一个屏幕截图(select\\u width.png)。
Here is the code that i\'m using :
add_filter( \'woocommerce_get_catalog_ordering_args\', \'sort_by_width_woocommerce_shop\' );
function sort_by_width_woocommerce_shop( $args ) {
global $wp_query;
$orderby_value = isset( $_GET[\'orderby\'] ) ? wc_clean( (string) wp_unslash( $_GET[\'orderby\'] ) ) : wc_clean( get_query_var( \'orderby\' ) );
if ( \'width-asc\' == $orderby_value ) {
$args[\'orderby\'] = \'meta_value_num\';
$args[\'order\'] = \'ASC\';
$args[\'meta_key\'] = \'_pm_width_inches\';
}
else if(\'width-desc\'== $orderby_value){
$args[\'orderby\'] = \'meta_value_num\';
$args[\'order\'] = \'DESC\';
$args[\'meta_key\'] = \'_pm_width_inches\';
}
return $args;
}
// 2. Add new product filter to Sorting dropdown
add_filter( \'woocommerce_catalog_orderby\', \'custom_woocommerce_catalog_orderby\' );
function custom_woocommerce_catalog_orderby( $orderby ) {
unset($orderby);
$orderby[\'price\'] = __(\'Price: Low to High\');
$orderby[\'price-desc\'] = __(\'Price: High to Low\');
$orderby[\'popularity\'] = __(\'By Popularity\');
$orderby[\'date\'] = __(\'Newest Arrivals\');
$orderby[\'width-asc\'] = __(\'Width: small to large\');
$orderby[\'width-desc\'] = _(\'Width:large to small \');
return $orderby;
}
add_filter( \'woocommerce_catalog_orderby\', \'custom_woocommerce_catalog_orderby\', 20 );
and once when i sort,i see this result: