Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #386754

    Hey Kriesi.at Support Team,

    I’ve been trying for hours to get a custom sort field to work but had no luck. WooThemes’ documentation suggests to use the code from this page. Here is my modified code which is written to sort the products based on whether or not the products are in stock:

    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 ( 'stock' == $orderby_value ) {
    		$args['orderby'] = 'meta_value';
    		$args['order'] = 'asc';
    		$args['meta_key'] = '_stock_status';
    	}
    
    	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['stock'] = 'Stock Status';
    	return $sortby;
    }

    I have also edited part of the woocommerce-config.php file to see how adding my custom field to the front end will work (i intend to use functions.php in my child theme folder to override this later)

    Here are my edits to woocommerce-config.php starting at line 986:

    #
    # displays a front end interface for modifying the shoplist query parameters like sorting order, product count etc
    #
    if(!function_exists('avia_woocommerce_frontend_search_params'))
    {
    	add_action( 'woocommerce_before_shop_loop', 'avia_woocommerce_frontend_search_params', 20);
    
    	function avia_woocommerce_frontend_search_params()
    	{
    		global $avia_config;
    
    		if(!empty($avia_config['woocommerce']['disable_sorting_options'])) return false;
    
    		$product_order['default'] 	= __("Default Order",'avia_framework');
    		$product_order['title'] 	= __("Name",'avia_framework');
    		$product_order['price'] 	= __("Price",'avia_framework');
    		$product_order['date'] 		= __("Date",'avia_framework');
    		$product_order['popularity'] = __("Popularity",'avia_framework');
    		$product_order['stock'] = __("Stock Status",'avia_framework');
    
    		$product_sort['asc'] 		= __("Click to order products ascending",  'avia_framework');
    		$product_sort['desc'] 		= __("Click to order products descending",  'avia_framework');
    
    		$per_page_string 		 	= __("Products per page",'avia_framework');
    
    		$per_page 		 		 	= get_option('avia_woocommerce_product_count');
    		if(!$per_page) $per_page 	= get_option('posts_per_page');
    		if(!empty($avia_config['woocommerce']['default_posts_per_page'])) $per_page = $avia_config['woocommerce']['default_posts_per_page'];
    
    		parse_str($_SERVER['QUERY_STRING'], $params);
    
    		$po_key = !empty($avia_config['woocommerce']['product_order']) ? $avia_config['woocommerce']['product_order'] : 'default';
    		$ps_key = !empty($avia_config['woocommerce']['product_sort'])  ? $avia_config['woocommerce']['product_sort'] : 'asc';
    		$pc_key = !empty($avia_config['woocommerce']['product_count']) ? $avia_config['woocommerce']['product_count'] : $per_page;
    
    		$ps_key = strtolower($ps_key);
    		
    		$nofollow = 'rel="nofollow"';
    
    		//generate markup
    		$output  = "";
    		$output .= "<div class='product-sorting'>";
    		$output .= "    <ul class='sort-param sort-param-order'>";
    		$output .= "    	<li><span class='currently-selected'>".__("Sort by",'avia_framework')." <strong>".$product_order[$po_key]."</strong></span>";
    		$output .= "    	<ul>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'default')."><a href='".avia_woo_build_query_string($params, 'product_order', 'default')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['default']."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'title')."><a href='".avia_woo_build_query_string($params, 'product_order', 'title')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['title']."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'price')."><a href='".avia_woo_build_query_string($params, 'product_order', 'price')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['price']."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'date')."><a href='".avia_woo_build_query_string($params, 'product_order', 'date')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['date']."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'popularity')."><a href='".avia_woo_build_query_string($params, 'product_order', 'popularity')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['popularity']."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($po_key, 'stock')."><a href='".avia_woo_build_query_string($params, 'product_order', 'stock')."' {$nofollow}>	<span class='avia-bullet'></span>".$product_order['stock']."</a></li>";
    		$output .= "    	</ul>";
    		$output .= "    	</li>";
    		$output .= "    </ul>";
    
    		$output .= "    <ul class='sort-param sort-param-sort'>";
    		$output .= "    	<li>";
    		if($ps_key == 'desc') 	$output .= "    		<a title='".$product_sort['asc']."' class='sort-param-asc'  href='".avia_woo_build_query_string($params, 'product_sort', 'asc')."' {$nofollow}>".$product_sort['desc']."</a>";
    		if($ps_key == 'asc') 	$output .= "    		<a title='".$product_sort['desc']."' class='sort-param-desc' href='".avia_woo_build_query_string($params, 'product_sort', 'desc')."' {$nofollow}>".$product_sort['asc']."</a>";
    		$output .= "    	</li>";
    		$output .= "    </ul>";
    
    		$output .= "    <ul class='sort-param sort-param-count'>";
    		$output .= "    	<li><span class='currently-selected'>".__("Display",'avia_framework')." <strong>".$pc_key." ".$per_page_string."</strong></span>";
    		$output .= "    	<ul>";
    		$output .= "    	<li".avia_woo_active_class($pc_key, $per_page).">  <a href='".avia_woo_build_query_string($params, 'product_count', $per_page)."' {$nofollow}>		<span class='avia-bullet'></span>".$per_page." ".$per_page_string."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($pc_key, $per_page*2)."><a href='".avia_woo_build_query_string($params, 'product_count', $per_page * 2)."' {$nofollow}>	<span class='avia-bullet'></span>".($per_page * 2)." ".$per_page_string."</a></li>";
    		$output .= "    	<li".avia_woo_active_class($pc_key, $per_page*3)."><a href='".avia_woo_build_query_string($params, 'product_count', $per_page * 3)."' {$nofollow}>	<span class='avia-bullet'></span>".($per_page * 3)." ".$per_page_string."</a></li>";
    		$output .= "    	</ul>";
    		$output .= "    	</li>";
    		$output .= "	</ul>";
    
    		$output .= "</div>";
    		echo $output;
    	}
    }

    and starting at line 1088:

    //function that actually overwrites the query parameters
    if(!function_exists('avia_woocommerce_overwrite_catalog_ordering'))
    {
    	add_action( 'woocommerce_get_catalog_ordering_args', 'avia_woocommerce_overwrite_catalog_ordering', 20);
    
    	function avia_woocommerce_overwrite_catalog_ordering($args)
    	{
    		global $avia_config;
    
    		if(!empty($avia_config['woocommerce']['disable_sorting_options'])) return $args;
    
    		//check the folllowing get parameters and session vars. if they are set overwrite the defaults
    		$check = array('product_order', 'product_count', 'product_sort');
    		if(empty($avia_config['woocommerce'])) $avia_config['woocommerce'] = array();
    
    		foreach($check as $key)
    		{
    			if(isset($_GET[$key]) ) $_SESSION['avia_woocommerce'][$key] = esc_attr($_GET[$key]);
    			if(isset($_SESSION['avia_woocommerce'][$key]) ) $avia_config['woocommerce'][$key] = $_SESSION['avia_woocommerce'][$key];
    		}
    
    		// is user wants to use new product order remove the old sorting parameter
    		if(isset($_GET['product_order']) && !isset($_GET['product_sort']) && isset($_SESSION['avia_woocommerce']['product_sort']))
    		{
    			unset($_SESSION['avia_woocommerce']['product_sort'], $avia_config['woocommerce']['product_sort']);
    		}
    
    		extract($avia_config['woocommerce']);
    
    		// set the product order
    		if(!empty($product_order))
    		{
    			switch ( $product_order ) {
    				case 'date'  : $orderby = 'date'; $order = 'desc'; $meta_key = '';  break;
    				case 'price' : $orderby = 'meta_value_num'; $order = 'asc'; $meta_key = '_price'; break;
    				case 'popularity' : $orderby = 'meta_value_num'; $order = 'desc'; $meta_key = 'total_sales'; break;
    				case 'stock' : $orderby = 'meta_value'; $order = 'asc'; $meta_key = '_stock_status'; break;
    				case 'title' : $orderby = 'title'; $order = 'asc'; $meta_key = ''; break;
    				case 'default':
    				default : $orderby = 'menu_order title'; $order = 'asc'; $meta_key = ''; break;
    			}
    		}
    
    		// set the product count
    		if(!empty($product_count) && is_numeric($product_count))
    		{
    			$avia_config['shop_overview_products_overwritten'] = true;
    			$avia_config['shop_overview_products'] = (int) $product_count;
    		}
    
    		//set the product sorting
    		if(!empty($product_sort))
    		{
    			switch ( $product_sort ) {
    				case 'desc' : $order = 'desc'; break;
    				case 'asc' : $order = 'asc'; break;
    				default : $order = 'asc'; break;
    			}
    		}
    
    		if(isset($orderby)) $args['orderby'] = $orderby;
    		if(isset($order)) 	$args['order'] = $order;
    		if (!empty($meta_key))
    		{
    			$args['meta_key'] = $meta_key;
    		}
    
    		$avia_config['woocommerce']['product_sort'] = $args['order'];
    
    		return $args;
    	}
    
    }

    Even after all of these edits, I cannot get it to work. Is there any other files that i have to override or edit in order to get the products to display in the correct order when I’ve set the product sort to Stock Status? I’m currently running the latest version of Enfold on the latest version of WordPress with the latest version of WooCommerce. Thanks in advance.

    Sincerely,

    Cory Gottschalk
    Web Developer
    New Leaf Innovations
    (Email address hidden if logged out)

    #387918

    Hey Jonathan!

    I tested the first half of your code out. I added this to the bottom of my functions.php file.

    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 ( 'stock' == $orderby_value ) {
    		$args['orderby'] = 'meta_value';
    		$args['order'] = 'asc';
    		$args['meta_key'] = '_stock_status';
    	}
    
    	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['stock'] = 'Stock Status';
    	return $sortby;
    }

    And it seems to work fine. What it does is it adds a new sort option in Dashboard > Woocommerce > Settings > Products called “Stock Status” so you need to select that in the “Default Product Sorting” setting and save.

    Cheers!
    Elliott

    #1318125
    This reply has been marked as private.
    #1319052

    Hi,

    Sorry for the delay. What happens when you add the modification directly within the avia_woocommerce_overwrite_catalog_ordering function in the enfold/config-woocommerce/config.php file?

    You may also have to assign the order and orderby value to the $avia_config global variable in order to actually override the avia_woocommerce_overwrite_catalog_ordering function, which is using the same filter.

    $avia_config['woocommerce']['product_order'] = strtolower(  $args['orderby'] );
    $avia_config['woocommerce']['product_sort'] = strtolower(  $args['order'] );
    

    Best regards,
    Ismael

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.