The below code allows you to sort products, using the wp-e-commerce wordpress plugin by category and product title.

Trying to adapt and change this code, so that the products, when viewed by their categories in a grid view, can be searched by their "stock" status, so that all "in stock" products are shown first.

The code that we're working with is:

class OrderWpscProducts {

// flag for when a shop products post query is being created
private $isProductQuery = false;

public function __construct() {
    // hooks for altering order of product pages
    add_filter('parse_query', array($this, 'filterProductQueryMark'), 20);
    add_filter('posts_join', array($this, 'filterProductQueryJoins'), 20);
    add_filter('posts_orderby', array($this, 'filterProductQueryOrderBy'), 20);
}

/**
* detect a posts query that should return a list of shop products,
* and mark it for later filters
* @param WP_Query $query
* @return WP_Query
*/
public function filterProductQueryMark($query) {
    $this->isProductQuery = false;

    if (!is_admin() && (wpsc_is_in_category() || !empty($query->query_vars['wpsc_wine_style']))) {
        $this->isProductQuery = true;
    }
}

/**
* if the posts query is for a list of shop products,
* then add some tables to the join so that we can sort by category name
* @param string $joins
* @return string
*/
public function filterProductQueryJoins($joins) {
    global $wpdb;

    if ($this->isProductQuery) {
        $joins .= "
INNER JOIN $wpdb->term_relationships wpsc_cat_rel ON wp_posts.ID = wpsc_cat_rel.object_id
INNER JOIN $wpdb->term_taxonomy wpsc_cat_tax ON wpsc_cat_tax.term_taxonomy_id =                 wpsc_cat_rel.term_taxonomy_id
and wpsc_cat_tax.taxonomy = 'wpsc_product_category'
INNER JOIN $wpdb->terms wpsc_cat ON wpsc_cat.term_id = wpsc_cat_tax.term_id

"; }

    return $joins;
}

/**
* if the posts query is for a list of shop products, then sort by category name and product title
* @param string $orderby
* @return string
*/
public function filterProductQueryOrderBy($orderby) {
    if ($this->isProductQuery) {
        $orderby = 'wpsc_cat.name ASC, wp_posts.post_title ASC';
    }

    return $orderby;
}
}

// create instance, will be kept alive by hooks
new OrderWpscProducts(); 

A better approach, would be to create a filter, to achieve, the above.

For a filter, we have this so far:

add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
global $wpdb;
// only change query on wp-e-commerce loops
if (!is_admin() && (is_productQuery() || is_wpsc_product_category() || is_product_tag() ||    is_wpsc_cat.taxonomy_id())) {
    $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID =  istockstatus.post_id) ";
    $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
    $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
}
return $posts_clauses;
}

Still very new to coding, so any help or suggestions, would be more than welcomed.

Tried two different approaches, as mentioned above, and was hoping that the in stock products would show first, when viewing the products in a category grid view.

0

There are 0 best solutions below