Woocommerce: Show Products in Alphabetic order

后端 未结 5 610
轮回少年
轮回少年 2021-02-06 19:40

Ok I know it\'s might be a silly question or you may think it was asked dozen times. but either I am desperate and missed something or this is some unique problem. Anyway, I nee

相关标签:
5条回答
  • 2021-02-06 20:02
    <?php
    
    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    
    function custom_woocommerce_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 ( 'random_list' == $orderby_value ) {
    $args['orderby'] = 'rand';
    $args['order'] = '';
    $args['meta_key'] = '';
    }
    
    return $args;
    }
    
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    
    function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['random_list'] = 'Random';
    return $sortby;
    }
    
    0 讨论(0)
  • 2021-02-06 20:09
    $catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
            'menu_order' => __( 'Default sorting', 'woocommerce' ),
       here   ->  'title' => __( 'Sortierung nach Titel, A–Z', 'woocommerce' ),
            'popularity' => __( 'Sort by popularity', 'woocommerce' ),
            'rating'     => __( 'Sort by average rating', 'woocommerce' ),
            'date'       => __( 'Sort by newness', 'woocommerce' ),
            'price'      => __( 'Sort by price: low to high', 'woocommerce' ),
            'price-desc' => __( 'Sort by price: high to low', 'woocommerce' )
        ) );
    
    0 讨论(0)
  • 2021-02-06 20:10

    woocommerce_clean() is deprecated, use wc_clean() instead.

    https://docs.woocommerce.com/wc-apidocs/function-woocommerce_clean.html

    0 讨论(0)
  • 2021-02-06 20:13

    To expand on @Vikas_Gautam's answer and modify it to sort by post title, you would do the following:

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    
    function custom_woocommerce_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 ( 'alphabetical' == $orderby_value ) {
            $args['orderby'] = 'title';
            $args['order'] = 'DESC';
        }
    
        return $args;
    }
    
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    
    function custom_woocommerce_catalog_orderby( $sortby ) {
        $sortby['alphabetical'] = __( 'Alphabetical' );
        return $sortby;
    }
    

    Take a look at the Codex for WP_Query sort parameters.

    0 讨论(0)
  • 2021-02-06 20:23

    To expand on @helgatheviking's answer, I tried that and it sorted in (descending) alphabetical order but only if the Alphabetical option was chosen on the drop-down because of the if ( 'alphabetical' == $orderby_value ) condition.

    This is mod which sorts in ascending alphabetical order by default, the differences being the condition if ('alphabetical' == $orderby_value || 'menu_order' == $orderby_value) and $args['order'] = 'ASC'

    add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
    
    function custom_woocommerce_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 ( 'alphabetical' == $orderby_value || 'menu_order' == $orderby_value ) {
            $args['orderby'] = 'title';
            $args['order'] = 'ASC';
        }
    
        return $args;
    }
    
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
    
    function custom_woocommerce_catalog_orderby( $sortby ) {
        $sortby['alphabetical'] = __( 'Alphabetical' );
        return $sortby;
    }
    

    The site I was working on has grouped products so I made sure they were alphabetical as well...

    add_filter( 'woocommerce_grouped_children_args', 'custom_grouped_children_args' );
    function custom_grouped_children_args( $args ){
        $args['orderby'] = 'title';
        $args['order'] = 'ASC';
        return $args;
    }
    
    0 讨论(0)
提交回复
热议问题