Last active
December 2, 2023 18:48
-
-
Save softiconic/4c9617b72998daccc16e6a184d4db0ec to your computer and use it in GitHub Desktop.
WooCommerce default setting for stock in orders.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('posts_clauses', 'order_by_stock_status'); | |
function order_by_stock_status($posts_clauses) { | |
global $wpdb; | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment