1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Model\Product; |
4
|
|
|
|
5
|
|
|
use SilverShop\Page\Product; |
6
|
|
|
use SilverStripe\Versioned\Versioned; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Product - Order Item |
10
|
|
|
* Connects a product to an orde. |
11
|
|
|
* |
12
|
|
|
* @property int $ProductVersion |
13
|
|
|
* @property int $ProductID |
14
|
|
|
*/ |
15
|
|
|
class OrderItem extends \SilverShop\Model\OrderItem |
16
|
|
|
{ |
17
|
|
|
private static $db = [ |
|
|
|
|
18
|
|
|
'ProductVersion' => 'Int', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
private static $has_one = [ |
|
|
|
|
22
|
|
|
'Product' => Product::class, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private static $table_name = 'SilverShop_Product_OrderItem'; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* the has_one join field to identify the buyable |
29
|
|
|
*/ |
30
|
|
|
private static $buyable_relationship = 'Product'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get related product |
34
|
|
|
* - live version if in cart, or |
35
|
|
|
* - historical version if order is placed |
36
|
|
|
* |
37
|
|
|
* @param boolean $forcecurrent - force getting latest version of the product. |
38
|
|
|
* |
39
|
|
|
* @return Product |
40
|
|
|
*/ |
41
|
|
|
public function Product($forcecurrent = false) |
42
|
|
|
{ |
43
|
|
|
//TODO: this might need some unit testing to make sure it compliles with comment description |
44
|
|
|
//ie use live if in cart (however I see no logic for checking cart status) |
45
|
|
|
if ($this->ProductID && $this->ProductVersion && !$forcecurrent) { |
46
|
|
|
return Versioned::get_version(Product::class, $this->ProductID, $this->ProductVersion); |
|
|
|
|
47
|
|
|
} elseif ($this->ProductID |
48
|
|
|
&& $product = Versioned::get_one_by_stage( |
49
|
|
|
Product::class, |
50
|
|
|
'Live', |
51
|
|
|
'"SilverShop_Product"."ID" = ' . $this->ProductID |
52
|
|
|
) |
53
|
|
|
) { |
54
|
|
|
return $product; |
|
|
|
|
55
|
|
|
} |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function onPlacement() |
60
|
|
|
{ |
61
|
|
|
parent::onPlacement(); |
62
|
|
|
if ($product = $this->Product(true)) { |
63
|
|
|
$this->ProductVersion = $product->Version; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getTableTitle() |
68
|
|
|
{ |
69
|
|
|
$product = $this->Product(); |
70
|
|
|
$tabletitle = ($product) ? $product->Title : $this->i18n_singular_name(); |
|
|
|
|
71
|
|
|
$this->extend('updateTableTitle', $tabletitle); |
72
|
|
|
|
73
|
|
|
return $tabletitle; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function Link() |
77
|
|
|
{ |
78
|
|
|
if ($product = $this->Product()) { |
79
|
|
|
return $product->Link(); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|