Search code examples
woocommercehook-woocommerce

Custom ordering products based on user role


My shop is currently set to sort products by ascending price. Is there a way I can sort products by custom ordering for a specific user role only?

Thank you for your help!


Solution

  • I've found the answer to this myself, for anyone wondering:

    add_filter('woocommerce_default_catalog_orderby', 'change_catalog_orderby_specific_user');
    function change_catalog_orderby_specific_user( $sort_by ) {
        $user = wp_get_current_user();
        if ( in_array( 'author', (array) $user->roles ) ) {
            return 'menu_order';
        }
        else {
            return 'price';
        }
    }