Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. //Sort products without stock WooCommerce
  2. class iWC_Orderby_Stock_Status
  3. {
  4. public function __construct()
  5. {
  6. // Check if WooCommerce is active
  7. if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
  8. add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000);
  9. }
  10. }
  11. public function order_by_stock_status($posts_clauses)
  12. {
  13. global $wpdb;
  14. // only change query on WooCommerce loops
  15. if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
  16. $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
  17. $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
  18. $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
  19. }
  20. return $posts_clauses;
  21. }
  22. }
  23. new iWC_Orderby_Stock_Status;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement