Skip to content

Commit 15bcc91

Browse files
authored
Merge pull request #1164 from sumitwebkul/gli-2183
Added: Option to add/edit customer address from order detail page of Back office
2 parents 175966c + 154a768 commit 15bcc91

File tree

7 files changed

+247
-135
lines changed

7 files changed

+247
-135
lines changed

Diff for: admin/themes/default/sass/controllers/_order.sass

+14-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,17 @@
8282
max-height: 200px
8383

8484
.panel-refund-request .refunded_amount
85-
margin-top: 2px
85+
margin-top: 2px
86+
87+
.panel-guest_address
88+
.panel-heading
89+
align-items: center
90+
display: flex
91+
gap: 10px
92+
justify-content: space-between
93+
.panel-title
94+
width: 100%
95+
.dropdown-menu
96+
right: 0
97+
left: unset
98+
font-size: 14px

Diff for: admin/themes/default/template/controllers/orders/helpers/view/view.tpl

+48-116
Original file line numberDiff line numberDiff line change
@@ -634,128 +634,60 @@
634634
</div>
635635

636636
{/if}
637-
<!-- Tab nav -->
638-
<div class="row" style="display:none">
639-
<ul class="nav nav-tabs" id="tabAddresses">
640-
<li class="active">
641-
<a href="#addressShipping">
642-
<i class="icon-truck"></i>
643-
{l s='Shipping address'}
644-
</a>
645-
</li>
646-
<li>
647-
<a href="#addressInvoice">
648-
<i class="icon-file-text"></i>
649-
{l s='Invoice address'}
650-
</a>
651-
</li>
652-
</ul>
653-
<!-- Tab content -->
654-
<div class="tab-content panel">
655-
<!-- Tab status -->
656-
<div class="tab-pane in active" id="addressShipping">
657-
<!-- Addresses -->
658-
<h4 class="visible-print">{l s='Shipping address'}</h4>
659-
{if !$order->isVirtual()}
660-
<!-- Shipping address -->
661-
{if $can_edit}
662-
<form class="form-horizontal hidden-print" method="post" action="{$link->getAdminLink('AdminOrders')|escape:'html':'UTF-8'}&amp;vieworder&amp;id_order={$order->id|intval}">
663-
<div class="form-group">
664-
<div class="col-lg-9">
665-
<select name="id_address">
666-
{foreach from=$customer_addresses item=address}
667-
<option value="{$address['id_address']}"
668-
{if $address['id_address'] == $order->id_address_delivery}
669-
selected="selected"
670-
{/if}>
671-
{$address['alias']} -
672-
{$address['address1']}
673-
{$address['postcode']}
674-
{$address['city']}
675-
{if !empty($address['state'])}
676-
{$address['state']}
677-
{/if},
678-
{$address['country']}
679-
</option>
680-
{/foreach}
681-
</select>
682-
</div>
683-
<div class="col-lg-3">
684-
<button class="btn btn-default" type="submit" name="submitAddressShipping"><i class="icon-refresh"></i> {l s='Change'}</button>
685-
</div>
686-
</div>
687-
</form>
688-
{/if}
689-
<div class="well">
690-
<div class="row">
691-
<div class="col-sm-6">
692-
<a class="btn btn-default pull-right" href="?tab=AdminAddresses&amp;id_address={$addresses.delivery->id}&amp;addaddress&amp;realedit=1&amp;id_order={$order->id}&amp;address_type=1&amp;token={getAdminToken tab='AdminAddresses'}&amp;back={$smarty.server.REQUEST_URI|urlencode}">
693-
<i class="icon-pencil"></i>
694-
{l s='Edit'}
695-
</a>
696-
{displayAddressDetail address=$addresses.delivery newLine='<br />'}
697-
{if $addresses.delivery->other}
698-
<hr />{$addresses.delivery->other}<br />
699-
{/if}
700-
</div>
701-
</div>
637+
</div>
638+
639+
<div class="panel panel-guest_address">
640+
<div class="panel-heading">
641+
<span class="panel-title"><i class="icon icon-envelope"></i> &nbsp;{l s='Customer Address'}</span>
642+
{if $can_edit}
643+
{if $idOrderAddressInvoice}
644+
<button id="edit_guest_address" class="btn btn-primary pull-right fancybox" href="{$link->getAdminLink('AdminAddresses')}&amp;id_address={$idOrderAddressInvoice}&amp;updateaddress&amp;id_order={$order->id|intval}&amp;address_type=2&amp;realedit=1&amp;liteDisplaying=1&amp;submitFormAjax=1#">
645+
<i class="icon-pencil"></i> {l s='Edit'}
646+
</button>
647+
{if (!$idCurrentAddress || ($idCurrentAddress != $idOrderAddressInvoice)) || $ordersWithDiffInvAddr}
648+
<div class="guest_address_actions dropdown">
649+
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
650+
<i class="icon-ellipsis-vertical"></i>
651+
</a>
652+
<ul class="dropdown-menu">
653+
{if $ordersWithDiffInvAddr}
654+
<li><a href="{$current_index}&amp;vieworder&amp;token={$smarty.get.token|escape:'html':'UTF-8'}&amp;id_order={$order->id|intval}&amp;action=set_old_orders_address">{l s='Set for all orders'}</a></li>
655+
{/if}
656+
{if !$idCurrentAddress || ($idCurrentAddress != $idOrderAddressInvoice)}
657+
<li><a href="{$current_index}&amp;vieworder&amp;token={$smarty.get.token|escape:'html':'UTF-8'}&amp;id_order={$order->id|intval}&amp;action=set_address_current_address">{l s='Set as current address'}</a></li>
658+
{/if}
659+
</ul>
702660
</div>
703661
{/if}
704-
</div>
705-
<div class="tab-pane " id="addressInvoice">
706-
<!-- Invoice address -->
707-
<h4 class="visible-print">{l s='Invoice address'}</h4>
708-
{if $can_edit}
709-
<form class="form-horizontal hidden-print" method="post" action="{$link->getAdminLink('AdminOrders')|escape:'html':'UTF-8'}&amp;vieworder&amp;id_order={$order->id|intval}">
710-
<div class="form-group">
711-
<div class="col-lg-9">
712-
<select name="id_address">
713-
{foreach from=$customer_addresses item=address}
714-
<option value="{$address['id_address']}"
715-
{if $address['id_address'] == $order->id_address_invoice}
716-
selected="selected"
717-
{/if}>
718-
{$address['alias']} -
719-
{$address['address1']}
720-
{$address['postcode']}
721-
{$address['city']}
722-
{if !empty($address['state'])}
723-
{$address['state']}
724-
{/if},
725-
{$address['country']}
726-
</option>
727-
{/foreach}
728-
</select>
729-
</div>
730-
<div class="col-lg-3">
731-
<button class="btn btn-default" type="submit" name="submitAddressInvoice"><i class="icon-refresh"></i> {l s='Change'}</button>
732-
</div>
733-
</div>
734-
</form>
735-
{/if}
736-
<div class="well">
737-
<div class="row">
738-
<div class="col-sm-6">
739-
<a class="btn btn-default pull-right" href="?tab=AdminAddresses&amp;id_address={$addresses.invoice->id}&amp;addaddress&amp;realedit=1&amp;id_order={$order->id}&amp;address_type=2&amp;back={$smarty.server.REQUEST_URI|urlencode}&amp;token={getAdminToken tab='AdminAddresses'}">
740-
<i class="icon-pencil"></i>
741-
{l s='Edit'}
742-
</a>
743-
{displayAddressDetail address=$addresses.invoice newLine='<br />'}
744-
{if $addresses.invoice->other}
745-
<hr />{$addresses.invoice->other}<br />
746-
{/if}
747-
</div>
662+
{else}
663+
<button id="add_guest_address" class="btn btn-primary pull-right fancybox" href="{$link->getAdminLink('AdminAddresses')}&amp;addaddress&amp;id_order={$order->id|intval}&amp;address_type=2&amp;id_customer={$order->id_customer}&amp;liteDisplaying=1&amp;submitFormAjax=1#">
664+
<i class="icon-plus-circle"></i> {l s='Add Address'}
665+
</button>
666+
{if $idCurrentAddress}
667+
<div class="guest_address_actions dropdown">
668+
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
669+
<i class="icon-ellipsis-vertical"></i>
670+
</a>
671+
<ul class="dropdown-menu">
672+
<li><a href="{$current_index}&amp;vieworder&amp;token={$smarty.get.token|escape:'html':'UTF-8'}&amp;id_order={$order->id|intval}&amp;action=set_order_active_address">{l s='Set current address for this order'}</a></li>
673+
</ul>
748674
</div>
675+
{/if}
676+
{/if}
677+
{/if}
678+
</div>
679+
<div class="row">
680+
{if $guestFormattedAddress}
681+
{$guestFormattedAddress}
682+
{else}
683+
<div class="list-empty">
684+
<div class="list-empty-msg">
685+
<i class="icon-warning-sign list-empty-icon"></i>
686+
{l s='Guest address not found.'}
749687
</div>
750688
</div>
751-
</div>
689+
{/if}
752690
</div>
753-
<script>
754-
$('#tabAddresses a').click(function (e) {
755-
e.preventDefault()
756-
$(this).tab('show')
757-
})
758-
</script>
759691
</div>
760692

761693
{* Order Internal notes *}

Diff for: classes/Address.php

+40-6
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function add($autodate = true, $null_values = false)
180180
{
181181
// for customer address we need to check of customer address already exists.
182182
if ($this->id_customer) {
183-
if ($id_address = Customer::getCustomerIdAddress($this->id_customer, false)) {
183+
if ($this->deleted == 0 && $id_address = Customer::getCustomerIdAddress($this->id_customer, false)) {
184184
return false;
185185
}
186186
}
@@ -196,6 +196,8 @@ public function add($autodate = true, $null_values = false)
196196

197197
public function update($null_values = false)
198198
{
199+
200+
$context = Context::getContext();
199201
// Empty related caches
200202
if (isset(self::$_idCountries[$this->id])) {
201203
unset(self::$_idCountries[$this->id]);
@@ -208,9 +210,42 @@ public function update($null_values = false)
208210
Customer::resetAddressCache($this->id_customer, $this->id);
209211
}
210212

213+
// If address is already used in any order, it will not be edited directly, set address to "deleted" and create a new address
214+
if ($this->isUsed() && $this->deleted == 0) {
215+
return $this->updateUsedAddress($this);
216+
}
217+
211218
return parent::update($null_values);
212219
}
213220

221+
/**
222+
* If address is already used in any order
223+
* set address to "deleted" and create a new address
224+
* @param Address $address
225+
* @return bool
226+
*/
227+
public function updateUsedAddress(Address $address)
228+
{
229+
$objOldAddress = new Address($address->id);
230+
$address->id = $address->id_address = null;
231+
232+
if ($objOldAddress->delete() && $address->save()) {
233+
// we need to change the address id in the current cart as the deleted ID can assigned in context->cart.
234+
if ($customerCarts = Cart::getCustomerCarts($objOldAddress->id_customer, false)) {
235+
foreach ($customerCarts as $cart) {
236+
$objCart = new Cart($cart['id_cart']);
237+
if ($cart['id_address_invoice'] == $objOldAddress->id) {
238+
$objCart->updateAddressId($objOldAddress->id, $address->id);
239+
}
240+
}
241+
}
242+
243+
return true;
244+
}
245+
246+
return false;
247+
}
248+
214249
/**
215250
* @see ObjectModel::delete()
216251
*/
@@ -222,12 +257,12 @@ public function delete()
222257
// reset checkout process if addresses deletes
223258
CheckoutProcess::refreshCheckoutProcess();
224259

225-
if (!$this->isUsed()) {
226-
return parent::delete();
227-
} else {
260+
if ($this->isUsed()) {
228261
$this->deleted = true;
229262
return $this->update();
230263
}
264+
265+
return parent::delete();
231266
}
232267

233268
/**
@@ -252,10 +287,9 @@ public function validateController($htmlentities = true)
252287

253288
// for customer address we need to check of customer address already exists.
254289
// if we are editing an address we do not need to validate already created address.
255-
if (!Validate::isLoadedObject($this)) {
290+
if (!Validate::isLoadedObject($this) && $this->deleted != 0) {
256291
if ($idCustomer = Tools::getValue('id_customer')) {
257292
if ($id_address = Customer::getCustomerIdAddress($idCustomer)) {
258-
259293
$errors[] = sprintf(Tools::displayError('Customer address already exists. Id address: #%d'), $id_address);
260294
}
261295
}

Diff for: classes/order/Order.php

+15-6
Original file line numberDiff line numberDiff line change
@@ -960,21 +960,30 @@ public function isPaidAndShipped()
960960
*
961961
* @param int $id_customer Customer id
962962
* @param bool $show_hidden_status Display or not hidden order statuses
963+
* @param bool $skip_id_address_invoice Skip orders from this id_address_invoice
963964
* @return array Customer orders
964965
*/
965-
public static function getCustomerOrders($id_customer, $show_hidden_status = false, Context $context = null)
966+
public static function getCustomerOrders($id_customer, $show_hidden_status = false, Context $context = null, $id_address_invoice = null, $skip_address = 0)
966967
{
967968
if (!$context) {
968969
$context = Context::getContext();
969970
}
970971

971-
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
972-
SELECT o.*, (SELECT SUM(od.`product_quantity`) FROM `'._DB_PREFIX_.'order_detail` od WHERE od.`id_order` = o.`id_order`) nb_products
972+
$sql = 'SELECT o.*, (SELECT SUM(od.`product_quantity`) FROM `'._DB_PREFIX_.'order_detail` od WHERE od.`id_order` = o.`id_order`) nb_products
973973
FROM `'._DB_PREFIX_.'orders` o
974-
WHERE o.`id_customer` = '.(int)$id_customer.
975-
Shop::addSqlRestriction(Shop::SHARE_ORDER).'
974+
WHERE o.`id_customer` = '.(int)$id_customer;
975+
976+
// if you want orders from / not from a specific id_address_invoice
977+
if (!is_null($id_address_invoice)) {
978+
$sql .= ' AND o.`id_address_invoice`';
979+
$sql .= ($skip_address ? ' != ' : ' = ').(int)$id_address_invoice;
980+
}
981+
982+
$sql .= Shop::addSqlRestriction(Shop::SHARE_ORDER).'
976983
GROUP BY o.`id_order`
977-
ORDER BY o.`date_add` DESC');
984+
ORDER BY o.`date_add` DESC';
985+
986+
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
978987
if (!$res) {
979988
return array();
980989
}

0 commit comments

Comments
 (0)