Cancelling an order There may of course be times when a customer wishes to cancel an order, for reasons such as maybe we were too slow to dispatch it, perhaps they changed their mind, or
Trang 1Query for order items
The second query for the model is to get the individual items in the order The
following is a basic query which does this, although this does not take into account
variants of a product that we may have been purchased:
$sql = "SELECT ctp.price, (ctp.price*i.qty) AS cost, i.qty, v.name,
i.product_id
FROM content_types_products ctp, orders_items i,
content c, content_versions v
WHERE i.order_id={$this->id}
AND c.ID=i.product_id AND v.ID=current_revision
AND ctp.content_version=v.ID";
The results of this query should be cached, so that any of our controllers can send the
results directly to a view by associating the cache ID with a template variable
Cancelling an order
There may of course be times when a customer wishes to cancel an order, for reasons
such as maybe we were too slow to dispatch it, perhaps they changed their mind,
or perhaps they ordered mistakenly While allowing a customer to cancel an order
will obviously lose us a sale, it keeps our potential customers happy, and should
hopefully help our store's reputation
Up until the point where we dispatch an order, it should be very easy for a customer
to cancel the order Of course, we may want to expand this later to handle returns
and authorizing returns from customers
For the customer to cancel an order there should be a few simple stages:
1 First they should view the order
2 Next they should select an option somewhere to cancel the order
3 They should be able to enter a comment or note about why they are
cancelling the order
4 They should confirm their wish
5 The order should be cancelled
6 An e-mail should be sent to them to confirm their order was cancelled
(particularly useful if they cancelled it mistakenly)
7 Payment should be refunded (automatically, if the payment method
allows it)
8 An e-mail should be sent to the store administrator
Trang 2This requires some functionality in our order model to cancel the order, and also
some code for our user area controller
Order model additions
There are a few additions required to our order model, so that we can change the
functionality of the cancel order method Depending on whether the administrator or
the customer cancels the order, we can use a parameter to indicate who is cancelling
the order
public function cancelOrder( $initiatedBy )
{
// Only orders that are awaiting payment or awaiting dispatch can
// be cancelled, so we must check the status of the order first.
// is the order pending payment or dispatch i.e cancellable?
if( $this->status == 1 || $this->status == 2 )
{
// We then update the order item in the database to cancelled.
$changes = array( 'status' => 4 );
$this->registry->getObject('db')->updateRecords('orders', $changes,
'ID=' $this->id );
// If the order was cancelled by the customer, we then need to
// e-mail the administrator, e-mail confirmation to the customer,
// and if we can, refund the payment.
if( $initiatedBy == 'user' )
{
// e-mail the administrator
// e-mail the customer confirmation
// refund the payment?
}
// If the order was refunded by the administrator we e-mail the administrator we e-mail the we e-mail the
// customer to inform then, and if possible, refund the payment.
elseif( $initiatedBy == 'admin' )
{
// e-mail the customer
// refund the payment?
}
// We return true, so that the relevant controller can display a
// message indicating that the order was cancelled.
return true;
}
else
Trang 3We return false to indicate that the order was not cancelled, allowing our controllerindicate that the order was not cancelled, allowing our controller that the order was not cancelled, allowing our controller
to inform the user of this
return false;
}
}
Controller code
To facilitate cancelling the order, we need to have two functions in our controller:
one to display a confirmation message and another to actually cancel the order
The following function checks whether the order is valid and belongs to the current
user; if it does, it displays a confirmation message:
private function confirmCancelOrder( $orderId )
{
$this->registry->getObject('template')->
buildFromTemplates('header.tpl.php',
'account/confirm-cancel.tpl.php','footer.tpl.php');
require_once( FRAMEWORK_PATH 'models/order/model.php');
$this->order = new Order( $this->registry, $orderId );
if( $this->order->isValid() )
{
if( $this->order->getUser() == $this->registry->
getObject('authenticate')->getUserID() )
{
$this->registry->getObject('db')->getPage()->addTag('orderid',
$orderId );
}
else
{
$this->registry->redirectUser( 'useraccount', 'Invalid order',
'The order was not cancelled as it was not tied to your
account', $admin = false );
}
}
else
{
$this->registry->redirectUser( 'useraccount', 'Invalid order',
'The order was not found', $admin = false );
}
}
Trang 4When the customer clicks on the confirmation link within the confirmation page, the
following function is called; this again validates whether the order belongs to the
customer, and then cancels it using the model (which in turn, checks if the order
can be cancelled, and sends any relevant e-mail notifications):
private function cancelOrder( $orderId )
{
require_once( FRAMEWORK_PATH 'models/order/model.php');
$this->order = new Order( $this->registry, $orderId );
if( $this->order->isValid() )
{
if( $this->order->getUser() == $this->registry->
getObject('authenticate')->getUserID() )
{
$this->order->cancelOrder('user');
$this->registry->redirectUser( 'useraccount', 'Order
cancelled', 'The order has been cancelled',
$admin = false );
}
else
{
$this->registry->redirectUser( 'useraccount', 'Invalid order',
'The order was not cancelled as it was not tied to your
account', $admin = false );
}
}
else
{
$this->registry->redirectUser( 'useraccount', 'Invalid order',
'The order was not found', $admin = false );
}
}
Expansion
As our needs for our store grow, we can expand this area of the framework; perhaps
we would like it to offer:
Returns handling: This will let our customers indicate if they wish to return
an item The store administrator can provide a returns authorization number,
•
Trang 5A feedback area: This will let us improve the framework by collecting the
general feedback from customers
Exclusive discounts: Incentives for customers!
Advance notice on pre-releases: This will let us entice customers to
make pre-orders
Summary
In this chapter, we have created a centralized customer area, which allowed
customers to update their password, update their default delivery address, and list
their orders and their statuses We have also created an orders model, which we
used to allow customers to view individual orders, as well as cancel existing orders
We also looked into how we might expand the customer area to make it better,
providing more value to the customer
This provides a nice, convenient place for the customer to manage their account, the
information stored by our site on them, and see an overview of all their pending and
processed orders at a glance, to see what is happening with them
•
•
•