Skip to content

Commit

Permalink
Minor fix to checkout delivery address
Browse files Browse the repository at this point in the history
  • Loading branch information
sampoyigi committed Jul 6, 2019
1 parent c9a06dd commit 49a7e36
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
5 changes: 3 additions & 2 deletions classes/OrderManager.php
Expand Up @@ -108,8 +108,9 @@ public function saveOrder($order, $data)
$addressId = null;
if ($address = array_get($data, 'address', [])) {
$address['customer_id'] = $customerId;
$address['address_id'] = $order->address_id;
$addressId = Addresses_model::createOrUpdateFromRequest($address)->getKey();

$addressId = array_get($data, 'address_id');
$addressId = !empty($addressId) ? $addressId : Addresses_model::createOrUpdateFromRequest($address)->getKey();

// Update customer default address
if ($this->customer) {
Expand Down
37 changes: 29 additions & 8 deletions components/Checkout.php
Expand Up @@ -2,6 +2,7 @@

namespace Igniter\Cart\Components;

use Admin\Models\Addresses_model;
use Admin\Models\Customers_model;
use Admin\Traits\ValidatesForm;
use ApplicationException;
Expand All @@ -10,6 +11,7 @@
use Exception;
use Igniter\Cart\Classes\OrderManager;
use Igniter\Cart\Models\Orders_model;
use Igniter\Local\Classes\CoveredArea;
use Illuminate\Http\RedirectResponse;
use Location;
use Main\Traits\HasPageOptions;
Expand Down Expand Up @@ -187,7 +189,7 @@ public function onConfirm()

$this->validate($data, $this->createRules());

if ($address = array_get($data, 'address', []))
if ($address = $this->getAddressFromRequest($data))
$this->validateAddress($address);

$order = $this->getOrder();
Expand Down Expand Up @@ -254,7 +256,10 @@ protected function validateCart($throwException = TRUE)

protected function validateAddress($address)
{
$address['country'] = app('country')->getCountryNameById($address['country_id'] ?? setting('country_id'));
if (!Location::requiresUserPosition())
return;

$address['country'] = app('country')->getCountryNameById($address['country_id']);
$address = implode(' ', array_only($address, ['address_1', 'address_2', 'city', 'state', 'postcode', 'country']));

$collection = app('geocoder')->geocode($address);
Expand All @@ -265,7 +270,7 @@ protected function validateAddress($address)
throw new ApplicationException(lang('igniter.cart::default.checkout.error_covered_area'));

if (!Location::isCurrentAreaId($area->area_id)) {
Location::setCoveredArea($area);
Location::setCoveredArea(new CoveredArea($area));
throw new ApplicationException(lang('igniter.cart::default.checkout.alert_delivery_area_changed'));
}
}
Expand All @@ -283,12 +288,12 @@ protected function createRules()
];

if (Location::orderTypeIsDelivery()) {
$namedRules[] = ['address_id', 'lang:igniter.cart::default.checkout.label_address', 'integer'];
$namedRules[] = ['address.address_1', 'lang:igniter.cart::default.checkout.label_address_1', 'required|min:3|max:128'];
$namedRules[] = ['address.city', 'lang:igniter.cart::default.checkout.label_city', 'required|min:2|max:128'];
$namedRules[] = ['address_id', 'lang:igniter.cart::default.checkout.label_address', 'sometimes|required|integer'];
$namedRules[] = ['address.address_1', 'lang:igniter.cart::default.checkout.label_address_1', 'requiredIf:address_id,0|min:3|max:128'];
$namedRules[] = ['address.city', 'lang:igniter.cart::default.checkout.label_city', 'requiredIf:address_id,0|min:2|max:128'];
$namedRules[] = ['address.state', 'lang:igniter.cart::default.checkout.label_state', 'max:128'];
$namedRules[] = ['address.postcode', 'lang:igniter.cart::default.checkout.label_postcode', 'required|min:2|max:10'];
$namedRules[] = ['address.country_id', 'lang:igniter.cart::default.checkout.label_country', 'sometimes|required|integer'];
$namedRules[] = ['address.postcode', 'lang:igniter.cart::default.checkout.label_postcode', 'requiredIf:address_id,0|min:2|max:10'];
$namedRules[] = ['address.country_id', 'lang:igniter.cart::default.checkout.label_country', 'sometimes|requiredIf:address_id,0|integer'];
}

return $namedRules;
Expand All @@ -309,4 +314,20 @@ protected function isOrderMarkedAsProcessed()

return Redirect::to($order->getUrl($successPage));
}

protected function getAddressFromRequest(&$data)
{
$addressId = array_get($data, 'address_id');
if (empty($addressId)) {
if (isset($data['address']))
$data['address']['country_id'] = $data['address']['country_id'] ?? setting('country_id');

return array_get($data, 'address', []);
}

$data['address'] = ($model = Addresses_model::find($addressId))
? $model->toArray() : null;

return $data['address'];
}
}
18 changes: 13 additions & 5 deletions components/checkout/address_fields.php
@@ -1,10 +1,12 @@
<div class="form-group">
<label for=""><?= lang('igniter.cart::default.checkout.text_delivery_address'); ?></label>
<div class="input-group">
<?php if (count($customerAddresses = $order->listCustomerAddresses())) { ?>
<?php $customerAddresses = $order->listCustomerAddresses(); ?>
<?php if (count($customerAddresses)) { ?>
<select
class="form-control" name="address_id"
id="">
class="form-control"
name="address_id"
>
<option value="0"><?= lang('igniter.cart::default.checkout.text_address'); ?></option>
<?php $index = 0;
foreach ($customerAddresses as $address) { ?>
Expand All @@ -19,7 +21,7 @@ class="form-control" name="address_id"
data-state=""
data-postcode=""
data-country=""
<?= $isDefaultAddress ? 'selected="selected"' : ''; ?>
<?= set_select('address_id', $address->address_id, $isDefaultAddress); ?>
><?= $address->formatted_address; ?></option>
<?php $index++; ?>
<?php } ?>
Expand All @@ -29,7 +31,13 @@ class="form-control" name="address_id"
<?= form_error('address_id', '<span class="text-danger">', '</span>'); ?>
</div>

<div class="mt-3">
<div class="mt-3"
<?php if (count($customerAddresses)) { ?>
data-trigger="[name='address_id']" data-trigger-action="show"
data-trigger-condition="value[0]"
data-trigger-closest-parent="form"
<?php } ?>
>
<input
type="hidden"
name="address[address_id]"
Expand Down
9 changes: 8 additions & 1 deletion components/checkout/payments.php
@@ -1,16 +1,23 @@
<?php if ($paymentGateways) { ?>
<div class="row">
<div class="col-sm-8">
<input
type="hidden"
name="payment"
value="">
<div class="form-group">
<label for=""><?= lang('igniter.cart::default.checkout.label_payment_method'); ?></label><br/>
<div class="btn-group btn-group-toggle btn-group-vertical w-100" data-toggle="buttons">
<?php foreach ($paymentGateways as $paymentGateway) { ?>
<div class="btn btn-light text-left" role="button">
<div
class="btn btn-light text-left<?= set_value('payment') == $paymentGateway->code ? ' active' : ''; ?>"
role="button">
<input
type="radio"
name="payment"
value="<?= $paymentGateway->code ?>"
autocomplete="off"
<?= set_radio('payment', $paymentGateway->code) ?>
/>
<?= $paymentGateway->name; ?>
<?php if (!$paymentGateway->isApplicable($order->order_total, $paymentGateway)) { ?>
Expand Down

0 comments on commit 49a7e36

Please sign in to comment.