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
// Order.php

<?php
class Order extends DataObject {
	static $db = array(
		'Title' => 'Text',
		'Session' => 'Text'
	);
	
	static $has_many = array(
		"Items" => "Order_Item"
	);
}


class Order_Item extends DataObject {
	static $has_one = array(
		"Order" => "Order", // Internal field becomes OrderID, not Order
		"ProductPage" => "ProductPage",
	);
}



// ProductPage.php
<?php
class ProductPage extends Page {
	static $icon = '/ssshop/images/product';
	
	static $db = array(
		'Price' => 'Float'
	);
	
	static $has_many = array(
		'ProductVariations' => 'ProductVariations'
	);
	
	static $belongs_many_many = array(
		'Orders' => 'Order'
	);
	
	
	function getCMSFields() {
		$f = parent::getCMSFields();
		$f->addFieldToTab("Root.Content.Main", new NumericField("Price"));
		$manager = new DataObjectManager(
			$this, // Controller
			'ProductVariations', // Source name
			'ProductVariations', // Source class
			array(
				'Title' => 'Title'
			), // Headings 
			'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
		);
		
		$f->addFieldToTab("Root.Content.ProductTypes", $manager);
		return $f;
	}
	

	
}

class ProductPage_Controller extends Page_Controller {
	function CurrentSession(){
		return Cookie::get('PHPSESSID');
	}
	
	function CurrentOrder() {
		return DataObject::get_one('Order', 'Session =\'' . $this->CurrentSession() . '\'');
	}
	
	function StartOrder(){
		$orderStarted = $this->CurrentOrder();
		if(!$orderStarted) {
			$myOrder = new Order();
			$myOrder->setField('Session', $this->CurrentSession());
			$myOrder->write();
			echo('ok');
		}
	}
	
	function ViewOrder() {
		$myOrder = DataObject::get("Order_Item", "OrderID = 1", "", "", "");
		return $myOrder;
	}

	function productForm(){
		$fields = new FieldSet();
		$fields->push(new TextField("HowMany", "HowMany", 1));
		$actions = new FieldSet(
			new FormAction('addProduct', _t('MemberTableField.ADD','Add'))
		);
		$productForm = new Form($this, "productForm", $fields, $actions);
		return $productForm;
	}
	
	function addProduct() {
		$myOrderItem = new Order_Item();
		$myOrder = $this->CurrentOrder();
		$myOrderItem->OrderID = $myOrder->ID;
		$myOrderItem->ProductPageID = $this->ID;
		$myOrderItem->write();
		Director::redirectBack();
	}
	
	function doAction() {
		
	}
	
	public function init() {
		parent::init();
		$this->StartOrder();
	}

	
}