AffinityEbayCategory.php
10.8 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
class AffinityEbayCategory {
const ebayCategoryMetaFieldName = "_affinity_ebaycategory";
const ebayCategoryClassName = "AffinityEbayCategory";
public $categoryId;
public $categoryParentId;
public $name;
public $path;
public $level;
public $leaf;
//@Todo
public function getCategoryNameWithParents($separator = ">") {
return "Category Name";
//return $this->name;
}
public static function get($categoryId) {
require_once(__DIR__ . "/../ecommerce-adapters/AffinityDataLayer.php");
return AffinityDataLayer::get(self::ebayCategoryClassName, $categoryId);
}
/*
* @return objEbayCategory / false if not found
*/
public static function getProductEbayCategory($objAffinityProduct) {
$ebayCategoryId = get_post_meta($objAffinityProduct->id, self::ebayCategoryMetaFieldName, true);
if($ebayCategoryId < 1) {
return false;
}
return self::get($ebayCategoryId);
}
static function showWooCatName($cats, $id, $enc=false) {
if (!empty($cats[$id][3])) {
if ($enc) {
return self::showWooCatName($cats, $cats[$id][3]) . ' <span class="ebayaffinity-rightblue">›</span> ' .esc_html($cats[$id][0]);
} else {
return self::showWooCatName($cats, $cats[$id][3]) . ' > ' .$cats[$id][0];
}
} else {
if ($enc) {
return esc_html($cats[$id][0]);
} else {
return $cats[$id][0];
}
}
}
static function getAlleBay($ids = array()) {
global $wpdb;
$sql = "SELECT CONCAT(IF(c1.name IS NOT NULL, IF(LOCATE('Lots More', c1.name) > 0, CONCAT('zzzzzzzzzzz', c1.name), c1.name), ''),
IF(c2.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c2.name) > 0, CONCAT('zzzzzzzzzzz', c2.name), c2.name)), ''),
IF(c3.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c3.name) > 0, CONCAT('zzzzzzzzzzz', c3.name), c3.name)), ''),
IF(c4.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c4.name) > 0, CONCAT('zzzzzzzzzzz', c4.name), c4.name)), ''),
IF(c5.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c5.name) > 0, CONCAT('zzzzzzzzzzz', c5.name), c5.name)), ''),
IF(c6.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c6.name) > 0, CONCAT('zzzzzzzzzzz', c6.name), c6.name)), '')) AS catname,
IF(c6.categoryId IS NOT NULL, c6.categoryId,
IF(c5.categoryId IS NOT NULL, c5.categoryId,
IF(c4.categoryId IS NOT NULL, c4.categoryId,
IF(c3.categoryId IS NOT NULL, c3.categoryId,
IF(c2.categoryId IS NOT NULL, c2.categoryId, c1.categoryId))))) AS categoryId
FROM
".$wpdb->prefix."ebayaffinity_categories AS c1 LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c2 ON (c2.parentCategoryId = c1.categoryId AND c2.level = 2) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c3 ON (c3.parentCategoryId = c2.categoryId AND c3.level = 3) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c4 ON (c4.parentCategoryId = c3.categoryId AND c4.level = 4) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c5 ON (c5.parentCategoryId = c4.categoryId AND c5.level = 5) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c6 ON (c6.parentCategoryId = c5.categoryId AND c6.level = 6)
WHERE c1.parentCategoryID = -1 AND c1.level = 1 ";
if (!empty($ids)) {
$idstr = implode(',', $ids);
$sql .= " AND (c6.categoryId IN (".$idstr.") OR c5.categoryId IN (".$idstr.") OR c4.categoryId IN (".$idstr.") OR c3.categoryId IN (".$idstr.") OR c2.categoryId IN (".$idstr.")) ";
}
$sql .= "ORDER BY catname";
$res = $wpdb->get_results($sql);
foreach ($res as $k=>$v) {
$res[$k]->catname = str_replace('zzzzzzzzzzz', '', $v->catname);
}
return $res;
}
static function getSubsByParent($parentId = -1) {
global $wpdb;
$res = $wpdb->get_results(
$wpdb->prepare(
"SELECT CONCAT(IF(c2.name IS NOT NULL, IF(LOCATE('Other', c2.name) > 0, CONCAT('zzzzzzzzzzz', c2.name), c2.name), ''),
IF(c3.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c3.name) > 0, CONCAT('zzzzzzzzzzz', c3.name), c3.name)), ''),
IF(c4.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c4.name) > 0, CONCAT('zzzzzzzzzzz', c4.name), c4.name)), ''),
IF(c5.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c5.name) > 0, CONCAT('zzzzzzzzzzz', c5.name), c5.name)), ''),
IF(c6.name IS NOT NULL, CONCAT(' > ', IF(LOCATE('Other', c6.name) > 0, CONCAT('zzzzzzzzzzz', c6.name), c6.name)), '')) AS catname,
IF(c6.categoryId IS NOT NULL, c6.categoryId,
IF(c5.categoryId IS NOT NULL, c5.categoryId,
IF(c4.categoryId IS NOT NULL, c4.categoryId,
IF(c3.categoryId IS NOT NULL, c3.categoryId, c2.categoryId)))) AS categoryId
FROM ".$wpdb->prefix."ebayaffinity_categories AS c2 LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c3 ON (c3.parentCategoryId = c2.categoryId AND c3.level = 3) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c4 ON (c4.parentCategoryId = c3.categoryId AND c4.level = 4) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c5 ON (c5.parentCategoryId = c4.categoryId AND c5.level = 5) LEFT JOIN
".$wpdb->prefix."ebayaffinity_categories AS c6 ON (c6.parentCategoryId = c5.categoryId AND c6.level = 6)
WHERE c2.parentCategoryID = %d AND c2.level = 2
ORDER BY catname", $parentId
)
);
foreach ($res as $k=>$v) {
$res[$k]->catname = str_replace('zzzzzzzzzzz', '', $v->catname);
}
return $res;
}
static function getByParent($parentId = -1) {
global $wpdb;
$res = $wpdb->get_results(
$wpdb->prepare(
"SELECT categoryId, IF(LOCATE('Lots More', name) > 0, CONCAT('zzzzzzzzzzz', name), name) AS name, leaf FROM ".$wpdb->prefix."ebayaffinity_categories WHERE parentCategoryId = %d ORDER BY name", $parentId
)
);
foreach ($res as $k=>$v) {
$res[$k]->name = str_replace('zzzzzzzzzzz', '', $v->name);
}
return $res;
}
static function getEbayMappedCategoryId($ecommerceCategoryId) {
require_once(__DIR__ . "/../ecommerce-adapters/AffinityDataLayer.php");
return AffinityDataLayer::getDataAssociatedToEcommerceTerm($ecommerceCategoryId, self::ebayCategoryMetaFieldName, false);
}
static function mapCategory($term_id, $categoryId) {
update_term_meta($term_id, self::$ebayCategoryFieldName, $categoryId);
}
static function mapProduct($post_id, $categoryId) {
update_post_meta($post_id, self::$ebayCategoryFieldName, $categoryId);
}
static function getUnmappedWCCategories() {
$arr = get_categories(array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 1,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => 1,
));
$arr_o = array();
foreach ($arr as $el) {
$id = get_term_meta($el->term_id, self::$ebayCategoryFieldName, true);
if (empty($id)) {
$arr_o[$el->term_id.''] = html_entity_decode($el->name, ENT_QUOTES, get_option('blog_charset'));
}
}
return $arr_o;
}
static function getCategoriesShipRules() {
$arr = get_categories(array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 1,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => 1,
));
$arr_o = array();
foreach ($arr as $el) {
$id = get_term_meta($el->term_id, '_affinity_shiprule', true);
if (empty($id)) {
$id = 0;
}
$arr_o[$el->term_id.''] = array(html_entity_decode($el->name, ENT_QUOTES, get_option('blog_charset')), $id);
}
return $arr_o;
}
static function getCategoriesTitleRules() {
$arr = get_categories(array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 1,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => 1,
));
$arr_o = array();
foreach ($arr as $el) {
$id = get_term_meta($el->term_id, '_affinity_titlerule', true);
if (empty($id)) {
$id = 0;
}
$arr_o[$el->term_id.''] = array(html_entity_decode($el->name, ENT_QUOTES, get_option('blog_charset')), $id);
}
return $arr_o;
}
static function getCategoriesCatRules() {
$arr = get_categories(array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'show_count' => 1,
'pad_counts' => 0,
'hierarchical' => 0,
'title_li' => '',
'hide_empty' => 1,
));
$arr_o = array();
foreach ($arr as $el) {
$id = get_term_meta($el->term_id, '_affinity_ebaycategory', true);
if (empty($id)) {
$id = 0;
}
$arr_o[$el->term_id.''] = array(html_entity_decode($el->name, ENT_QUOTES, get_option('blog_charset')), $id);
}
return $arr_o;
}
static function getUnmappedWCProducts() {
$exclude_cats = array_keys(self::getUnmappedWCCategories());
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $exclude_cats,
'operator' => 'NOT IN'
),
)
);
$wp_query = new WP_Query($args);
$found = $wp_query->found_posts;
$data = array();
while ($wp_query->have_posts()) {
$wp_query->the_post();
$id = get_the_ID();
$product = new WC_Product(get_the_ID());
$data[] = array(
'id' => $id,
'title' => $product->get_title(),
'img' => $product->get_image(array(64, 64))
);
}
return array($data, $found);
}
static function getUnmappedWCSuggestionsProducts() {
$exclude_cats = array_keys(self::getUnmappedWCCategories());
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $exclude_cats,
'operator' => 'NOT IN'
),
),
'meta_query' => array(
'key' => '_affinity_suggestedCatId',
'compare' => 'EXISTS',
)
);
$wp_query = new WP_Query($args);
$found = $wp_query->found_posts;
$data = array();
while ($wp_query->have_posts()) {
$wp_query->the_post();
$suggestedCatId = get_post_meta(get_the_ID(), '_affinity_suggestedCatId', true);
$id = get_the_ID();
$product = new WC_Product(get_the_ID());
$data[] = array(
'id' => $id,
'title' => $product->get_title(),
'img' => $product->get_image(array(64, 64)),
'suggestedCatId' => $suggestedCatId
);
}
return array($data, $found);
}
}