-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathShoppingCart.php
103 lines (83 loc) · 3.51 KB
/
ShoppingCart.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
<?php
namespace Silvershop\SimpleOptions;
use Silvershop\SimpleOptions\Extensions\OrderExtension;
class ShoppingCart extends \ShoppingCart
{
/**
* Finds an existing order item.
*
* This method is originally responsible for ensuring unique order items are created correctly
*
* However, if we want to check for simple options, we have to do some extra checks
*
*
* @param \Buyable $buyable
* @param array $customfilter
*
* @return \OrderItem | bool (the item requested, or false)
*/
public function get(\Buyable $buyable, $customfilter = array())
{
$order = $this->current();
if (!$buyable || !$order) {
return false;
}
$buyable = $this->getCorrectBuyable($buyable);
$filter = array(
'OrderID' => $order->ID,
);
$itemclass = \Config::inst()->get(get_class($buyable), 'order_item');
$relationship = \Config::inst()->get($itemclass, 'buyable_relationship');
$filter[$relationship . "ID"] = $buyable->ID;
$required = array('Order', $relationship);
if (is_array($itemclass::config()->required_fields)) {
$required = array_merge($required, $itemclass::config()->required_fields);
}
$query = new \MatchObjectFilter($itemclass, array_merge($customfilter, $filter), $required);
// ==== From here on the code might be different from default class
$items = $itemclass::get()->where($query->getFilter());
$item = $items->first();
if (!$item) {
return $this->error(_t("ShoppingCart.ItemNotFound", "Item not found."));
}
// do check if buyable even has module or options, else no point in doing more code
if (!$buyable->hasExtension("Silvershop\SimpleOptions\Extensions\ProductOptionsExtension") || !$buyable->ProductOptions()->exists()) {
return $item;
}
// might be bool on cart creation..
if (!$item instanceof \OrderItem) {
return $item;
}
return $this->handleItemsOptionsData($items, $customfilter);
}
protected function handleItemsOptionsData($items, $customfilter)
{
$item = $items->first();
$submittedOptions = array_column(OrderExtension::filterDataForSimpleOptions($customfilter), "ValueID");
sort($submittedOptions);
// default behavior just selects items with correct ProductID, there might be multiple
// so check if an item exists with the same options as submitted options
// If so, that item should be selected
foreach($items as $orderitem){
$values = $orderitem->OrderOptionDataItems()->column("SimpleProductOptionValueID");
sort($values);
// compare options
if($submittedOptions == $values){
$item = $orderitem;
break;
}
}
// if it has no options set, then whatever
if (!$item->OrderOptionDataItems()->exists()) {
return $item;
}
$existingValues = $item->OrderOptionDataItems()->column("SimpleProductOptionValueID");
sort($existingValues);
// if the submitted options are the same as existing attached options, then just return the item
// if submitted options are different, please create a new order item
if ($submittedOptions != $existingValues) {
return $this->error(_t("ShoppingCart.ItemNotFound", "Item not found."));
}
return $item;
}
}