1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Checkout\Step; |
4
|
|
|
|
5
|
|
|
use SilverShop\Cart\ShoppingCart; |
6
|
|
|
use SilverShop\Forms\CheckoutForm; |
7
|
|
|
use SilverShop\Checkout\Component\BillingAddress; |
8
|
|
|
use SilverShop\Checkout\CheckoutComponentConfig; |
9
|
|
|
use SilverShop\Checkout\Component\ShippingAddress; |
10
|
|
|
use SilverStripe\Forms\CheckboxField; |
11
|
|
|
use SilverStripe\Forms\FieldList; |
12
|
|
|
use SilverStripe\Forms\FormAction; |
13
|
|
|
|
14
|
|
|
class Address extends CheckoutStep |
15
|
|
|
{ |
16
|
|
|
private static $allowed_actions = array( |
|
|
|
|
17
|
|
|
'shippingaddress', |
18
|
|
|
'ShippingAddressForm', |
19
|
|
|
'setshippingaddress', |
20
|
|
|
'billingaddress', |
21
|
|
|
'BillingAddressForm', |
22
|
|
|
'setbillingaddress', |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
public function shippingconfig() |
26
|
|
|
{ |
27
|
|
|
$config = CheckoutComponentConfig::create(ShoppingCart::curr()); |
28
|
|
|
$config->addComponent(ShippingAddress::create()); |
29
|
|
|
|
30
|
|
|
return $config; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function shippingaddress() |
34
|
|
|
{ |
35
|
|
|
$form = $this->ShippingAddressForm(); |
36
|
|
|
$form->Fields()->push( |
37
|
|
|
CheckboxField::create( |
38
|
|
|
'SeperateBilling', |
39
|
|
|
_t(__CLASS__ . '.SeperateBilling', 'Bill to a different address from this') |
40
|
|
|
) |
41
|
|
|
); |
42
|
|
|
$order = $this->shippingconfig()->getOrder(); |
43
|
|
|
if ($order->BillingAddressID !== $order->ShippingAddressID) { |
44
|
|
|
$form->loadDataFrom(['SeperateBilling' => 1]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return ['OrderForm' => $form]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function ShippingAddressForm() |
51
|
|
|
{ |
52
|
|
|
$form = CheckoutForm::create($this->owner, 'ShippingAddressForm', $this->shippingconfig()); |
53
|
|
|
$form->setActions( |
54
|
|
|
FieldList::create( |
55
|
|
|
FormAction::create('setshippingaddress', _t('SilverShop\Checkout\Step\CheckoutStep.Continue', 'Continue')) |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
$this->owner->extend('updateShippingAddressForm', $form); |
59
|
|
|
|
60
|
|
|
return $form; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setshippingaddress($data, $form) |
64
|
|
|
{ |
65
|
|
|
$this->shippingconfig()->setData($form->getData()); |
66
|
|
|
$step = null; |
67
|
|
|
if (isset($data['SeperateBilling']) && $data['SeperateBilling']) { |
68
|
|
|
$step = 'billingaddress'; |
69
|
|
|
} else { |
70
|
|
|
//ensure billing address = shipping address, when appropriate |
71
|
|
|
$order = $this->shippingconfig()->getOrder(); |
72
|
|
|
$order->BillingAddressID = $order->ShippingAddressID; |
73
|
|
|
$order->write(); |
74
|
|
|
} |
75
|
|
|
return $this->owner->redirect($this->NextStepLink($step)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function billingconfig() |
79
|
|
|
{ |
80
|
|
|
$config = CheckoutComponentConfig::create(ShoppingCart::curr()); |
81
|
|
|
$config->addComponent(BillingAddress::create()); |
82
|
|
|
|
83
|
|
|
return $config; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function billingaddress() |
87
|
|
|
{ |
88
|
|
|
return array('OrderForm' => $this->BillingAddressForm()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function BillingAddressForm() |
92
|
|
|
{ |
93
|
|
|
$form = CheckoutForm::create($this->owner, 'BillingAddressForm', $this->billingconfig()); |
94
|
|
|
$form->setActions( |
95
|
|
|
FieldList::create( |
96
|
|
|
FormAction::create('setbillingaddress', _t('SilverShop\Checkout\Step\CheckoutStep.Continue', 'Continue')) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
$this->owner->extend('updateBillingAddressForm', $form); |
100
|
|
|
|
101
|
|
|
return $form; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setbillingaddress($data, $form) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$this->billingconfig()->setData($form->getData()); |
107
|
|
|
return $this->owner->redirect($this->NextStepLink()); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|