1 <?php
2
3 4 5 6 7 8
9 class CartOrderButton extends ViewableData implements OrderButtonInterface {
10
11 private $product = 0;
12 private $variation;
13
14 function __construct($product, $variation="") {
15 parent::__construct();
16 $this->product = $product;
17 $this->variation = $variation;
18 }
19
20 21 22 23 24 25 26 27
28 public function Show($withNum=false, $hasOne=false) {
29 if ($hasOne) {
30 $cart = Cart::get_items();
31 if ($cart && !empty($cart))
32 foreach ($cart as $item) {
33 if ($item->getProductID() == $this->product)
34 return false;
35 }
36 }
37
38 $fields = new FieldSet(
39 new HiddenField('itemId', '', $this->product)
40 );
41
42 if (($product = DataObject::get_by_id('Product', $this->product)) && $product->hasMethod('Variations') && $product->Variations()->exists()) {
43 if (($availableVariations = $product->AllVariations()) && $availableVariations->Count() && $this->variation) {
44 $fields->push(new HiddenField('variationId', '', $this->variation));
45
46
47
48
49 } else {
50 return false;
51 }
52 }
53
54 if ($withNum) {
55 $fields->push($field = new NumericField('Num', '', 1));
56 $field->addExtraClass('AddProductNum');
57 }
58
59 $form = new Form(
60 new Cart_Controller(),
61 "add",
62 $fields,
63 new FieldSet($action = new FormAction("add", _t('Cart.OrderButtonTitle', 'Add Cart')))
64 );
65
66 $action->addExtraClass('AddProductAction');
67 $form->setTemplate('CartOrderButton');
68 $form->setHTMLID('AddProductForm_'. $this->product);
69 $form->addExtraClass('AddProductForm');
70 if ($withNum) $form->addExtraClass('AddProductForm__Num');
71 $form->getValidator()->setJavascriptValidationHandler('none');
72 $form->disableSecurityToken();
73
74 return $form;
75 }
76
77 78 79 80 81
82 public function One() {
83 return $this->Show();
84 }
85
86 87 88 89 90
91 public function WithNum() {
92 return $this->Show(true);
93 }
94
95 96 97 98 99
100 public function forTemplate() {
101 if ($this->One())
102 return $this->One()->forTemplate();
103 }
104 }
105
[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.
-