forked from hparfr/prestashopBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrestashopBridge.php
203 lines (151 loc) · 5.83 KB
/
PrestashopBridge.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
<?php
namespace Hpar\PrestashopBridge;
use Symfony\Component\HttpFoundation\Request;
class PrestashopBridge {
protected $pathToPrestashop;
protected $id_shop;
public function __construct($pathToPrestashop, $id_shop = 1) {
$this->id_shop = $id_shop;
if (!$pathToPrestashop || $pathToPrestashop == '')
$pathToPrestashop = '.';
$this->pathToPrestashop = $pathToPrestashop;
$this->loadPrestaKernel();
}
/*
* Load Prestashop core files
*/
protected function loadPrestaKernel() {
//if id_shop is not found in $_GET or $_POST
//a redirection will be done in Prestashop/classes/shop/Shop.php:initialize()
//we need also $_SERVER['SERVER_NAME'] for setting the Cookie in the right domain
$currentRequest = Request::createFromGlobals();
//create new HttpFundation\Request
//add id_shop in $_GET
//copy $_SERVER from currentRequest
$cleanRequest = Request::create('', 'GET', array('id_shop'=> $this->id_shop), array(), array(), $currentRequest->server->all());
$cleanRequest->overrideGlobals();
//init prestashop
include($this->pathToPrestashop.'/config/config.inc.php');
}
public function userExist($email) {
$customer = new \Customer();
$authentication = $customer->getByEmail($email);
if (!$authentication)
return false;
return true;
}
public function login($email) {
$customer = new \Customer();
$authentication = $customer->getByEmail($email);
if (!$authentication) //user doesn't exist
return false;
$ctx = \Context::getContext();
$ctx->cookie->id_compare = isset($ctx->cookie->id_compare) ? $ctx->cookie->id_compare: \CompareProduct::getIdCompareByIdCustomer($customer->id);
$ctx->cookie->id_customer = (int)($customer->id);
$ctx->cookie->customer_lastname = $customer->lastname;
$ctx->cookie->customer_firstname = $customer->firstname;
$ctx->cookie->logged = 1;
$customer->logged = 1;
$ctx->cookie->is_guest = $customer->isGuest();
$ctx->cookie->passwd = $customer->passwd;
$ctx->cookie->email = $customer->email;
// Add customer to the context
$ctx->customer = $customer;
$id_cart = (int)\Cart::lastNoneOrderedCart($ctx->customer->id);
if ($id_cart) {
$ctx->cart = new \Cart($id_cart);
} else {
$ctx->cart = new \Cart();
$ctx->cart->id_currency = \Currency::getDefaultCurrency()->id; //mandatory field
}
$ctx->cart->id_customer = (int)$customer->id;
$ctx->cart->secure_key = $customer->secure_key;
$ctx->cart->save();
$ctx->cookie->id_cart = (int)$ctx->cart->id;
\CartRule::autoRemoveFromCart($ctx);
\CartRule::autoAddToCart($ctx);
$ctx->cookie->write();
return true;
}
/**
* @param string password md5 string or null
* if password = null, login will only be possible by the current bridge
*/
public function createUser($email, $lastname, $firstname, $password = null) {
if (\Customer::customerExists($email)) {
return false;
}
$customer = new \Customer();
$customer->active = 1;
$customer->firstname = $firstname;
$customer->lastname = $lastname;
$customer->email = $email;
$customer->active = 1;
$customer->passwd = $password ? $password : md5(bin2hex(openssl_random_pseudo_bytes(10)));
if ($customer->add())
return true;
else
return false;
}
public function logout() {
$ctx = \Context::getContext();
if (!$ctx)
$ctx->customer->logout();
}
//add a product to the cart with quantity and a reference
public function addToCurrentCart($idProduct, $quantity = 1, $ref = null) {
$ctx = \Context::getContext();
$cart = $ctx->cart;
if ($ref) { //do not add twice the same ref
$customiziations = $ctx->cart->getProductCustomization($idProduct);
$customWithRef = array_filter($customiziations, function ($c) use ($ref) {
return $ref == $c['value'];
});
if (!$customWithRef) { //not already present
$cart->addTextFieldToProduct($idProduct, 1, \Product::CUSTOMIZE_TEXTFIELD, $ref);
$cart->updateQty($quantity, $idProduct);
} else { //already present, remplace quantity (if needed)
$custom = $customWithRef[0];
$qtyDiff = $quantity - $custom['quantity'];
$upOrDown = ($qtyDiff > 0) ? 'up' : 'down'; //updateQty only change relatively
if ($qtyDiff !== 0)
$cart->updateQty( abs($qtyDiff), $idProduct, null, $custom['id_customization'], $upOrDown);
}
} else
$cart->updateQty($quantity, $idProduct);
}
//add a product to the cart with quantity and a reference as a GUEST
public function addToCart($idProduct, $quantity = 1, $ref = null) {
$ctx = \Context::getContext();//print("\n ctx: ");print_r($ctx);exit;
$guest = new \Guest();
$guest->save();
$ctx->cart = new \Cart();
$ctx->cart->id_currency = \Currency::getDefaultCurrency()->id;
$ctx->cart->id_lang = $ctx->language->id;
$ctx->cart->id_guest = (int)($guest->id);
$ctx->cart->save();
$ctx->cookie->id_cart = (int)$ctx->cart->id;//lerro honekin karritoan bistaratzia lortu dut.
$ctx->cookie->write();
$cart = $ctx->cart;
if ($ref) { //do not add twice the same ref
$customiziations = $ctx->cart->getProductCustomization($idProduct);//print("\n customiziations: ");print_r($customiziations);
$customWithRef = array_filter($customiziations, function ($c) use ($ref) {
return $ref == $c['value'];
});
if (!$customWithRef) { //not already present
$cart->addTextFieldToProduct($idProduct, 1, \Product::CUSTOMIZE_TEXTFIELD, $ref);
$cart->updateQty($quantity, $idProduct);
$ctx->cookie->write();
} else { //already present, remplace quantity (if needed)
$custom = $customWithRef[0];
$qtyDiff = $quantity - $custom['quantity'];
$upOrDown = ($qtyDiff >= 0) ? 'up' : 'down'; //updateQty only change relatively
if ($qtyDiff !== 0)
$cart->updateQty( abs($qtyDiff), $idProduct, null, $custom['id_customization'], $upOrDown);//print("\n cart: ");print_r($cart);
$ctx->cookie->write();
}
} else
$cart->updateQty($quantity, $idProduct);
$ctx->cookie->write();
}
}