skip to Main Content

My goal is to get the current WooCommerce sorting option selected, such as websitename.com/?orderby=popularity and set whatever the current query selection is as a body class. (Ideally something like body class="popularity").

In the example below I am using if/elseif statements. I attempted this with a switch operator as well as simply using global $orderby. I haven’t been able to find any example covering this question and feeling quite stuck so I appreciate any help.

Thank you. Here is my current documentation:

add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );

function gaz_wc_sorting_as_body_class( $class ){

    $orderby_value = $_GET['orderby'];

    if ($orderby_value === 'popularity') {
        array( ' popular '  => $class);
    } elseif ($orderby_value === 'date') {
        array( ' recent '  => $class);
    } elseif ($orderby_value === 'price') {
        array( ' cost-lo-hi '  => $class);
    } elseif ($orderby_value === 'price-desc') {
        array( ' cost-hi-lo '  => $class);
    } else {
        array( 'sort-clear '  => $class);
    }
    return $class;
}

3

Answers


  1. Chosen as BEST ANSWER

    Solution - Woo hoo! Bhautik is correct about array_push($class, 'string'); Though it wasn't working for me until I changed the singular quotations around the string to push into double quotations. Using double quotations, this documentation using array_push() works like a charm!

    Though, I took it one step further to make the function dynamic for orderby queries by pushing the $orderby_value as the class name pushed to the body class array.

    function gaz_wc_sorting_as_body_class( $class ){
        
     $orderby_value = $_GET['orderby'];
        
        if ( $orderby_value ) {
            array_push( $class, $orderby_value );
        }
    
      return $class;
    
    }
    add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );
    

    For anyone interested in using static classes pushed to the body HTML element, here is my documentation that worked with Bhautik's solution:

    function gaz_wc_sorting_as_body_class( $class ){
    
        $orderby_value = $_GET['orderby'];
        
        if ( $orderby_value === 'popularity' ) {
            // Required DOUBLE quotations (not singluar.)
            array_push($class, "popular" );
        } elseif ( $orderby_value === 'date' ) {
            array_push($class, "recent");
        } elseif ( $orderby_value === 'price' ) {
            array_push($class, "cost-lo-hi");
        } elseif ( $orderby_value === 'price-desc' ) {
            array_push($class, "cost-hi-lo");
        } else {
            array_push($class, "sort-clear");
        }
        
        return $class;
    }
    add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );
    

  2. This might do the trick.

    add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );
    
    function gaz_wc_sorting_as_body_class( $class ){
        
         $class = array('orderby' => '', 'order' => '');
         if (self::isActive()) {
             $query = new WC_Query();
             $class = $query->get_catalog_ordering_args();
         }
         return $class;
     }
    
    Login or Signup to reply.
  3. You can push your class name to the $class array. You can use the array_push function. try the below code.

    function gaz_wc_sorting_as_body_class( $class ){
    
        $orderby_value = $_GET['orderby'];
    
        if ( $orderby_value === 'popularity' ) {
            array_push( $class, 'popular' );
        } elseif ( $orderby_value === 'date' ) {
            array_push($class, 'recent');
        } elseif ( $orderby_value === 'price' ) {
            array_push($class, 'cost-lo-hi');
        } elseif ( $orderby_value === 'price-desc' ) {
            array_push($class, 'cost-hi-lo');
        } else {
            array_push($class, 'sort-clear');
        }
        
        return $class;
    }
    add_filter( 'body_class', 'gaz_wc_sorting_as_body_class' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search