if you want cancel order from front end create module like this
<?xml version="1.0"?>
<!--/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/-->
<config>
<modules>
<Namespace_CustomerOrderCancel>
<version>1.0.0.0</version>
</Namespace_CustomerOrderCancel>
</modules>
<global>
<blocks>
<sales>
<rewrite>
<order_history>Namespace_CustomerOrderCancel_Block_Order_History</order_history>
</rewrite>
</sales>
</blocks>
<helpers>
<customerordercancel>
<class>Namespace_CustomerOrderCancel_Helper</class>
</customerordercancel>
</helpers>
</global>
<adminhtml>
<translate>
<modules>
<Namespace_CustomerOrderCancel>
<files>
<default>Namespace_CustomerOrderCancel.csv</default>
</files>
</Namespace_CustomerOrderCancel>
</modules>
</translate>
</adminhtml>
<frontend>
<routers>
<customerordercancel>
<use>standard</use>
<args>
<module>Namespace_CustomerOrderCancel</module>
<frontName>customerordercancel</frontName>
</args>
</customerordercancel>
</routers>
<layout>
<updates>
<customerordercancel>
<file>customerordercancel.xml</file>
</customerordercancel>
</updates>
</layout>
<translate>
<modules>
<Namespace_CustomerOrderCancel>
<files>
<default>Namespace_CustomerOrderCancel.csv</default>
</files>
</Namespace_CustomerOrderCancel>
</modules>
</translate>
</frontend>
<default>
<sales>
<cancel>
<enabled>0</enabled>
<leadtime></leadtime>
<cancel_partially_shipped>0</cancel_partially_shipped>
<cancel_fully_shipped>0</cancel_fully_shipped>
<send_email>1</send_email>
</cancel>
</sales>
</default>
</config>
<?xml version="1.0"?>
<!--/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/-->
<config>
<sections>
<sales>
<groups>
<cancel translate="label" module="customerordercancel">
<label>Customer Cancelation</label>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enabled translate="label comment">
<label>Enable cancelation by customer</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Allows customers to cancel their orders from their account</comment>
</enabled>
<leadtime translate="label comment" module="customerordercancel">
<label>Allow cancelation of orders younger than...</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>In days. Leave empty for no limit.</comment>
<validate>validate-digits</validate>
<depends><enabled>1</enabled></depends>
</leadtime>
<cancel_partially_shipped translate="label">
<label>Allow cancelation of partially shipped orders</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends><enabled>1</enabled></depends>
</cancel_partially_shipped>
<cancel_fully_shipped translate="label">
<label>Allow cancelation of fully shipped orders</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends><enabled>1</enabled></depends>
</cancel_fully_shipped>
<send_email translate="label comment">
<label>Send order update email</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Email template is the one configured for Order Comments.</comment>
<depends><enabled>1</enabled></depends>
</send_email>
</fields>
</cancel>
</groups>
</sales>
</sections>
</config>
app/etc/modules/<Namespace_Modulename>.xml
<config>
<modules>
<Namespace_CustomerOrderCancel>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Sales />
</depends>
</Namespace_CustomerOrderCancel>
</modules>
</config>
<modules>
<Namespace_CustomerOrderCancel>
<active>true</active>
<codePool>community</codePool>
<depends>
<Mage_Sales />
</depends>
</Namespace_CustomerOrderCancel>
</modules>
</config>
app/community/Namespace/Modulename
------controllers
-------etc
-------Helper
if you want cancel button in history page (myorder) need override block file
Mage_Sales_Block_Order_History.php
app/community/Namespace/Modulename/etc/
----config.xml
----system.xml
config.xml
<!--/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/-->
<config>
<modules>
<Namespace_CustomerOrderCancel>
<version>1.0.0.0</version>
</Namespace_CustomerOrderCancel>
</modules>
<global>
<blocks>
<sales>
<rewrite>
<order_history>Namespace_CustomerOrderCancel_Block_Order_History</order_history>
</rewrite>
</sales>
</blocks>
<helpers>
<customerordercancel>
<class>Namespace_CustomerOrderCancel_Helper</class>
</customerordercancel>
</helpers>
</global>
<adminhtml>
<translate>
<modules>
<Namespace_CustomerOrderCancel>
<files>
<default>Namespace_CustomerOrderCancel.csv</default>
</files>
</Namespace_CustomerOrderCancel>
</modules>
</translate>
</adminhtml>
<frontend>
<routers>
<customerordercancel>
<use>standard</use>
<args>
<module>Namespace_CustomerOrderCancel</module>
<frontName>customerordercancel</frontName>
</args>
</customerordercancel>
</routers>
<layout>
<updates>
<customerordercancel>
<file>customerordercancel.xml</file>
</customerordercancel>
</updates>
</layout>
<translate>
<modules>
<Namespace_CustomerOrderCancel>
<files>
<default>Namespace_CustomerOrderCancel.csv</default>
</files>
</Namespace_CustomerOrderCancel>
</modules>
</translate>
</frontend>
<default>
<sales>
<cancel>
<enabled>0</enabled>
<leadtime></leadtime>
<cancel_partially_shipped>0</cancel_partially_shipped>
<cancel_fully_shipped>0</cancel_fully_shipped>
<send_email>1</send_email>
</cancel>
</sales>
</default>
</config>
system.xml
<!--/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/-->
<config>
<sections>
<sales>
<groups>
<cancel translate="label" module="customerordercancel">
<label>Customer Cancelation</label>
<frontend_type>text</frontend_type>
<sort_order>1000</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enabled translate="label comment">
<label>Enable cancelation by customer</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Allows customers to cancel their orders from their account</comment>
</enabled>
<leadtime translate="label comment" module="customerordercancel">
<label>Allow cancelation of orders younger than...</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>In days. Leave empty for no limit.</comment>
<validate>validate-digits</validate>
<depends><enabled>1</enabled></depends>
</leadtime>
<cancel_partially_shipped translate="label">
<label>Allow cancelation of partially shipped orders</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>30</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends><enabled>1</enabled></depends>
</cancel_partially_shipped>
<cancel_fully_shipped translate="label">
<label>Allow cancelation of fully shipped orders</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>40</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<depends><enabled>1</enabled></depends>
</cancel_fully_shipped>
<send_email translate="label comment">
<label>Send order update email</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<comment>Email template is the one configured for Order Comments.</comment>
<depends><enabled>1</enabled></depends>
</send_email>
</fields>
</cancel>
</groups>
</sales>
</sections>
</config>
Helper/Data.php
<?php
/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/
class Namespace_CustomerOrderCancel_Helper_Data extends Mage_Core_Helper_Abstract {
public function canCancel(Mage_Sales_Model_Order $order) {
if (!Mage::getStoreConfigFlag('sales/cancel/enabled')) {
return false;
}
if (!$order->canCancel()) {
return false;
}
if ($order->hasShipments() && $order->canShip() && !Mage::getStoreConfigFlag('sales/cancel/cancel_partially_shipped')) {
return false;
}
if (!$order->canShip() && !Mage::getStoreConfigFlag('sales/cancel/cancel_fully_shipped')) {
return false;
}
$dateModel = Mage::getModel('core/date');
$createdAt = $order->getCreatedAtStoreDate();
$deltaDays = ($dateModel->gmtTimestamp() - $dateModel->gmtTimestamp($createdAt)) / 86400;
if (Mage::getStoreConfig('sales/cancel/leadtime') !== '' && $deltaDays > Mage::getStoreConfig('sales/cancel/leadtime')) {
return false;
}
return true;
}
}
controllers/OrderController.php
<?php
/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/
class Namespace_CustomerOrderCancel_OrderController extends Mage_Core_Controller_Front_Action {
public function cancelAction() {
$orderId = $this->getRequest()->getParam('order_id');
$order = Mage::getModel('sales/order')->load($orderId);
$session = Mage::getSingleton('catalog/session');
try {
if (!Mage::helper('customerordercancel')->canCancel($order)) {
throw new Exception('Order cannot be canceled anymore.');
}
$order->cancel();
$order->save();
if (Mage::getStoreConfigFlag('sales/cancel/send_email')) {
$order->sendOrderUpdateEmail();
}
$session->addSuccess($this->__('The order has been canceled.'));
} catch (Exception $e) {
Mage::logException($e);
$session->addError($this->__('The order cannot be canceled.'));
}
$this->_redirect('sales/order/view', array('order_id' => $orderId));
}
public function cancelhistoryAction() {
if (($order_id = $this->getRequest()->getParam('order_id'))) {
$order = Mage::getModel('sales/order')->load($order_id);
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true);
$order->save();
}
$this->_redirect('sales/order/history');
}
}
rewrite process
Block/Order/History.php
<?php
/* @category Namespace
* @package Namespace_CustomerOrderCancel
* @author Pawan Kumar N
*/
require_once 'Mage/Sales/Block/Order/History.php';
class Namespace_CustomerOrderCancel_Block_Order_History extends Mage_Sales_Block_Order_History {
public function __construct() {
parent::__construct();
$this->setTemplate('customerordercancel/sales/order/history.phtml');
}
}
design/frontend/base/default/layout/customerordercancel.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<sales_order_view>
<reference name="sales.order.info.buttons">
<block type="sales/order_info_buttons" name="customer.order.cancel.button" as="customer_order_cancel_button" template="customerordercancel/sales/order/info/buttons/cancel.phtml"></block>
</reference>
</sales_order_view>
</layout>
design/frontend/base/default/template/
customerordercancel/sales/order/info/buttons/cancel.phtml
<?php $_order = $this->getOrder() ?>
<?php if(Mage::helper('customerordercancel')->canCancel($_order)): ?>
<span class="separator">|</span>
<a href="<?php echo $this->getUrl('customerordercancel/order/cancel', array('order_id' => $_order->getId(), '_secure'=>true)) ?>" class="link-reorder"><?php echo $this->__('Cancel Order') ?></a>
<?php endif ?>
for history.html
you can copy the original file from sale/order/history.phtml to our module customerordercancel/sales/order/history.phtml
and add the below code after 64 line number( separator class)
<span class="separator">|</span> <a href="<?php echo $this->getUrl('customerordercancel/order/cancelhistory', array('order_id' => $_order->getId())); ?>" class="link-reorder" onclick="return confirm('<?php echo $this->__('Are you sure want to cancel this order ?');?>')" style="<?php echo ($_order->getState() == Mage_Sales_Model_Order::STATE_CANCELED) ? ('pointer-events:none;cursor:default') : ''; ?>"><?php echo $this->__('Cancel') ?></a>
if any wrong please post suggestions
I think you can use an extension called Frontend Cancel Order: http://bsscommerce.com/magento-2-frontend-cancel-order.html
ReplyDeleteIt allows customers to cancel orders right from the frontend and then automatically send notification emails to admin about canceled orders.
Hope it helps!
it is paid so better to develop module by using this blog if help full
Delete