// 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",
"ProductPage" => "ProductPage",
);
}
<?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,
'ProductVariations',
'ProductVariations',
array(
'Title' => 'Title'
),
'getCMSFields_forPopup'
);
$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();
}
}