-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathPrestashopBridge.php
176 lines (135 loc) · 4.58 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
<?php
namespace Hpar\PrestashopBridge;
use Symfony\Component\HttpFoundation\Request;
class PrestashopBridge {
protected $pathToPrestashop;
protected $id_shop;
/**
* @param string pathToPrestashop : path to prestashop source code
* @param int idShop: id of the shop
*/
public function __construct($pathToPrestashop, $idShop = 1) {
$this->idShop = $idShop;
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->idShop), array(), array(), $currentRequest->server->all());
$cleanRequest->overrideGlobals();
//init prestashop
include($this->pathToPrestashop.'/config/config.inc.php');
}
/**
* @param string email
*/
public function userExist($email) {
$customer = new \Customer();
$authentication = $customer->getByEmail($email);
if (!$authentication)
return false;
return true;
}
/**
* @param string email
*/
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 email
* @param string lastname
* @param string firstname
* @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
* @param int idProduct
* @param in quantity
* @param string ref
*/
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);
}
}