maldboy scatman
maldboy scatman

Reputation: 1

Add Woocommerce Default Product Sorting

I need to change the default sorting to another sorting. I want on my /shop/ page category and tags pages the products to show by default the last modified.When i edit a product and i change something inside the product to move on the first row.

Is there anyone who can help me with this please?

Best Regards

Upvotes: 0

Views: 6277

Answers (1)

Senthil
Senthil

Reputation: 2246

WooCommerce - Change default catalog sort order. Similarly do for shop page, etc by hooks.

   /**
   * This code should be added to functions.php of your theme
    **/
   add_filter('woocommerce_default_catalog_orderby',    'custom_default_catalog_orderby');

   function custom_default_catalog_orderby() {
        return 'post_modified'; // Can also use title and price
   }

[or]
   add_filter('woocommerce_get_catalog_ordering_args',    'am_woocommerce_catalog_orderby');
    function am_woocommerce_catalog_orderby( $args ) {
       $args['orderby'] = 'last_modified';
       $args['order'] = 'desc'; 
       return $args;
    }

Ref: https://gist.github.com/mikejolley/1622323

Or ref : this can do it in admin panel. but need to add the hooks in functions.php as mentioend above . Manageable in woocomerce admin panel. http://www.remicorson.com/woocommerce-sort-products-from-oldest-to-most-recent/

Try this option it worked for me fine . This worked for me. Manageable in woocomerce admin panel. http://www.remicorson.com/woocommerce-sort-products-from-oldest-to-most-recent/ . Add the following in your current theme (functions.php) file.

    // Filters
    add_filter( 'woocommerce_get_catalog_ordering_args',     'custom_woocommerce_get_catalog_ordering_args' );
    add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
    add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );

    // Apply custom args to main query
    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 ( 'oldest_to_recent' == $orderby_value ) {
            $args['orderby'] = 'post_modified';
            $args['order'] = 'DESC';
        }

        return $args;
    }

    /* Create new sorting method */
    function custom_woocommerce_catalog_orderby( $sortby ) {    
        $sortby['oldest_to_recent'] = 
         __( 'Based on Last modified to be displayed recent', 'woocommerce' );
        return $sortby;
    }

Go to your admin panel http://localhost/wpppame/wp-admin/admin.php?page=wc-settings&tab=products&section=display and then you will see the new option added. Select it and click save. Then go to front end of localhost/wpppame/shop, you can see the changes in the page.

Upvotes: 1

Related Questions