-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
221 lines (178 loc) · 7.14 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Plugin Name: SL Product & Category Accordion
* Description: Category and product accordion listing with quantities
* Version: 1.0
* Author: SemperLabs
* Author URI: https://semperlabs.com
* Text Domain: sl_product_accordion
* Domain Path: /languages/
**/
class SL_Product_Accordion extends WP_Widget {
public function __construct() {
/* ajax calls */
add_action( 'wp_ajax_sl_get_category_data', array($this, 'sl_get_category_data' ));
add_action( 'wp_ajax_nopriv_sl_get_category_data', array($this, 'sl_get_category_data' ));
$widget_ops = array(
'classname' => 'sl_product_accordion',
'description' => 'Category and product accordion listing with quantities'
);
parent::__construct('sl_product_accordion', 'SL Product & Category Accordion', $widget_ops);
}
public function sl_load_scripts_and_styles() {
wp_enqueue_style('sl_styles', plugin_dir_url( __FILE__ ) . 'main.css');
wp_enqueue_script('sl_scripts', plugin_dir_url( __FILE__ ) . '/js/events.js', array('jquery'));
/* localize path for ajax calls */
wp_register_script( 'sl_acc_localize', plugin_dir_url( __FILE__ ) . '/js/vars.js' );
wp_localize_script( 'sl_acc_localize', 'ajax_url', admin_url( 'admin-ajax.php' ));
wp_enqueue_script( 'sl_acc_localize');
}
/* get stock quantity of regular products */
public function sl_get_stock_quantity( $cat ) {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'product_cat' => $cat
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$stock += $product->get_stock_quantity();
endwhile;
wp_reset_query();
return $stock ? $stock : 0;
}
/* get stock quantity of sale products */
public function sl_get_stock_quantity_sale_products() {
}
/* ajax */
public function sl_cat_has_children( $term_id = 0, $taxonomy = 'product_cat' ) {
$children = get_categories(
array(
'child_of' => $term_id,
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'ids'
)
);
return $children;
}
public function sl_get_category_data() {
if ( isset($_REQUEST) ) {
// get data for selected category - $_POST['category_id']
$selected_cat = $_POST['category_id'];
$args = array(
'taxonomy' => 'product_cat',
'order_by' => 'name',
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0,
'parent' => $selected_cat,
'child_of' => 0
);
$categories = get_categories( $args );
$parsed_data = [];
foreach ( $categories as $cat ) {
// check if has children
$has_subcategories = $this->sl_cat_has_children( $cat->term_id ) ? 'fas fa-chevron-down' : false;
$quantity = $cat->count . "/" . $this->sl_get_stock_quantity($cat_4->name);
$data = array(
'id' => $cat->term_id,
'slug' => $cat->slug,
'name' => $cat->name,
'url' => get_category_link( $cat->term_id ),
'is_on_sale' => false,
'icon' => $has_subcategories,
'quantity' => $quantity
);
array_push( $parsed_data, $data );
}
echo json_encode($parsed_data);
wp_die();
}
}
/* main */
public function widget( $args, $instance ) {
$this->sl_load_scripts_and_styles();
$icon = "fas fa-chevron-down";
?>
<!-- structure -->
<div class="sl-accordion-wrapper">
<div class="sl-header">
<i class="fas fa-shopping-cart"></i><span>Online shop</span>
</div>
<!-- top level categories -->
<?php
$args = array(
'taxonomy' => 'product_cat',
'order_by' => 'name',
'hierarchical' => 1,
'title_li' => '',
'hide_empty' => 0,
'parent' => 0,
'exclude' => 15
);
$top_categories = get_categories( $args );
?>
<div class="sl-acc-ul-wrapper">
<ul>
<?php
foreach ( $top_categories as $cat_1 ) {
?>
<li class="sub">
<a href="#" data-sl-category-id="<?php echo $cat_1->term_id ?>">
<i class="<?php echo $icon ?>"></i>
<span class="category-link" data-slug="<?php echo get_term_link($cat_1->slug, $cat_1->taxonomy) ?>">
<?php echo $cat_1->name ?>
</span>
<div class="sl-quantities"><?php echo $cat_1->count . "/" . $this->sl_get_stock_quantity($cat_1->name) ?></div>
</a>
</li>
<?php
}
?>
</ul>
<?php
// products on sale
$args_sale = array(
'limit' => -1
);
$sale_products = wc_get_products( $args_sale );
?>
<li class="sub sl-acc-sale-items">
<a href="#">
<i class="<?php echo $icon ?>"></i>
<span class="category-link" data-slug="proizvodi-na-akciji">PROIZVODI NA AKCIJI</span>
<div class="sl-quantities"><?php echo count( $sale_products ) ?></div>
</a>
<?php
if ( $sale_products ) {
?>
<ul class="sub-categories sl-sale-products">
<?php
foreach ( $sale_products as $sp ) {
if ( $sp->is_on_sale() ) {
?>
<li class="sub">
<a href="#">
<span class="category-link" data-slug="<?php echo get_permalink($sp->id) ?>">
<?php echo $sp->name ?>
</span>
</a>
<?php
}
}
}
?>
</ul></li>
</div>
</div>
<?php
}
}
add_action(
'widgets_init', function() {
register_widget('SL_Product_Accordion');
}
);
?>