WooCommerce根据实际产品宽度(而不是发货宽度)对产品进行排序

时间:2018-09-11 作者:YeshDev

我有常规/简单产品和数据库中存在差异的产品。常规产品和变体具有与其关联的产品宽度(\\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)。

select_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:

sort_results

1 个回复
SO网友:YeshDev

以下代码适用于简单产品,但不适用于有差异的产品

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\';
        $args[\'meta_type\'] = \'NUMERIC\';
        $args[\'type\'] = \'product\';

    }
    else if(\'width-desc\'== $orderby_value){
        $args[\'orderby\'] = \'meta_value_num\';
        $args[\'order\'] = \'DESC\';
        $args[\'meta_key\'] = \'_pm_width_inches\';
        $args[\'type\'] = \'product\';


    }
  /*   ?>
    <?php echo "<ul>". var_dump(  $args[\'meta_key\'] )."</ul>";?>
 <?php */

    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\'] = __(\'Size: small to large\');
    $orderby[\'width-desc\'] = _(\'Size:large to  small \');
   /*  ?>
   <?php echo "<ul>".var_dump($orderby)."</ul>";?>
<?php */
    return $orderby;
}
add_filter( \'woocommerce_catalog_orderby\', \'custom_woocommerce_catalog_orderby\', 20 );                          

结束

相关推荐

Ajaxify Post Sort

我这里有一些代码,当您单击某些链接时,它会重新加载页面,并根据我设置的查询数组对我的帖子进行排序。我只是想知道如何让它通过ajax加载和排序,而不是重新加载页面?以下是我使用的代码: <?php $sort= esc_attr($_GET[\'sort\']); if($sort == \"title_asc\") { $order= \"&orderby=title&order=ASC\"; } if($sort == \"ti