Wordpress wp-e-commerce plugin move out of stock products to the end of pages

117 views Asked by At

Using the wp-e-commerce plugin below:

https://github.com/wp-e-commerce/WP-e-Commerce

Trying to figure out how to get all of the products which have the "out of stock" status to appear at the end of the pages (when viewing the product categories in grid view), so that the out of stock products appear at the end, and aren't all cluttered up, amongst the "in stock" products.

Messing about with the code below:

$theCategory = get_the_category();

$args = array(
 'post_type'     => 'post',
 'post_status'    => 'publish',
 'cat'          =>$theCategory[0]->term_id,
 'meta_query'    => array(
    'relation' => 'OR',
     array(
         'key'   => 'test_stock',
         'value' => 0,
         'compare' => '!=',
     ),
     array(
         'key'   => 'test_stock',
         'compare' => 'NOT EXISTS',
     )         
 )

);

$query = new WP_Query( $args );

The ideal solution would be a filter to add to the active themes function.php file.

For Woocoomerce which is a different Wordpress shopping cart plugin the following filter can be used, so trying to see if the first section of code that I've posted, could somehow been incorporated into the filter below, to move the out of stock products to the end of the product pages, in our wp-e-commerce store.

Woocommerce filter below:

add_filter('posts_clauses', 'order_by_stock_status');
function order_by_stock_status($posts_clauses) {
global $wpdb;
// only change query on WooCommerce loops
if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || 
is_product_taxonomy())) {
    $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;
}

Would anyone have any suggestions on creating a filter to go in the active Wordpress themes function.php file, which would move all of the "out of stock" products to the end of the pages, so that when customers are browsing through the pages, they don't see the "out of stock" products until the end.

Below is a link, which shows all the shortcodes for the Wordpress wp-e-commerce plugin:

https://wpshortcode.org/wp-e-commerce/

Latest update below:

Have noticed that there is a settings tab within the plugin dashboard, that lets you arrange the product layout by ascending, and descending order, and by price, time uploaded and some other options as well.

Have added the following code below to register the option for "In Stock" which now shows in the settings tab as an option to pick. Just need to work out the code required for this new "In Stock" option to work, so that when the "In Stock" tab is selected, the products are arranged by their stock status, with all the products that are in stock, showing first.

option value="In Stock"  <?php selected( get_option( 
'wpsc_product_order' ), 'In_Stock' ); ?>><?php _ex( 'In Stock', 
'product 
 order setting', 'wp-e-commerce' ); ?></option>

Code for the above, with the other settings below:

    <?php _e( 'Sort Product By', 'wp-e-commerce' ); ?>:
    </th>
    <td>
    <select name='wpsc_options[wpsc_sort_by]'>
    <option <?php if ( isset( $wpsc_sort_by1 ) )
    echo $wpsc_sort_by1; ?> value='name'><?php esc_html_e( 'Name', 'wp- 
    e-commerce' ); ?></option>
    <option <?php if ( isset( $wpsc_sort_by2 ) )
    echo $wpsc_sort_by2; ?> value='price'><?php esc_html_e( 'Price', 
    'wp-e-commerce' ); ?></option>
    <option <?php if ( isset( $wpsc_sort_by4 ) )
    echo $wpsc_sort_by4; ?> value='dragndrop'><?php esc_html_e( 'Drag 
    &amp; Drop', 'wp-e-commerce' ); ?></option>
    <option <?php if ( isset( $wpsc_sort_by3 ) )
    echo $wpsc_sort_by3; ?> value='id'><?php esc_html_e( 'Time 
    Uploaded', 'wp-e-commerce' ); ?></option>
    </select>

    <select name="wpsc_options[wpsc_product_order]">
    <option value="ASC" <?php selected( get_option( 
    'wpsc_product_order' ), 'ASC' ); ?>><?php _ex( 'Ascending', 
    'product order setting', 'wp-e-commerce' ); ?></option>
    <option value="DESC"  <?php selected( get_option( 
    'wpsc_product_order' ), 'DESC' ); ?>><?php _ex( 'Descending', 
    'product order setting', 'wp-e-commerce' ); ?></option>
    <option value="In Stock"  <?php selected( get_option( 
    'wpsc_product_order' ), 'In_Stock' ); ?>><?php _ex( 'In Stock', 
    'product order setting', 'wp-e-commerce' ); ?></option>
    </select>
    </td>
    </tr>

Would anyone have any ideas, or suggestions, on the additional code, required for the products that are in stock (published) to show first, in front of the out of stock products, once the "In Stock" box has been selected?

Any ideas, or suggestions, would be very much appreciated!

0

There are 0 answers