WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
Adding a search/sort parameter for quantity (2 posts)

  1. ianarmstrong
    Member
    Posted 6 months ago #

    I was shuttled over here by the devs at GIT. Hopefully someone here will be able to help me out.

    Perhaps my attempt was overly simplistic. Suffice to say, it didn't work:

    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 ( 'by_quantity' == $orderby_value ) {
            $args['orderby'] = 'meta_value';
            $args['order'] = 'desc';
            $args['meta_key'] = 'stock';
        }
    
        return $args;
    }

    What I'd rather do is filter class-wc-settings-products.php and add the ability to do a default sort by quantity in the settings, since that's really what they want. If I can add it to the base code, then I can make it a default directly through the WP Settings.

    Could one of you help me out with that filter?

    Thanks!

    https://wordpress.org/plugins/woocommerce/

  2. Daniel Espinoza
    Member
    Posted 6 months ago #

    Coen was right, github issues isn't support :)

    Your code sample is missing the filter that ads 'by_quantity' to the sort by list. Refer to the gist here: http://docs.woothemes.com/document/custom-sorting-options-ascdesc/

    And make sure you have this code:

    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['by_quantity'] = 'Random';
    	return $sortby;
    }

Reply

You must log in to post.

About this Plugin

About this Topic