0

I have used the hook "woocommerce_get_catalog_ordering_args" and I can edit the sort by, but can't figure out how to search params and sort by product names before descriptions. Any ideas?

add_action( 'woocommerce_get_catalog_ordering_args', 'sort_by_quantity', 900);

function sort_by_quantity( $args ) {
    $args['orderby'] = 'meta_value';
    $args['order'] = 'ASC';
    $args['meta_key'] = '_stock_status';
    return $args;


};

Edit:

Just to clarify my example of what I need...

If I was to have a product with the name Purple Pants, and someone was to search pants. They should see "Purple Pants" before any products that don't have "pants" in their product name. (ex. description)

In the example below you can see I have searched 006 but the second product doesn't have 006 in the name and the third product does. I want to make sure the hierarchy is product name over description.

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting' );

function custom_product_sorting( $args ) {
    $search = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ));
    // Show products in stock first
    if( 'in-stock' === $search ) { 
        $args['meta_key'] = '_stock_status';
        $args['orderby'] = array( 'meta_value' => 'ASC' );
    }
    if('relevance' === $search) {
        $args['meta_key'] = ['','_stock_status'];
        $args['orderby'] = ['relevance' => "desc", 'meta_value' => 'ASC'];
    }

    return $args;

}

enter image description here

davidb3rn
  • 141
  • 2
  • 15
  • 4
    Look here: https://stackoverflow.com/questions/52273655/add-a-new-custom-default-ordering-catalog-option-in-woocommerce and https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/ – Vincenzo Di Gaetano May 05 '21 at 20:28
  • @VincenzoDiGaetano I have checked your source, and it doesn't quite explain the issue I am having. I have updated my ticket to reflect. – davidb3rn May 06 '21 at 13:01

0 Answers0