forked from tagesjump/magmi-m2
/
grouppriceprocessor.php
executable file
·141 lines (124 loc) · 5.48 KB
/
grouppriceprocessor.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
<?php
/**
* Import group prices for columns names called "group_price:"
*/
class grouppriceprocessor extends Magmi_ItemProcessor
{
protected $_groups = array();
protected $_singleStore;
protected $_priceScope;
protected $_tax_class_id;
public function getPluginInfo()
{
return array('name'=>'Group Price Importer','author'=>'Tim Bezhashvyly,dweeves','version'=>'0.0.4');
}
public function processItemAfterId(&$item, $params = null)
{
$table_name = $this->tablename("catalog_product_entity_group_price");
$group_cols = array_intersect(array_keys($this->_groups), array_keys($item));
$reusableIds = array();
if (!empty($group_cols)) {
$website_ids = $this->_singleStore && $this->_priceScope ? $this->getItemWebsites($item) : array(0);
$group_ids = array();
foreach ($group_cols as $key) {
if ($this->_groups[$key]['id']) {
$group_ids[] = $this->_groups[$key]['id'];
}
}
if (!empty($group_ids)) {
$sql = 'SELECT * FROM ' . $table_name . '
WHERE entity_id=?
AND customer_group_id IN (' . implode(', ', $group_ids) . ')
AND website_id IN (' . implode(', ', $website_ids) . ')';
$rows = $this->select($sql, array($params['product_id']))->fetchAll();
foreach ($rows as $row){
$reusableIds[] = $row['value_id'];
}
//Deleting the records from the table is the best way to do it as we need to handle removed rows, however perhaps we can re-used the IDs...
$sql = 'DELETE FROM ' . $table_name . '
WHERE entity_id=?
AND customer_group_id IN (' . implode(', ', $group_ids) . ')
AND website_id IN (' . implode(', ', $website_ids) . ')';
$this->delete($sql, array($params['product_id']));
}
$sql = 'INSERT INTO ' . $table_name .
' (value_id,entity_id, all_groups, customer_group_id, value, website_id) VALUES ';
$data=array();
$inserts=array();
foreach ($group_cols as $key) {
$price=str_replace(",", ".", $item[$key]);
if (!empty($price)) {
$group_id = $this->_groups[$key]['id'];
foreach ($website_ids as $website_id) {
$inserts[] = '(?,?,?,?,?,?)';
$data[] = count($reusableIds) == 0 ? 0 : array_pop($reusableIds);
$data[] = $params['product_id'];
$data[] = 0;
$data[] = $group_id;
$data[] = $price;
$data[] = $website_id;
}
}
}
//multiple insert
if (!empty($data)) {
$sql .= implode(', ', $inserts);
$sql .= ' ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)';
$this->insert($sql, $data);
}
unset($data);
unset($inserts);
}
return true;
}
public function createGroup($groupname)
{
$cg=$this->tablename('customer_group');
$sql="INSERT INTO $cg (customer_group_code,tax_class_id)
VALUES (?,?)";
$gid=$this->insert($sql, array($groupname, $this->_tax_class_id));
return $gid;
}
/**
* Inspect column list for group price columns info
*
* @param
* $cols
* @param null $params
* @return bool
*/
public function processColumnList(&$cols, $params = null)
{
foreach ($cols as $col) {
if (preg_match("|group_price:(.*)|", $col, $matches)) {
$groupname=$matches[1];
$sql = 'SELECT customer_group_id FROM ' . $this->tablename("customer_group") .
' WHERE UPPER(customer_group_code) = ?';
if ($id = $this->selectone($sql, strtoupper($groupname), "customer_group_id")) {
$this->_groups[$col] = array('name'=>$groupname,'id'=>$id);
} else {
if($groupname == 'NOT LOGGED IN'){
$this->_groups[$col] = array('name'=>$groupname,'id'=>$id);
}
else{
$this->_groups[$col] =
array('name'=>$groupname,'id'=>$this->createGroup($groupname));
}
}
}
}
return true;
}
public function initialize($params)
{
$sql = 'SELECT COUNT(store_id) as cnt FROM ' . $this->tablename('store') . ' WHERE store_id != 0';
$ns = $this->selectOne($sql, array(), "cnt");
$this->_singleStore = $ns == 1;
/* Check price scope in a general config (0 = global, 1 = website) */
$sql = 'SELECT value FROM ' . $this->tablename('core_config_data') . ' WHERE path = ?';
$this->_priceScope = intval($this->selectone($sql, array('catalog/price/scope'), 'value'));
/* Getting customer tax class */
$sql="SELECT class_id FROM " . $this->tablename('tax_class') . " WHERE class_type='CUSTOMER'";
$this->_tax_class_id=$this->selectone($sql, null, 'class_id');
}
}