WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] Category order by SKU shortcode? (3 posts)

  1. JapeNZ
    Member
    Posted 7 months ago #

    Hello!
    I'm trying to display category/product listings on various pages by their SKU's using shortcodes but can't get it to work, here's my latest attempt:

    [product_category category="uncanny-x-men-vol-3" per_page="99" columns="5" orderby="meta_value" order="desc" meta_key="_sku"]

    I was able to get the order for every category to change using this:

    add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
    function am_woocommerce_catalog_orderby( $args ) {
        $args['meta_key'] = '_sku';
        $args['orderby'] = 'meta_value';
        return $args;
    }

    but I don't want it applied to everything and it overrides the other category views I have set up ('date', 'rand', 'title')

    Any help would be very much appreciated:)

    https://wordpress.org/plugins/woocommerce/

  2. JapeNZ
    Member
    Posted 7 months ago #

    So I can set Default Product Sorting to 'SKU: Descending' using this code:

    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 ( 'sku_desc' == $orderby_value ) {
    		$args['orderby'] = 'meta_value';
    		$args['order'] = 'desc';
    		$args['meta_key'] = '_sku';
    	}
    
    	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['sku_desc'] = 'SKU: Descending';
    	return $sortby;
    }

    But this stops the shortcodes I have on some pages using 'title', 'date' and 'rand' from working?!

    [product_category category="xmenv2" per_page="99" columns="5" orderby="date" order="desc" meta_key=""]

    Is there some code I can add to 'SKU: Descending' that will allow custom ordering so I can have 'custom ordering + SKU: Descending' as an alternative to 'default sorting (custom ordering + name)'?

    Any help would be very much appreciated:)

  3. JapeNZ
    Member
    Posted 7 months ago #

    Okay so I think I've managed to come up with a solution.

    Rather than set SKU: Descending as default I've used the following code to add sku as a shortcode option:

    <?php
    /**
    	 * Returns an array of arguments for ordering products based on the selected values
    	 *
    	 * @access public
    	 * @return array
    	 */
    	public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
    		// Get ordering from query string unless defined
    		if ( ! $orderby ) {
    			$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    
    			// Get order + orderby args from string
    			$orderby_value = explode( '-', $orderby_value );
    			$orderby       = esc_attr( $orderby_value[0] );
    			$order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
    		}
    
    		$orderby = strtolower( $orderby );
    		$order   = strtoupper( $order );
    
    		$args = array();
    
    		// default - menu_order
    		$args['orderby']  = 'menu_order title';
    		$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    		$args['meta_key'] = '';
    
    		switch ( $orderby ) {
    			case 'rand' :
    				$args['orderby']  = 'rand';
    			break;
    			case 'date' :
    				$args['orderby']  = 'date';
    				$args['order']    = $order == 'ASC' ? 'ASC' : 'DESC';
    			break;
    			case 'price' :
    				$args['orderby']  = 'meta_value_num';
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    				$args['meta_key'] = '_price';
    			break;
    			case 'popularity' :
    				$args['meta_key'] = 'total_sales';
    
    				// Sorting handled later though a hook
    				add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
    			break;
    			case 'rating' :
    				// Sorting handled later though a hook
    				add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
    			break;
    			case 'title' :
    				$args['orderby']  = 'title';
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    			break;
    			case 'sku' :
    				$args['orderby']  = 'meta_value';
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    				$args['meta_key'] = '_sku';
    			break;
    		}
    
    		return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
    	}

    I copied the majority of this from woocommerce / includes / class-wc-shortcodes.php and added:

    case 'sku' :
    				$args['orderby']  = 'meta_value';
    				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
    				$args['meta_key'] = '_sku';
    			break;

    to the list.

    The result being I can now add use "sku" as orderby= in product category shortcodes... sweet! Haha!

    [product_category category="x-men-vol-4" per_page="99" columns="5" orderby="sku" order="desc" meta_key=""]

    You need to create a new file called class-wc-query.php in your child theme:

    public_html/wp-content/themes/'your child theme'/woocommerce/includes

    and then paste the code in there.

Reply

You must log in to post.

About this Plugin

About this Topic