1 <?php
2
3 4 5 6 7 8
9 class SimpleOrderButton extends ViewableData implements OrderButtonInterface {
10
11 private $product = 0;
12
13 function __construct($product) {
14 parent::__construct();
15 $this->product = $product;
16 }
17
18 19 20 21 22 23
24 static function get_order_page() {
25 if ($page = DataObject::get_one('SimpleOrderForm')) {
26 return $page;
27 }
28 if ($page = DataObject::get_one('SimpleOrderPage')) {
29 return $page;
30 }
31 return false;
32 }
33
34 35 36 37 38 39 40
41 public function Show($withNum = false) {
42 $fields = new FieldSet();
43 $fields->push(new HiddenField('itemId', '', $this->product));
44 if ($withNum) {
45 $fields->push($f = new NumericField('Num', '', 1));
46 $f->addExtraClass('OrderNumField');
47 $f->addExtraClass('AddProductNum');
48 $f->addExtraAttribute('title', _t('SimpleOrderButton.OrderNum', 'Product count to add...'));
49 }
50 $actions = new FieldSet($a = new FormAction("checkout", _t('SimpleOrderButton.AddToCart', 'Checkout')));
51 $a->addExtraClass('AddProductAction');
52
53 $orderPage = self::get_order_page();
54 if (!$orderPage) {
55 user_error(_t('SimpleOrderPage.OrderPageNeed', 'Need to create order page first'), E_USER_ERROR);
56 return false;
57 }
58 $controller = ModelAsController::controller_for($orderPage);
59 $form = new Form($controller, "checkout", $fields, $actions);
60
61 $form->setTemplate('SimpleOrderButton');
62 $form->setHTMLID('AddProductForm_'. $this->product);
63 $form->addExtraClass('AddProductForm');
64 $form->getValidator()->setJavascriptValidationHandler('none');
65 $form->disableSecurityToken();
66 $form->setFormMethod('GET');
67 return $form;
68 }
69
70 71 72 73 74
75 public function One() {
76 return $this->Show();
77 }
78
79 80 81 82 83
84 public function WithNum() {
85 return $this->Show(true);
86 }
87
88 89 90 91 92
93 public function forTemplate() {
94 return $this->One()->forTemplate();
95 }
96 }
97
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-