-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeliveryTime.php
99 lines (88 loc) · 2.88 KB
/
DeliveryTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Netzstrategen\ShopStandards\ProductFilters;
/**
* Widgets functionality.
*/
class DeliveryTime {
const HTTP_QUERY_NAME = 'delivery_time';
/**
* Registers products filter widgets supporting delivery time.
*
* @implemets widgets_init
*/
public static function widgets_init() {
// Registers widget to filter products by delivery time.
register_widget(__NAMESPACE__ . '\WidgetFilterDeliveryTime');
// Overrides layered nav woocommerce widgets with new ones supporting
// woocommerce-german-market delivery time taxonomy terms to be used
// as product filters.
unregister_widget('WC_Widget_Layered_Nav_Filters');
unregister_widget('WC_Widget_Layered_Nav');
register_widget(__NAMESPACE__ . '\WidgetLayeredNav');
register_widget(__NAMESPACE__ . '\WidgetLayeredNavFilters');
}
/**
* Adds custom query variable to filter products by delivery time.
*
* @implements query_vars
*/
public static function query_vars($vars) {
$vars[] = 'delivery_time';
return $vars;
}
/**
* Modifies the main query to allow filtering products by delivery time.
*
* @implements pre_get_posts
*/
public static function pre_get_posts($query) {
if (is_admin() || !$query->is_main_query() || !$filter_values = $_GET['delivery_time'] ?? []) {
return;
}
$filter_values = array_filter(array_map('absint', explode(',', wp_unslash($filter_values))));
$meta_query = [
[
'key' => '_lieferzeit',
'value' => $filter_values,
'compare' => 'IN',
],
[
'relation' => 'OR',
[
'key' => '_shop-standards_back_in_stock_date',
'compare' => 'NOT EXISTS',
],
[
'key' => '_shop-standards_back_in_stock_date',
'value' => date('Y-m-d'),
'compare' => '<=',
],
],
];
$query->set('meta_query', $meta_query);
}
/**
* Adds the passed argument as query parameter to all matched hrefs.
*
* @param string $html_filter
* The content to perform the transformation on.
* @param string $filter_name
* The query parameter to add.
*
* @return string
* Content modified to include given query parameter.
*/
public static function addFilterToNavLinks(string $html_filter, string $filter_name): string {
if (!$filter_args = $_GET[$filter_name] ?? []) {
// Return early if filter is currently not active.
return $html_filter;
}
$filter_args = array_filter(array_map('absint', explode(',', wp_unslash($filter_args))));
// Add query parameter to all found hrefs.
$html_filter = preg_replace_callback('@href="(.+?[^"])"@', function ($match) use ($filter_name, $filter_args) {
$link = 'href="' . esc_url(add_query_arg($filter_name, implode(',', $filter_args), $match[1])) . '"';
return $link;
}, $html_filter);
return $html_filter;
}
}