by
1
4
12k
353
Top 1% !
Popular
Pearl of Wisdom
Specified
Popularity: 343rd place

Published on:

No tags for this snippet yet.
LanguagePHP
LicenseMIT_X11
SourceGitHub

WooCommerce Products Order by Stock Status (in stock products first)

WooCommerce Products Order by Stock Status (in stock products first): 
i-wc-order-by-stock-status.php
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/114858/WooCommerce-Products-Order-by-Stock-Stat?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
<?php /** * Order product collections by stock status, instock products first. */ class iWC_Orderby_Stock_Status { public function __construct() { // Check if WooCommerce is active if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000); } } public 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())) { $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; } } new iWC_Orderby_Stock_Status; ?>
If you want to be updated about similar snippets, Sign in and follow our Channels