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

Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous

When trying to filter sales order grid with From and To dates it was redirecting to dashboard.after that again i tried to open sales order grind it is generating reports in reports file it showing. "Integrity constraint violation: 1052 Column 'created_at' in where clause is ambiguous" means it is finding a another created_at field. because when we adding or joining the other table then it has also a field named as created_at. So below is the  solution for this error. magento that created_at is of the main_table not of my custom table. Find the below code in the sales order grid.php file. $this->addColumn('created_at', array(            'header' => Mage::helper('sales')->__('Purchased On'),             'index' => 'created_at',             'type' => 'datetime',             'width' => '100px',         )); ...

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

How to Add Magento 2 Sort by Price for Low to High & High to Low Options and name A-Z & Z-A etc sort dropdown

The store design and its navigation must be in such a way that makes it easier for the shopper to find the exact required product and make the shopping process comfortable and enjoyable.  Navigation can be made easier and hence improve the shopping experience by offering custom sorting options. The default Magento 2 offers sorting by position, product name, and price  A price-sensitive customer may save some clicks by starting with the cheapest products. On the other hand, customers who have a high standard for quality may quickly find their most desired products by sampling from high prices to low prices. To provide such feature in Magento 2 and serve both the type of price-sensitive customers, you can add Magento 2 sort by price for low to high & high to low options. Some people can sort by names A-Z or Z-A, position low to high high to low like this we can improve sales to our site and user can easily find products for implementing this fallow given steps to implement s...