2018-08-23 13:11:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Customer\Http\Controllers;
|
|
|
|
|
|
2021-11-29 10:10:18 +00:00
|
|
|
use Webkul\Customer\Http\Requests\CustomerAddressRequest;
|
2018-08-23 13:11:56 +00:00
|
|
|
use Webkul\Customer\Repositories\CustomerAddressRepository;
|
|
|
|
|
|
|
|
|
|
class AddressController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* Contains route related configuration.
|
2018-08-23 13:11:56 +00:00
|
|
|
*
|
2019-07-01 11:33:36 +00:00
|
|
|
* @var array
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
|
|
|
|
protected $_config;
|
2018-11-01 13:48:59 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* Current customer.
|
|
|
|
|
*
|
|
|
|
|
* @var \Webkul\Customer\Models\Customer
|
|
|
|
|
*/
|
|
|
|
|
protected $customer;
|
|
|
|
|
|
2020-03-05 05:34:57 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new controller instance.
|
|
|
|
|
*
|
|
|
|
|
* @param \Webkul\Customer\Repositories\CustomerAddressRepository $customerAddressRepository
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2022-04-01 14:57:11 +00:00
|
|
|
public function __construct(protected CustomerAddressRepository $customerAddressRepository)
|
2018-08-23 13:11:56 +00:00
|
|
|
{
|
|
|
|
|
$this->_config = request('_config');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* Address route index page.
|
2018-10-17 10:33:57 +00:00
|
|
|
*
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
2018-11-01 13:48:59 +00:00
|
|
|
public function index()
|
|
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
|
|
|
|
return view($this->_config['view'])->with('addresses', $customer->addresses);
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* Show the address create form.
|
2018-10-17 10:33:57 +00:00
|
|
|
*
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
2018-11-01 13:48:59 +00:00
|
|
|
public function create()
|
|
|
|
|
{
|
2019-12-23 13:28:54 +00:00
|
|
|
return view($this->_config['view'], [
|
|
|
|
|
'defaultCountry' => config('app.default_country'),
|
|
|
|
|
]);
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-17 10:33:57 +00:00
|
|
|
* Create a new address for customer.
|
2018-08-23 13:11:56 +00:00
|
|
|
*
|
2018-10-17 10:33:57 +00:00
|
|
|
* @return view
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
2021-11-29 10:10:18 +00:00
|
|
|
public function store(CustomerAddressRequest $request)
|
2018-11-01 13:48:59 +00:00
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
2021-12-29 12:22:38 +00:00
|
|
|
$data = $request->all();
|
2018-08-23 13:11:56 +00:00
|
|
|
|
2022-07-26 09:58:45 +00:00
|
|
|
$data['customer_id'] = $customer->id;
|
2021-11-29 10:10:18 +00:00
|
|
|
$data['address1'] = implode(PHP_EOL, array_filter(request()->input('address1')));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2022-07-26 09:58:45 +00:00
|
|
|
if ($customer->addresses->count() == 0) {
|
2018-10-27 11:13:57 +00:00
|
|
|
$data['default_address'] = 1;
|
|
|
|
|
}
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
if ($this->customerAddressRepository->create($data)) {
|
2019-01-16 08:38:39 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.account.address.create.success'));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2018-10-27 11:13:57 +00:00
|
|
|
return redirect()->route($this->_config['redirect']);
|
2018-08-24 05:18:53 +00:00
|
|
|
}
|
2021-11-29 10:10:18 +00:00
|
|
|
|
|
|
|
|
session()->flash('error', trans('shop::app.customer.account.address.create.error'));
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* For editing the existing addresses of current logged in customer.
|
2018-08-23 13:11:56 +00:00
|
|
|
*
|
2019-08-19 09:30:24 +00:00
|
|
|
* @return \Illuminate\View\View
|
2018-08-23 13:11:56 +00:00
|
|
|
*/
|
2018-11-01 13:48:59 +00:00
|
|
|
public function edit($id)
|
|
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$address = $this->customerAddressRepository->findOneWhere([
|
2020-01-10 13:07:58 +00:00
|
|
|
'id' => $id,
|
2022-07-26 09:58:45 +00:00
|
|
|
'customer_id' => $customer->id,
|
2019-04-09 08:35:57 +00:00
|
|
|
]);
|
2019-03-30 13:25:10 +00:00
|
|
|
|
2020-01-08 13:56:22 +00:00
|
|
|
if (! $address) {
|
2019-04-09 08:35:57 +00:00
|
|
|
abort(404);
|
2020-01-08 13:56:22 +00:00
|
|
|
}
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2020-03-05 05:34:57 +00:00
|
|
|
return view($this->_config['view'], array_merge(compact('address'), [
|
2021-11-29 10:10:18 +00:00
|
|
|
'defaultCountry' => config('app.default_country'),
|
2020-03-05 05:34:57 +00:00
|
|
|
]));
|
2018-08-24 05:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 10:33:57 +00:00
|
|
|
/**
|
2020-03-05 05:34:57 +00:00
|
|
|
* Edit's the premade resource of customer called Address.
|
2018-10-17 10:33:57 +00:00
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
2018-10-17 10:33:57 +00:00
|
|
|
*/
|
2021-11-29 10:10:18 +00:00
|
|
|
public function update($id, CustomerAddressRequest $request)
|
2018-11-01 13:48:59 +00:00
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
2021-12-29 12:22:38 +00:00
|
|
|
$data = $request->all();
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2021-11-29 10:10:18 +00:00
|
|
|
$data['address1'] = implode(PHP_EOL, array_filter(request()->input('address1')));
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2022-07-26 09:58:45 +00:00
|
|
|
$addresses = $customer->addresses;
|
2018-08-24 05:18:53 +00:00
|
|
|
|
2020-01-10 13:07:58 +00:00
|
|
|
foreach ($addresses as $address) {
|
2019-04-09 08:35:57 +00:00
|
|
|
if ($id == $address->id) {
|
2019-03-30 13:25:10 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.account.address.edit.success'));
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->customerAddressRepository->update($data, $id);
|
2019-03-30 13:25:10 +00:00
|
|
|
|
|
|
|
|
return redirect()->route('customer.address.index');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-31 20:02:58 +00:00
|
|
|
session()->flash('warning', trans('shop::app.security-warning'));
|
2018-08-23 13:11:56 +00:00
|
|
|
|
2018-11-01 13:48:59 +00:00
|
|
|
return redirect()->route('customer.address.index');
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|
2018-10-27 11:13:57 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-03-05 05:34:57 +00:00
|
|
|
* To change the default address or make the default address,
|
2021-11-29 10:10:18 +00:00
|
|
|
* by default when first address is created will be the default address.
|
2018-10-27 11:13:57 +00:00
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
2018-10-27 11:13:57 +00:00
|
|
|
*/
|
2018-11-01 13:48:59 +00:00
|
|
|
public function makeDefault($id)
|
|
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
|
|
|
|
if ($default = $customer->default_address) {
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->customerAddressRepository->find($default->id)->update(['default_address' => 0]);
|
2020-02-19 13:48:54 +00:00
|
|
|
}
|
2018-10-27 11:13:57 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
if ($address = $this->customerAddressRepository->find($id)) {
|
2018-10-30 12:46:40 +00:00
|
|
|
$address->update(['default_address' => 1]);
|
2018-10-27 11:13:57 +00:00
|
|
|
} else {
|
2019-01-16 08:38:39 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.account.address.index.default-delete'));
|
2018-10-27 11:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-11-29 10:10:18 +00:00
|
|
|
* Delete address of the current customer.
|
2018-10-27 11:13:57 +00:00
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param int $id
|
|
|
|
|
* @return \Illuminate\Http\Response
|
2018-10-27 11:13:57 +00:00
|
|
|
*/
|
2018-11-01 13:48:59 +00:00
|
|
|
public function destroy($id)
|
|
|
|
|
{
|
2022-07-26 09:58:45 +00:00
|
|
|
$customer = auth()->guard('customer')->user();
|
|
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$address = $this->customerAddressRepository->findOneWhere([
|
2020-01-10 13:07:58 +00:00
|
|
|
'id' => $id,
|
2022-07-26 09:58:45 +00:00
|
|
|
'customer_id' => $customer->id,
|
2019-04-09 08:35:57 +00:00
|
|
|
]);
|
2018-10-27 11:13:57 +00:00
|
|
|
|
2020-02-19 13:48:54 +00:00
|
|
|
if (! $address) {
|
2019-04-09 08:35:57 +00:00
|
|
|
abort(404);
|
2020-02-19 13:48:54 +00:00
|
|
|
}
|
2018-10-27 11:13:57 +00:00
|
|
|
|
2019-07-01 11:33:36 +00:00
|
|
|
$this->customerAddressRepository->delete($id);
|
2019-03-30 13:25:10 +00:00
|
|
|
|
2019-04-09 08:35:57 +00:00
|
|
|
session()->flash('success', trans('shop::app.customer.account.address.delete.success'));
|
2019-03-31 20:02:58 +00:00
|
|
|
|
|
|
|
|
return redirect()->route('customer.address.index');
|
2018-10-27 11:13:57 +00:00
|
|
|
}
|
2018-08-23 13:11:56 +00:00
|
|
|
}
|