Advertisement
Evengar

Untitled

Feb 17th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.08 KB | None | 0 0
  1. /**
  2. * Sorting out of stock WooCommerce products - Order product collections by stock status, in-stock products first.
  3. */
  4. class iWC_Orderby_Stock_Status
  5. {
  6. public function __construct()
  7. {
  8. // Check if WooCommerce is active
  9. if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
  10. add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000);
  11. }
  12. }
  13. public function order_by_stock_status($posts_clauses)
  14. {
  15. global $wpdb;
  16. // only change query on WooCommerce loops
  17. if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) {
  18. $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
  19. $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby'];
  20. $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where'];
  21. }
  22. return $posts_clauses;
  23. }
  24. }
  25. new iWC_Orderby_Stock_Status;
  26. /**
  27. * END - Order product collections by stock status, instock products first.
  28. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement