-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimport1c.module
More file actions
150 lines (119 loc) · 5.2 KB
/
import1c.module
File metadata and controls
150 lines (119 loc) · 5.2 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
<?php
function import1c_menu(){
$items['import1c'] = array(
'title' => 'Import 1C',
'page callback' => 'import1c_page',
'access arguments' => array('access content'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
function import1c_page(){
session_start();
if(!empty($_GET['mode'])){
if($_GET['mode']== 'checkauth'){
print "success\n";
print session_name()."\n";
print session_id();
}
if($_GET['mode'] == 'init'){
print "zip=no\n".
"file_limit=100000000\n";
}
if($_GET['mode'] == 'file'){
// Путь до папки куда можно выгрузить картинки
$main_path = 'sites/default/files/import1c';
$main_path = trim($main_path,'/');
$path = explode('/',$_GET['filename']);
array_pop($path);
if(count($path)){
$cur_dir = $main_path;
foreach($path as $dir){
$cur_dir.='/'.$dir;
if(!file_exists($cur_dir))
mkdir($cur_dir);
}
}
$f = fopen('sites/default/files/import1c/'.$_GET['filename'], 'w+');
fwrite($f, file_get_contents('php://input'));
fclose($f);
print "success\n";
/**
* Если товаров немного, то можно загузить их сразу,
* но лучше всего использовать bactch API и делать это
* в пакетном режиме.
*/
if($_GET['filename'] == 'offers.xml'){
$file = 'sites/default/files/import1c/offers.xml';
$xml = simplexml_load_file($file);
foreach($xml->ПакетПредложений->Предложения as $product){
foreach($product as $item){
$product_id = db_select('commerce_product', 'c')
->fields('c')
->condition('c.sku', $item->Ид)
->execute()
->fetchObject();
if(!empty($product_id->product_id)){ //обновляем товар если он есть
$tovar = commerce_product_load($product_id->product_id);
$form_state = array();
$form_state['values'] = array();
$form = array();
$form['#parents'] = array();
$price = array(LANGUAGE_NONE => array(0 => array(
'amount' => $item->Цены->Цена->ЦенаЗаЕдиницу * 100,
'currency_code' => 'RUB',
)));
$form_state['values']['commerce_price'] = $price;
field_attach_submit('product', $tovar, $form, $form_state);
commerce_product_save($tovar);
}else{ //добавляем товар если его нет
$form_state = array();
$form_state['values'] = '';
$form = array();
$form['#parents'] = array();
$new_product = commerce_product_new('product');
$new_product->status = 1;
$new_product->uid = 1;
$new_product->sku = $item->Ид;
$new_product->title = $item->Наименование;
$new_product->type = 'product';
$new_product->created = $new_product->changed = time();
$new_product->language = LANGUAGE_NONE;
$new_product->commerce_price['und'][0]['amount'] = $item->Цены->Цена->ЦенаЗаЕдиницу * 100;
$new_product->commerce_price['und'][0]['currency_code'] = 'RUB';
$new_product->commerce_price['und'][0]['data']['component'] = array();
field_attach_submit('product', $new_product, $form, $form_state);
commerce_product_save($new_product);
$node = new stdClass();
$node->type = 'product';
node_object_prepare($node);
$node->title = $item->Наименование;
$node->language = LANGUAGE_NONE;
$node->field_id['und'][0]['value'] = $item->Ид;
$node->field_id['und'][0]['safe_value'] = $item->Ид;
$node->field_id['und'][0]['format'] = NULL;
$node->body[$node->language][0]['value'] = 'описание товара';
$node->body[$node->language][0]['summary'] = 'описание товара';
$node->body[$node->language][0]['format'] = 'filtered_html';
$node->field_product['und'][0]['product_id'] = $new_product->product_id;
$node->uid = 1;
node_save($node);
}
}
}
} // конец добавления и изменения товаров
if($_GET['filename'] == 'import.xml'){
$file = 'sites/default/files/import1c/import.xml';
$xml = simplexml_load_file($file);
foreach($xml->Каталог->Товары as $product){
foreach($product as $item){
}
}
}
}
if($_GET['mode'] == 'import'){
print "success\n";
}
} // endif not empty mode
exit;
}