1

I'm trying to make a custom product sort option which excludes products which are POA, the client has done so by not setting a price for them.

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'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

  if ('price_nopoa_asc' == $orderby_value) {

    $args = array(
        'meta_key'   => '_price',
        'orderby'    => 'meta_value_num',
        'order'      => 'ASC',
        'meta_compare' => '!=',
        'meta_value'   => '',
        'meta_type'   => 'numerical',
    );

}

    return $args;
}
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['price_nopoa_asc'] = 'Sort by Price: products Low to High';
    return $sortby;
}

Everything other than ignoring the products without prices set works.

Am I checking for an empty field in the wrong way?

Ruvee
  • 8,611
  • 4
  • 18
  • 44
Thomas W
  • 11
  • 1

1 Answers1

0

Try the following code instead:

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'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

  if ('price_nopoa_asc' == $orderby_value) {

    $args = array(
        'meta_key'   => '_price',
        'orderby'    => 'meta_value_num',
        'order'      => 'ASC',
        'meta_query' => array(
            array(
             'key' => '_price', # add your key here for comparison
             'compare' => '!=',
             'value'   => '' # if the empty string didn't work, try changing it to zero
            )
        )
    );

}

    return $args;
}
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['price_nopoa_asc'] = 'Sort by Price: products Low to High';
    return $sortby;
}

Update 2023

I initially wrote this answer more than two years ago. A lot has happened since then! If you can't get this code to work it could be due to many reasons that I don't know about if you don't elaborate (It could be the version of wordpress/woocommerce that you're running on your server, or maybe you're problem is something else. There could be many reasons as why you can't get it to work). Simply saying "This or that doesn't work", and not explaining clearly and not providing error(s)/warning(s) you got, is not a good strategy to get your problem solved. You have to elaborate so that other people could help you. Thanks!

You might want to check this answer https://stackoverflow.com/a/52275842/15040627, since other people have found it helpful.

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • The above code did not work. – raratiru Jul 21 '23 at 15:12
  • @raratiru I've written this answer more than two years ago. A lot has happened since then! Can you explain what is the problem you're trying to solve? Which version of `wordpress`/`woocommerce` are you running on your server? Did you get any warning(s)/error(s)? Please elaborate your problem or create a new question so that others would be able to point you in the right direction. Thanks! – Ruvee Jul 21 '23 at 15:49
  • @raratiru I see you've already found your answer, good then you didn't have to drop a comment under my answer. It was really unnecessary. Simply saying "This or that doesn't work" is not a good strategy to get your problem solved. You have to elaborate so that other people could help you. – Ruvee Jul 21 '23 at 15:56
  • Sorry for not explaining, it was just a note to remember. I am still trying to exclude products without price from appearing in the default low to high price sorting. Those products are also is_purchasable() -> false. The above code creates a custom option but still does not exclude the products without price. Could it be due to is_purchasable() differentiation (aka they are not products?). – raratiru Jul 21 '23 at 16:30
  • @raratiru Not a problem at all. It could be what you said, I'm not sure because my mind is currently not in the `woocommerce universe`, I'm trying to solve other problems at this moment, so I'll look into it when I get a chance. Thanks again. – Ruvee Jul 21 '23 at 16:36
  • Thank you! I just found the solution: https://stackoverflow.com/a/76740163/2996101 – raratiru Jul 21 '23 at 17:49