Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Price range widget checks variations too #937

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions wpsc-components/theme-engine-v1/widgets/price_range_widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,31 @@ function wpsc_price_range( $args = null ) {

}

if(isset($_GET['range'])){
if ( isset( $_GET['range'] ) ) {
add_filter( 'posts_where', 'wpsc_range_where' );
}

function wpsc_range_where( $where ) {
global $wpdb, $wp_query;
$range = explode('-', $_GET['range']);
if(!strpos($where,'wpsc-product'))
return $where;
if(!$range[0]){
$where .= " AND $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key=\"_wpsc_price\" AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") ";
}elseif(!$range[1]){
$where .= " AND $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key=\"_wpsc_price\" AND $wpdb->postmeta.meta_value > " . ( $range[0]-1 ) . ") ";
}elseif($range[1] && $range[0]){
$where .= " AND $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key=\"_wpsc_price\" AND $wpdb->postmeta.meta_value > " . ( $range[0]-1 ) . " AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") ";
global $wpdb;

if ( ! strpos( $where, 'wpsc-product' ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to check if ( false === strpos( $where, 'wpsc-product' ) )

return $where;
}

$range = explode( '-', $_GET['range'] );

if ( empty( $range ) || $range[0] == 'all' ) {
return $where;
}

if ( ! $range[0] ) {
$where .= " AND ( $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") OR $wpdb->posts.id IN ( SELECT $wpdb->posts.post_parent FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->posts.post_type = 'wpsc-product' AND $wpdb->posts.post_status = 'inherit' AND $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") ) ";
} elseif ( ! $range[1] ) {
$where .= " AND ( $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value > " . ( $range[0] - 1 ) . ") OR $wpdb->posts.id IN ( SELECT $wpdb->posts.post_parent FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->posts.post_type = 'wpsc-product' AND $wpdb->posts.post_status = 'inherit' AND $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value > " . ( $range[0] - 1 ) . ") ) ";
} elseif( $range[1] && $range[0] ) {
$where .= " AND ( $wpdb->posts.id IN ( SELECT $wpdb->posts.id FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value > " . ( $range[0] - 1 ) . " AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") OR $wpdb->posts.id IN ( SELECT $wpdb->posts.post_parent FROM $wpdb->posts JOIN $wpdb->postmeta on $wpdb->postmeta.post_id = $wpdb->posts.id WHERE $wpdb->posts.post_type = 'wpsc-product' AND $wpdb->posts.post_status = 'inherit' AND $wpdb->postmeta.meta_key='_wpsc_price' AND $wpdb->postmeta.meta_value > " . ( $range[0] - 1 ) . " AND $wpdb->postmeta.meta_value < " . ( $range[1] + 1 ) . ") ) ";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these queries to be much more computationally expensive. It would be weird and likely just as expensive to use WP_Meta_Query, though we'd get the benefit of caching. Rather than do that, why don't we set a cache key for the range requested and cache the results of the query?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean to Transients API for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, transients would be fine. Be sure to invalidate the transient whenever updating a new price though.


return $where;
}
?>
?>