Skip to main content

order cancel from front end

if you want cancel order from front end create module like this
app/etc/modules/<Namespace_Modulename>.xml

<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

<?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>

system.xml

<?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>

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



Comments

  1. I think you can use an extension called Frontend Cancel Order: http://bsscommerce.com/magento-2-frontend-cancel-order.html
    It allows customers to cancel orders right from the frontend and then automatically send notification emails to admin about canceled orders.
    Hope it helps!

    ReplyDelete
    Replies
    1. it is paid so better to develop module by using this blog if help full

      Delete

Post a Comment

Popular posts from this blog

magento 1 extension converted in to magento 2 process

Simple Install =================================== Download as a zip below link. unzip and upload it to your public_html or other rootdoc directory. https://github.com/magento/code-migration. login to your server via shell switch to the code-migration-develop directory by running following command   cd code-migration-develop Run command following command in root directory of this toolkit composer instal Before running the migration, the following directories need to be prepared: ===================================== * `<src>` - Directory that contains custom Magento 1.x code that is intended to be migrated. The code must follow the Magento 1.x directory structure.   Magento1.x core files must not be included. * `<dst>` - Empty directory for the toolkit to put the generated Magento 2 code to * `<m1>` - Directory that contains: * Vanilla Magento 1.x codebase, and * Custom Magento 1.x code same as...

Magento 2 product collection Filtering multi-select attribute values

  If you have multi-select attribute of product like below If you want filter value for this option Use below syntax to get product data: ->addAttributeToFilter('store_model', array('finset' => $params['store_model'])) finset key is used for multiselect attribute filter. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $products = $objectManager->get('Magento\Catalog\Model\Product')         ->getCollection()         ->addAttributeToSelect('*')         ->addAttributeToSelect('store_brand')         ->addAttributeToSelect('store_model')         ->addAttributeToSelect('store_year')         ->addAttributeToFilter('store_brand', array('finset' => $params['store_brand']))         ->addAttributeToFilter('store_model', array('finset' => $params['store_model']))         ->ad...

Magento Debug HEADERS ALREADY SENT error

When you  receiving the following error in  system.log file: app\code\core\Mage\Core\Controller\Response\Http.php:44  [1] \www.pawan.com\lib\Zend\Controller\Response\Abstract.php:727  [2]  \www.pawan.com\app\code\core\Mage\Core\Controller\Response\Http.php:75  [3]  \www.pawan.com\app\code\core\Mage\Core\Controller\Varien\Front.php:188  [4]  \www.pawan.com\app\code\core\Mage\Core\Model\App.php:304  [5]  \www.pawan.com\app\Mage.php:596  [6]  \www.pawan.com\index.php:81 if you have no idea what file is causing this. Here is the steps to debug. That error is thrown from Mage_Core_Controller_Response_Http -> sendHeaders(). This function calls the super class function that actually does the check to see whether or not headers have already been sent, Zend_Controller_Response_Abstract -> canSendHeaders(). The Zend_Controller_Response_Abstract class handles, among other things, sending response header...