Skip to content

Commit 3c9d236

Browse files
committed
Add product options / variants
1 parent 062224a commit 3c9d236

File tree

23 files changed

+5994
-63
lines changed

23 files changed

+5994
-63
lines changed

app/Libraries/Cart.php

Lines changed: 132 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public function __construct()
2525
$this->session = \Config\Services::session();
2626

2727
// Libraries
28-
$this->setting = new \App\Libraries\Setting();
2928
$this->currency = new \App\Libraries\Currency();
29+
$this->language = new \App\Libraries\Language();
30+
$this->setting = new \App\Libraries\Setting();
3031
$this->weight = new \App\Libraries\Weight();
3132

3233
// Remove all expired cart items with no customer_id
@@ -87,7 +88,7 @@ public function __construct()
8788
foreach ($cart_query->getResult() as $result) {
8889
if ($result->seller_id !== $seller_id) {
8990
// Add cart to customer
90-
$this->add($customer_id, $result->seller_id, $result->product_id, $result->quantity);
91+
$this->add($customer_id, $result->seller_id, $result->product_id, $result->quantity, json_decode($result->option, true));
9192
}
9293

9394
// Delete cart
@@ -119,11 +120,16 @@ public function remove($customer_id, $seller_id, $key)
119120
*/
120121
public function add($customer_id, $seller_id, $product_id, $quantity, $options = [])
121122
{
123+
if (is_array($options)) {
124+
asort($options);
125+
}
126+
122127
$cart_builder = $this->db->table('cart');
123128

124129
$cart_builder->where('customer_id', $customer_id);
125130
$cart_builder->where('seller_id', $seller_id);
126131
$cart_builder->where('product_id', $product_id);
132+
$cart_builder->where('option', json_encode($options));
127133
$cart_builder->where('key', $this->getKey());
128134

129135
$cart_query = $cart_builder->get();
@@ -133,10 +139,12 @@ public function add($customer_id, $seller_id, $product_id, $quantity, $options =
133139
$cart_update_builder = $this->db->table('cart');
134140

135141
$cart_update_builder->set('quantity', 'quantity + ' . $quantity, false);
142+
$cart_update_builder->set('date_modified', new Time('now'));
136143

137144
$cart_update_builder->where('customer_id', $customer_id);
138145
$cart_update_builder->where('seller_id', $seller_id);
139146
$cart_update_builder->where('product_id', $product_id);
147+
$cart_update_builder->where('option', json_encode($options));
140148
$cart_update_builder->where('key', $this->getKey());
141149

142150
$cart_update_builder->update();
@@ -150,7 +158,9 @@ public function add($customer_id, $seller_id, $product_id, $quantity, $options =
150158
'key' => $this->getKey(),
151159
'product_id' => $product_id,
152160
'quantity' => $quantity,
161+
'option' => json_encode($options),
153162
'date_added' => new Time('now'),
163+
'date_modified' => new Time('now'),
154164
];
155165

156166
$cart_insert_builder->insert($cart_insert_data);
@@ -231,6 +241,8 @@ public function getProducts($seller_id)
231241
$cart_builder->where('seller_id', $seller_id);
232242
$cart_builder->where('key', $this->getKey());
233243

244+
$cart_builder->orderBy('date_modified', 'DESC');
245+
234246
$cart_query = $cart_builder->get();
235247

236248
foreach ($cart_query->getResult() as $result) {
@@ -248,6 +260,118 @@ public function getProducts($seller_id)
248260
$product_query = $product_builder->get();
249261

250262
if ($product = $product_query->getRow()) {
263+
// Get options
264+
$option_data = [];
265+
266+
if (is_array(json_decode($result->option, true))) {
267+
$options = json_decode($result->option, true);
268+
} else {
269+
$options = [];
270+
}
271+
272+
foreach ($options as $key => $value) {
273+
// Get option
274+
$option_builder = $this->db->table('option o');
275+
276+
$option_builder->where('o.option_id', $key);
277+
278+
$option_query = $option_builder->get();
279+
280+
if ($option_row = $option_query->getRow()) {
281+
// Get option description
282+
$option_description_data = [];
283+
284+
$option_description_builder = $this->db->table('option_description');
285+
286+
$option_description_builder->where('option_id', $key);
287+
288+
$option_description_query = $option_description_builder->get();
289+
290+
foreach ($option_description_query->getResult() as $option_description) {
291+
$option_description_data[$option_description->language_id] = [
292+
'name' => $option_description->name,
293+
];
294+
}
295+
296+
// Get option value
297+
$option_value_data = [];
298+
299+
$option_value_builder = $this->db->table('option_value ov');
300+
301+
$option_value_builder->where('ov.option_id', $key);
302+
$option_value_builder->where('ov.option_value_id', $value);
303+
304+
$option_value_query = $option_value_builder->get();
305+
306+
if ($option_value_row = $option_value_query->getRow()) {
307+
// Get option value description
308+
$option_value_description_data = [];
309+
310+
$option_value_description_builder = $this->db->table('option_value_description');
311+
312+
$option_value_description_builder->where('option_id', $key);
313+
$option_value_description_builder->where('option_value_id', $value);
314+
315+
$option_value_description_query = $option_value_description_builder->get();
316+
317+
foreach ($option_value_description_query->getResult() as $option_value_description) {
318+
$option_value_description_data[$option_value_description->language_id] = [
319+
'name' => $option_value_description->name,
320+
];
321+
}
322+
323+
$option_value_data = [
324+
'option_id' => $option_value_row->option_id,
325+
'option_value_id' => $option_value_row->option_value_id,
326+
'seller_id' => $option_value_row->seller_id,
327+
'customer_id' => $option_value_row->customer_id,
328+
'description' => $option_value_description_data,
329+
'sort_order' => $option_value_row->sort_order,
330+
'status' => $option_value_row->status,
331+
];
332+
}
333+
334+
$option_data[] = [
335+
'option_id' => $option_row->option_id,
336+
'seller_id' => $option_row->seller_id,
337+
'customer_id' => $option_row->customer_id,
338+
'description' => $option_description_data,
339+
'sort_order' => $option_row->sort_order,
340+
'status' => $option_row->status,
341+
'option_value' => $option_value_data,
342+
];
343+
}
344+
}
345+
346+
$option_data_sort_order = [];
347+
348+
foreach ($option_data as $key => $value) {
349+
$option_data_sort_order[$key] = $value['sort_order'];
350+
}
351+
352+
array_multisort($option_data_sort_order, SORT_ASC, $option_data);
353+
354+
// Product variant
355+
if (!empty($product->product_option)) {
356+
// Get product variant info
357+
$product_variant_builder = $this->db->table('product_variant');
358+
359+
$product_variant_builder->where('options', $result->option);
360+
361+
$product_variant_query = $product_variant_builder->get();
362+
363+
if ($product_variant_row = $product_variant_query->getRow()) {
364+
$price = $product_variant_row->price;
365+
$weight = $product_variant_row->weight;
366+
} else {
367+
$price = $product->price;
368+
$weight = $product->weight;
369+
}
370+
} else {
371+
$price = $product->price;
372+
$weight = $product->weight;
373+
}
374+
251375
$products[] = [
252376
'cart_id' => $result->cart_id,
253377
'customer_id' => $result->customer_id,
@@ -256,13 +380,16 @@ public function getProducts($seller_id)
256380
'name' => $product->name,
257381
'description' => $product->description,
258382
'slug' => $product->slug,
259-
'price' => $product->price,
260-
'weight' => $product->weight,
383+
'product_option' => $product->product_option,
384+
'price' => $price,
385+
'weight' => $weight,
261386
'weight_class_id' => $product->weight_class_id,
262387
'main_image' => $product->main_image,
263388
'quantity' => $result->quantity,
264-
'total' => $product->price * $result->quantity,
389+
'total' => $price * $result->quantity,
265390
'date_added' => $result->date_added,
391+
'option' => $option_data,
392+
'option_ids' => $result->option,
266393
];
267394
}
268395
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace Main\Marketplace\Controllers\Checkout;
4+
5+
class Cart extends \App\Controllers\BaseController
6+
{
7+
/**
8+
* Constructor.
9+
*/
10+
public function __construct()
11+
{
12+
// Model
13+
$this->model_product_product = new \Main\Marketplace\Models\Product\Product_Model();
14+
}
15+
16+
public function index()
17+
{
18+
$breadcrumbs[] = array(
19+
'text' => lang('Text.home', [], $this->language->getCurrentCode()),
20+
'href' => $this->url->customerLink('/'),
21+
'active' => false,
22+
);
23+
24+
$breadcrumbs[] = array(
25+
'text' => lang('Text.shopping_cart', [], $this->language->getCurrentCode()),
26+
'href' => $this->url->customerLink('marketplace/checkout/cart', '', true),
27+
'active' => true,
28+
);
29+
30+
$data['heading_title'] = lang('Heading.shopping_cart', [], $this->language->getCurrentCode());
31+
32+
// Get cart sellers
33+
$data['sellers'] = [];
34+
35+
$sellers = $this->cart->getSellers();
36+
37+
foreach ($sellers as $seller) {
38+
// Get cart products
39+
$product_data = [];
40+
41+
$products = $this->cart->getProducts($seller['seller_id']);
42+
43+
foreach ($products as $product) {
44+
// Image
45+
if (ROOTPATH . 'public/assets/images' . $product['main_image']) {
46+
$thumb = $this->image->resize($product['main_image'], 64, 64, true);
47+
} else {
48+
$thumb = $this->image->resize('no_image.png', 64, 64, true);
49+
}
50+
51+
$product_data[] = [
52+
'product_id' => $product['product_id'],
53+
'name' => $product['name'],
54+
'thumb' => $thumb,
55+
'price' => $this->currency->format($product['price'], $this->currency->getCurrentCode()),
56+
'quantity' => $product['quantity'],
57+
'total' => $this->currency->format($product['total'], $this->currency->getCurrentCode()),
58+
'option' => $product['option'],
59+
'href' => $this->url->customerLink('marketplace/product/product/get/' . $product['slug'] . '-p' . $product['product_id']),
60+
];
61+
}
62+
63+
$data['sellers'][] = [
64+
'seller_id' => $seller['seller_id'],
65+
'store_name' => $seller['store_name'],
66+
'product' => $product_data,
67+
'checkout' => $this->url->customerLink('marketplace/checkout/checkout', ['seller_id' => $seller['seller_id']], true),
68+
];
69+
}
70+
71+
$data['shopping_cart'] = $this->url->customerLink('marketplace/checkout/cart', '', true);
72+
$data['checkout'] = $this->url->customerLink('marketplace/checkout/checkout', '', true);
73+
74+
$data['language'] = $this->language;
75+
76+
// Header
77+
$header_params = array(
78+
'title' => lang('Heading.shopping_cart', [], $this->language->getCurrentCode()),
79+
'breadcrumbs' => $breadcrumbs,
80+
);
81+
$data['header'] = $this->marketplace_header->index($header_params);
82+
// Footer
83+
$footer_params = array();
84+
$data['footer'] = $this->marketplace_footer->index($footer_params);
85+
86+
return $this->template->render('ThemeMarketplace', 'com_openmvm', 'Basic', 'Checkout\cart', $data);
87+
}
88+
89+
public function add()
90+
{
91+
$json = [];
92+
93+
// Get customer id
94+
if ($this->customer->isLoggedin()) {
95+
$customer_id = $this->customer->getId();
96+
} else {
97+
$customer_id = 0;
98+
}
99+
100+
// Get product info
101+
if (!empty($this->request->getGet('product_id'))) {
102+
$product_id = $this->request->getGet('product_id');
103+
} else {
104+
$product_id = 0;
105+
}
106+
107+
$product_info = $this->model_product_product->getProduct($product_id);
108+
109+
// If product not found
110+
if (!$product_info) {
111+
$json['error'] = lang('Error.product_not_found', [], $this->language->getCurrentCode());
112+
}
113+
114+
// If customer is the seller of this product
115+
if ($this->customer->getSellerId() == $product_info['seller_id']) {
116+
$json['error'] = lang('Error.customer_is_seller', [], $this->language->getCurrentCode());
117+
}
118+
119+
if (empty($json['error'])) {
120+
$this->cart->add($customer_id, $product_info['seller_id'], $product_info['product_id'], $this->request->getPost('quantity'), $this->request->getPost('product_variant'));
121+
122+
$json['success'] = lang('Success.add_to_cart', [], $this->language->getCurrentCode());
123+
}
124+
125+
return $this->response->setJSON($json);
126+
}
127+
}

0 commit comments

Comments
 (0)