skip to Main Content

I see that it is possible to add your custom field for sorting products in WooCommerce (e.g. this question) Copying from the example:

add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_modified_date' );
function enable_catalog_ordering_by_modified_date( $args ) {
    if ( isset( $_GET['orderby'] ) ) {
        if ( 'modified_date' == $_GET['orderby'] ) {
            return array(
                'orderby'  => 'modified',  //This is the custom field to be sorted by, and what I am asking for
                'order'    => 'DESC',
            );
        }
    }
    return $args;
}

Is there a list of all fields that I can use for sorting?


P.S. Thanks for the comment, but what I need is not sorting normal posts in WordPress, but the products in WooCommerce.

3

Answers


  1. These are the default list of orderby options available( id, title, relevance, rand, date, price, popularity, rating). The case of the switch case may be what you are looking for.

    switch ( $orderby ) {
            case 'id':
                $args['orderby'] = 'ID';
                break;
            case 'menu_order':
                $args['orderby'] = 'menu_order title';
                break;
            case 'title':
                $args['orderby'] = 'title';
                $args['order']   = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
                break;
            case 'relevance':
                $args['orderby'] = 'relevance';
                $args['order']   = 'DESC';
                break;
            case 'rand':
                $args['orderby'] = 'rand'; // @codingStandardsIgnoreLine
                break;
            case 'date':
                $args['orderby'] = 'date ID';
                $args['order']   = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
                break;
            case 'price':
                $callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';
                add_filter( 'posts_clauses', array( $this, $callback ) );
                break;
            case 'popularity':
                add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
                break;
            case 'rating':
                add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
                break;
        }
    

    The above code is from woocommerce/includes/class-wc-query.php line 586…

    Login or Signup to reply.
  2. You need to know what is inside of an product object. You can sort your products by any of this product data.

    Here is a list I found where you can access all the data inside of a product object.
    (source: https://wpdavies.dev/how-to-get-all-product-info-in-woocommerce/)

    <?php
    
    /**
     *
     *  General Product Data
     *
     */
    $product->get_id(); // Returns the unique ID for this object.
    $product->get_description(); // Get product description.
    $product->get_formatted_name(); // Get product name with SKU or ID. Used within admin.
    $product->get_featured(); // If the product is featured.
    $product->get_name(); // Get product name.
    $product->get_title(); // Get the product's title. For products this is the product name.
    $product->get_type(); // Get internal type. Should return string and *should be overridden* by child classes.
    $product->get_virtual(); // Get virtual.
    $product->get_total_sales(); // Get number total of sales.
    $product->get_short_description(); // Get product short description.
    $product->get_sku(); // Get SKU (Stock-keeping unit) - product unique ID.
    $product->get_slug(); // Get product slug.
    $product->get_status(); // Get product status.
    $product->get_permalink(); // Product permalink.
    $product->get_catalog_visibility(); // Get catalog visibility.
    
    /**
     *
     *  Pricing Data
     *
     */
    $product->get_price(); // Returns the product's active price.
    $product->get_date_on_sale_from(); // Get date on sale from.
    $product->get_date_on_sale_to(); // Get date on sale to.
    $product->get_display_price(); // Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.
    $product->get_price_excluding_tax(); // Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.
    $product->get_price_html(); // Returns the price in html format.
    $product->get_price_html_from_text(); // Functions for getting parts of a price, in html, used by $product->get_price_html.
    $product->get_price_html_from_to(); // Functions for getting parts of a price, in html, used by $product->get_price_html.
    $product->get_price_including_tax(); // Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes.
    $product->get_price_suffix(); // Get the suffix to display after prices > 0.
    $product->get_sale_price(); // Returns the product's sale price.
    $product->get_regular_price(); // Returns the product's regular price.
    $product->get_tax_class(); // Returns the tax class.
    $product->get_tax_status(); // Returns the tax status.
    
    /**
     *
     *  Image Related Data
     *
     */
    $product->get_image(); // Returns the main product image.
    $product->get_image_id(); // Get main image ID.
    $product->get_gallery_attachment_ids(); // Returns the gallery attachment ids.
    $product->get_gallery_image_ids(); // Returns the gallery attachment ids.
    
    /**
     *
     *  Stock or Inventory Data
     *
     */
    $product->get_backorders(); // Get backorders.
    $product->get_availability(); // Returns the availability of the product.
    $product->get_max_purchase_quantity(); // Get max quantity which can be purchased at once.
    $product->get_min_purchase_quantity(); // Get min quantity which can be purchased at once.
    $product->get_stock_managed_by_id(); // If the stock level comes from another product ID, this should be modified.
    $product->get_stock_quantity(); // Returns number of items available for sale.
    $product->get_stock_status(); // Return the stock status.
    $product->get_total_stock(); // Get total stock - This is the stock of parent and children combined.
    $product->get_sold_individually(); // Return if should be sold individually.
    $product->get_low_stock_amount(); // Get low stock amount.
    
    /**
     *
     *  Shipping Data
     *
     */
    $product->get_height(); // Returns the product height.
    $product->get_length(); // Returns the product length.
    $product->get_weight(); // Returns the product's weight.
    $product->get_width(); // Returns the product width.
    $product->get_dimensions(); // Returns formatted dimensions.
    $product->get_manage_stock(); // Return if product manage stock.
    $product->get_shipping_class(); // Returns the product shipping class SLUG.
    $product->get_shipping_class_id(); // Get shipping class ID.
    
    /**
     *
     *  Product Variations / Parent Data
     *
     */
    $product->get_child(); // Returns the child product.
    $product->get_children(); // Returns the children IDs if applicable. Overridden by child classes.
    $product->get_formatted_variation_attributes(); // Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names.
    $product->get_matching_variation(); // Match a variation to a given set of attributes using a WP_Query.
    $product->get_parent(); // Get the parent of the post.
    $product->get_parent_id(); // Get parent ID.
    $product->get_variation_default_attributes(); // If set, get the default attributes for a variable product.
    $product->get_variation_description(); // Get product variation description.
    $product->get_variation_id(); // Get variation ID.
    
    /**
     *
     *  Product Downloads
     *
     */
    $product->get_download_expiry(); // Get download expiry.
    $product->get_download_limit(); // Get download limit.
    $product->get_downloadable(); // Get downloadable.
    $product->get_downloads(); // Get downloads.
    $product->get_file(); // Get a file by $download_id.
    $product->get_file_download_path(); // Get file download path identified by $download_id.
    $product->get_files(); // Same as $product->get_downloads in CRUD.
    
    /**
     *
     *  Attributes, Tags, Categories & Associated Data Objects
     *
     */
    $product->get_attribute(); // Returns a single product attribute as a string.
    $product->get_attributes(); // Returns product attributes.
    $product->get_categories(); // Returns the product categories.
    $product->get_category_ids(); // Get category ids.
    $product->get_default_attributes(); // Get default attributes.
    $product->get_cross_sell_ids(); // Get cross sell IDs.
    $product->get_cross_sells(); // Returns the cross sell product ids.
    $product->get_related(); // Get and return related products.
    $product->get_tag_ids(); // Get tag ids.
    $product->get_tags(); // Returns the product tags.
    $product->get_upsell_ids(); // Get upsell IDs.
    $product->get_upsells(); // Returns the upsell product ids.
    $product->get_meta(); // Get Meta Data by Key.
    $product->get_meta_data(); // Get All Meta Data.
    
    /**
     *
     *  Ratings and Reviews
     *
     */
    $product->get_rating_count(); // Get the total amount (COUNT) of ratings, or just the count for one rating e.g. number of 5 star ratings.
    $product->get_rating_counts(); // Get rating count.
    $product->get_rating_html(); // Returns the product rating in html format.
    $product->get_review_count(); // Get review count.
    $product->get_reviews_allowed(); // Return if reviews is allowed.
    $product->get_average_rating(); // Get average rating.
    
    /**
     *
     *  Other Product Data
     *
     */
    $product->get_changes(); // Return data changes only.
    $product->get_data(); // Returns all data for this object.
    $product->get_data_keys(); // Returns array of expected data keys for this object.
    $product->get_data_store(); // Get the data store.
    $product->get_date_created(); // Get product created date.
    $product->get_date_modified(); // Get product modified date.
    $product->get_extra_data_keys(); // Returns all "extra" data keys for an object (for sub objects like product types).
    $product->get_menu_order(); // Get menu order.
    $product->get_meta_cache_key(); // Helper method to compute meta cache key. Different from WP Meta cache key in that meta data cached using this key also contains meta_id column.
    $product->get_object_read(); // Get object read property.
    $product->get_post_data(); // Get the product's post data.
    $product->get_post_password(); // Get post password.
    $product->get_purchase_note(); // Get purchase note.
    

    With this you can see under "other product data", that there is a date_modified you can order your products by.

    It is not directly a list of all fields you can sort by. But it indirectly shows you which fields are available inside a product and what the fields are called. With the name, you are able to access them.

    Custom meta:

    Unfortunatelly there is no data saved for "published_date". But you could create a custom field for your product post type and save the date inside product. Via the meta key you can than access the data of the product and sort your products by your custom meta data.

    Login or Signup to reply.
  3. If the point of the question is whether there is a list that returns all the fields that can be used for sorting, the short answer is no.

    The woocommerce_get_catalog_ordering_args filter allows you to add your own ordering args, but there is no list that would store these fields. This filter is used to overwrite args, not to store it as an array.

    Within the get_catalog_ordering_args() function, some orderby defaults are statically specified: id, menu_order, title, relevance, rand, date, price, popularity, rating.

    But there is no way to tell what values have been added using a filter by WordPress plugins.


    Possible solution:

    There is a woocommerce_catalog_orderby filter that stores sorting options as an array.

    This is used to display sorting options in the view. So you can assume that WordPress plugins will add options to this as well.

    /**
     * Add orderby option by plugin
     */
    add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
        $options['order_by_plugin'] = __( 'Order by plugin setting', 'text-domain' );
    
        return $options;
    } );
    
    /**
     * Get orderby options
     */
    $catalog_orderby_options = apply_filters( 'woocommerce_catalog_orderby', array(
        'menu_order' => __( 'Default sorting (custom ordering + name)', 'woocommerce' ),
        'popularity' => __( 'Popularity (sales)', 'woocommerce' ),
        'rating'     => __( 'Average rating', 'woocommerce' ),
        'date'       => __( 'Sort by most recent', 'woocommerce' ),
        'price'      => __( 'Sort by price (asc)', 'woocommerce' ),
        'price-desc' => __( 'Sort by price (desc)', 'woocommerce' ),
    ) );
    

    You can use $catalog_orderby_options in which the sorting options are stored, if you only need the orderby fields, use the array_keys() function.

    Note: you must specify the default value in apply_filters(), because WooCommerce added these statically, not using add_filter(). So you have to add it statically too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search