Last active
August 15, 2023 18:01
-
-
Save ethanpil/651ded7f1e86ff5e1b051f92cce394ad to your computer and use it in GitHub Desktop.
Woocommerce Filter by On Sale
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
<?php | |
/* | |
* Plugin Name: On Sale Filter for WooCommerce Admin | |
* Description: Filter the products list in WooCommerce admin to show items on sale. | |
* Version: 1.0 | |
* Requires at least: 5.2 | |
* Requires PHP: 7.2 | |
*/ | |
// Credit: https://wpsimplehacks.com/how-to-filter-woocommerce-admin-products-by-on-sale/ | |
// Filter Woocommerce admin product by on sale | |
function wpsh_onsale_filter($output) { | |
global $wp_query; | |
$selected = filter_input(INPUT_GET, 'product_sale', FILTER_VALIDATE_INT); | |
if ($selected == false) { | |
$selected = 0; | |
} | |
$output .= ' | |
<select id="dropdown_product_sale" name="product_sale"> | |
<option value="">Filter by sale</option> | |
<option value="1" ' . (($selected === 1) ? 'selected="selected"' : '') . '>On Sale</option> | |
<option value="2" ' . (($selected === 2) ? 'selected="selected"' : '') . '>No Sale</option> | |
</select> | |
'; | |
return $output; | |
} | |
add_action('woocommerce_product_filters', 'wpsh_onsale_filter'); | |
// Woocommerce Filter by on sale where statement | |
function wpsh_onsale_filter_where_statement($where) { | |
global $wp_query, $wpdb; | |
// Get selected value | |
$selected = filter_input(INPUT_GET, 'product_sale', FILTER_VALIDATE_INT); | |
// Only trigger if required | |
if (!is_admin() || get_query_var('post_type') != "product" || !$selected) { | |
return $where; | |
} | |
$todaytimestamp = strtotime(date('Y-m-d', time()) . ' 23:59:59'); | |
$querystr = ' | |
SELECT p.ID, p.post_parent | |
FROM ' . $wpdb->posts . ' p | |
WHERE p.ID IN ( | |
SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price" AND pm.meta_value > \'\' | |
) AND p.ID IN ( | |
SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price_dates_from" AND pm.meta_value <= '.$todaytimestamp.' | |
) AND p.ID IN ( | |
SELECT post_id FROM ' . $wpdb->postmeta . ' pm WHERE pm.meta_key = "_sale_price_dates_to" AND pm.meta_value >= '.$todaytimestamp.' | |
) | |
'; | |
$pageposts = $wpdb->get_results($querystr, OBJECT); | |
$productsIDs = array_map(function($n){ | |
return $n->post_parent > 0 ? $n->post_parent : $n->ID; | |
}, $pageposts); | |
if ($selected == 1) { | |
$where .= ' AND ' . $wpdb->posts . '.ID IN (' . implode(",", $productsIDs) . ') '; | |
} | |
elseif ($selected == 2) { | |
$where .= ' AND ' . $wpdb->posts . '.ID NOT IN (' . implode(",", $productsIDs) . ') '; | |
} | |
return $where; | |
} | |
add_filter('posts_where' , 'wpsh_onsale_filter_where_statement'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment