Created
October 25, 2017 07:53
-
-
Save bummzack/5bb2f3e679fc3d9ca4423629f01f05cc to your computer and use it in GitHub Desktop.
SilverShop checkout address validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Apply this extension to the checkout page controller, eg: | |
* | |
* ``` | |
* CheckoutPage_Controller: | |
* extensions: | |
* - CheckoutControllerExtension | |
* ``` | |
*/ | |
class CheckoutControllerExtension extends Extension | |
{ | |
public function updateConfirmationComponentConfig($config) | |
{ | |
$config->addComponent(new ValidateAddressCheckoutComponent()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
Name: shop-config | |
--- | |
CheckoutPage_Controller: | |
extensions: | |
- CheckoutControllerExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validation component to validate an order contains a valid address | |
*/ | |
class ValidateAddressCheckoutComponent extends CheckoutComponent | |
{ | |
public function getFormFields(Order $order) | |
{ | |
$fields = new FieldList(); | |
$fields->add(HiddenField::create('_address', $order->getShippingAddress()->ID)); | |
/** @var ValidationResult $validation */ | |
$validation = $order->getShippingAddress()->validate(); | |
if(!$validation->valid()){ | |
$fields->add(LiteralField::create('_warn_Address', | |
'<div class="message warn missing-address">'.$this->MissingAddressMessage().'</div>' | |
)); | |
} | |
return $fields; | |
} | |
public function getRequiredFields(Order $order) | |
{ | |
return array(); | |
} | |
public function validateData(Order $order, array $data) | |
{ | |
$validation = $order->getShippingAddress()->validate(); | |
if(!$validation->valid()){ | |
throw new ValidationException($this->MissingAddressMessage()); | |
} | |
return $validation->valid(); | |
} | |
public function setData(Order $order, array $data) | |
{ | |
} | |
public function getData(Order $order) | |
{ | |
return array(); | |
} | |
protected function MissingAddressMessage() | |
{ | |
return _t( | |
'Checkout.NO_ADDRESS', | |
'No Address given. <a href="{Link}">Please go enter one</a>', | |
'', | |
array('Link' => Controller::curr()->Link('shippingaddress')) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment