-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatalog.php
More file actions
129 lines (113 loc) · 2.96 KB
/
Catalog.php
File metadata and controls
129 lines (113 loc) · 2.96 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
<?php
/**
*
*/
class MdtCatalog extends MdtList
{
private static $categories;
private static $wcproducts;
private static $productlist = null;
public function productsByCategoryId($code, $group = false){
if(!isset($this[$code])){
$this[$code] = MdtProductList::productsByCategoryId($code, $group);
}
return $this[$code];
}
public function listCategories(){
if(!isset(self::$categories)){
self::$categories = new MdtCategoryList();
}
return self::$categories;
}
public function fill($group = true){
$categoriesToLoad = array_diff(self::listCategories()->keys(), $this->keys());
if($categoriesToLoad != []){
/**/
$catalog = [];
foreach ($categoriesToLoad as $code) {
self::productsByCategoryId($code, $group);
}
/**/
/*/
$catalog = (new MdtConnection('productsByCategoryId'))->multipleSelfRequest($categoriesToLoad);
foreach ($catalog as $code => $products)
$this[$code] = new MdtProductList($products, $group);
/**/
}
return $this;
}
public function productList(){
if(self::$productlist){
return self::$productlist;
}
$list = new MdtProductList();
foreach($this->fill() as $code => $products){
$list->join($products);
}
return self::$productlist = $list;
}
public function lostInWc(){
$storedProducts = self::getStoredProducts();
$orphanIds = array_diff( $storedProducts->keys(), $this->productList()->keys() );
$orphans = [];
foreach ($orphanIds as $value) {
$orphans[$value] = $storedProducts[$value];
}
return new MdtProductList($orphans);
}
public function forAllCategories($what, $ids = null, $fields = null){
//self::fill();
$r = [];
foreach (self::fill() as $key => $value) {
if($value = $value->{$what}($ids, $fields))
$r[$key] = $value;
continue;
}
return $r;
}
/*
public function forAllCategories($what, $ids = null, $fields = null){
$r = [];
foreach (self::listCategories() as $key => $value) {
if($value = self::productsByCategoryI($key))
$r[$key] = $value->{$what}($ids, $fields);
continue;
}
return $r;
}
*/
static public function getStoredProducts(){
global $wpdb;
$query = $wpdb->get_results(
" SELECT posts.*, meta.meta_value AS msxid
FROM $wpdb->posts as posts
INNER JOIN $wpdb->postmeta as meta
ON ( posts.ID = meta.post_id )
WHERE meta.meta_key = 'msx-id'
AND posts.post_type IN ('product', 'product_variation')
GROUP BY posts.ID
ORDER BY posts.post_date
DESC "
);
$products = [];
if( count($query) )
foreach ($query as $value)
$products[ $value->msxid ] = new WP_Post($value);
return self::$wcproducts = new MdtProductList($products);
}
public function findProductsByIds($ids){
$list = $this->productList();
$result = new MdtProductList();
foreach ($ids as $id) {
$result[$id] = $list[$id];
}
return $result;
}
public function __construct($data = [])
{
require_once 'Product.php';
require_once 'Category.php';
parent::__construct($data);
}
}
?>