Skip to content

Commit b661a36

Browse files
committed
Finally added in the query vars to execute the "onsale" arguments. #377
1 parent 24d69ed commit b661a36

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

includes/classes/blocks/class-registration.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@ class Registration {
1111

1212
protected $disabled = [];
1313

14+
/**
15+
* Holds the array of featured queries.
16+
*
17+
* @var array
18+
*/
1419
protected $featured = [];
1520

21+
/**
22+
* True if the current query outputting needs to be onsale.
23+
*
24+
* @var boolean
25+
*/
26+
protected $onsale = false;
27+
1628
/**
1729
* Initialize the plugin by setting localization, filters, and administration functions.
1830
*
@@ -22,10 +34,12 @@ class Registration {
2234
*/
2335
public function __construct() {
2436
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_variations_script' ), 10 );
25-
add_filter( 'query_loop_block_query_vars', array( $this, 'query_args_filter' ), 10, 2 );
37+
add_filter( 'query_loop_block_query_vars', array( $this, 'query_args_filter' ), 1, 2 );
2638
add_filter( 'render_block', array( $this, 'maybe_hide_varitaion' ), 10, 3 );
2739

2840
add_filter( 'posts_pre_query', array( $this, 'posts_pre_query' ), 10, 2 );
41+
42+
add_filter( 'render_block_data', array( $this, 'save_onsale_queries' ), 10, 1 );
2943
}
3044

3145
/**
@@ -113,6 +127,7 @@ public function query_args_filter( $query, $block ) {
113127
// We only restric this on the destination post type, in case the block is used on a landing page.
114128
if ( 'destination' === get_post_type() ) {
115129
$query['post_parent__in'] = [ get_the_ID() ];
130+
$query['post__not_in'] = [ get_the_ID() ];
116131
}
117132
break;
118133

@@ -240,6 +255,22 @@ public function query_args_filter( $query, $block ) {
240255
break;
241256
}
242257

258+
// Look for the "on sale" CSS class.
259+
if ( true === $this->onsale ) {
260+
if ( isset( $query['meta_query']['relation'] ) ) {
261+
$query['meta_query']['relation'] = 'AND';
262+
}
263+
$query['meta_query'][] = array(
264+
'key' => 'sale_price',
265+
'compare' => 'EXISTS',
266+
);
267+
268+
// reset this to false for the next query.
269+
$this->onsale = false;
270+
}
271+
272+
do_action( 'qm/debug', $query );
273+
243274
return $query;
244275
}
245276

@@ -432,4 +463,33 @@ public function find_featured_items( $query ) {
432463
}
433464
return $items;
434465
}
466+
467+
/**
468+
* This function looks at the query blocks CSS classes to determine if it is onsale.
469+
*
470+
* @param array $parsed_block
471+
* @return array
472+
*/
473+
public function save_onsale_queries( $parsed_block ) {
474+
if ( ! isset( $parsed_block['blockName'] ) || ! isset( $parsed_block['attrs'] ) ) {
475+
return $parsed_block;
476+
}
477+
$allowed_blocks = array(
478+
'core/query',
479+
);
480+
481+
if ( ! in_array( $parsed_block['blockName'], $allowed_blocks, true ) ) {
482+
return $parsed_block;
483+
}
484+
if ( ! isset( $parsed_block['attrs']['className'] ) || '' === $parsed_block['attrs']['className'] || false === $parsed_block['attrs']['className'] ) {
485+
return $parsed_block;
486+
}
487+
488+
$this->onsale = false;
489+
490+
if ( false !== stripos( $parsed_block['attrs']['className'], 'on-sale' ) ) {
491+
$this->onsale = true;
492+
}
493+
return $parsed_block;
494+
}
435495
}

includes/classes/legacy/class-accommodation.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ public function price_filter( $html = '', $meta_key = false, $value = false, $be
123123
'single_supplement'
124124
];
125125

126-
do_action( 'qm/debug', [ get_post_type(), $meta_key ] );
127-
128126
if ( get_post_type() === 'accommodation' && in_array( $meta_key, $currency_fields ) ) {
129127
$price_type = get_post_meta( get_the_ID(), 'price_type', true );
130128
$value = preg_replace( '/[^0-9,.]/', '', $value );

0 commit comments

Comments
 (0)