• I added a code snipped to my functions.php file to add asc/desc sorting to my product category. Worked great, but it did not add the sorting drop down to product list pages, which are custom from my theme(I think).

    Below are 2 code blocks, is there a way I can get the product sorting code to work with the code that creates the lists?

    Code that creates the product list through product manager.

    function _wip_show_product_lists_for_manager( $col = 4, $postNumber = 4, $catID = 0, $featured = false, $pagination = false){
    	global $woocommerce_loop, $sidebar_layout, $fullwidth_layout;
    	wp_reset_query();
    	$content = "";
    	$woocol = $col;
    	$toocol = $col;
    	if( $col == 34 ){
    		$woocol = 3;
    		$toocol = 4;
    	}
    	$woocommerce_loop['columns'] = $woocol;
    	$paged = ( $pagination ) ? ( get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1 ) : false;
    	$parentLayout = ( $fullwidth_layout ) ? 'fullwidth' : 'content-sidebar';
    
    	$args = array(
    		'post_type'	=> 'product',
    		'post_status' => 'publish',
    		'ignore_sticky_posts'	=> 1,
    		'posts_per_page' => $postNumber,
    		'paged' => $paged
    	);
    
    	$args['meta_query'] = array(
    		  array(
    			  'key' => '_visibility',
    			  'value' => array('catalog', 'visible'),
    			  'compare' => 'IN'
    		  )
    	  );
    
    	if( !$featured && ( $catID && $catID != "" && $catID !== 0 ) ){
    		$args['tax_query'] = array(
    			array(
    				'taxonomy' => 'product_cat',
    				'field' => 'id',
    				'terms' => intval($catID),
    				'operator' => 'IN'
    			)
    		);
    	}
    
    	if( $featured ){
    		$args['meta_key'] = '_featured';
    		$args['meta_value'] = 'yes';
    	}
    
    	$colClass = 'col_four float_left';
    	switch( $col ){
    		case '5':
    				$colClass = 'col_five float_left';
    			break;
    		case '4':
    		case '34':
    				$colClass = 'col_four float_left';
    			break;
    		case '3':
    				$colClass = 'col_three float_left';
    			break;
    		case '2':
    				$colClass = 'col_two float_left';
    			break;
    	}
    
    	#sent new variable via $woocommerce_loop
    	$woocommerce_loop['wip_cols'] = $toocol;
    	$woocommerce_loop['parent_layout'] = $parentLayout;
    
    	//$product_query = new WP_Query( $args );
    	query_posts($args);
    	ob_start();
    	woocommerce_get_template_part( 'loop', 'shop' );
    	$woo_loop = ob_get_clean();
    	$woo_loop = str_replace('class="product ', 'class="'.$colClass.' ', $woo_loop);
    
    	$content .= '<div class="product_wraper">' . "\n";
    	$content .= '<div class="col_wraper no_margin">' . "\n";
    	$content .= $woo_loop;
    	$content .= ( $pagination ) ? wip_pagenavi( $args, false, '<div class="pagination_wrap">', '</div>') : '';
    	$content .= '</div>' . "\n";
    	$content .= '</div>' . "\n";
    
    	wp_reset_query();
    	return $content;
    
    }

    Here is the code that creates the sorting filter.

    /**
    
    * This code should be added to functions.php of your theme
    
    **/
    
    add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
    
    function custom_woocommerce_get_catalog_ordering_args( $args ) {
    
    if (isset($_SESSION['orderby'])) {
    
    switch ($_SESSION['orderby']) :
    
    case 'date_asc' :
    
    $args['orderby'] = 'date';
    
    $args['order'] = 'asc';
    
    $args['meta_key'] = '';
    
    break;
    
    case 'price_desc' :
    
    $args['orderby'] = 'meta_value_num';
    
    $args['order'] = 'desc';
    
    $args['meta_key'] = '_price';
    
    break;
    
    case 'title_desc' :
    
    $args['orderby'] = 'title';
    
    $args['order'] = 'desc';
    
    $args['meta_key'] = '';
    
    break;
    
    endswitch;
    
    }
    
    return $args;
    
    }
    
    add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
    
    function custom_woocommerce_catalog_orderby( $sortby ) {
    
    $sortby['title_desc'] = 'Reverse-Alphabetically';
    
    $sortby['price_desc'] = 'Price (highest to lowest)';
    
    $sortby['date_asc'] = 'Oldest to newest';
    
    return $sortby;
    
    }

    Can anyone help me put these together to this drop down menu to display on this page(https://motodude.com/literature/) and not just this page.(https://motodude.com/product-category/literature/)

    Any help would be greatly appreciated.

    https://www.ads-software.com/extend/plugins/woocommerce/

  • The topic ‘Adding sorting to product list php help’ is closed to new replies.