Monday 16 October 2017

How to Cancel order programatically in Magento 2 ?



Hi by default Magento don't provide the cancel order from front-end . But its a great feature to have on the website to make customer more trust on the website .

To achieve this you can create your custom controller and follow the below code.In this i am posting a order id from the account page to my controller

<?php
 Nmaespace\Modulename\Controller\Action;

class Cancelorder  extends \Magento\Framework\App\Action\Action
{
  protected $orderManagement;
  public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Sales\Api\OrderManagementInterface $orderManagement

) {
    $this->orderManagement = $orderManagement;
    parent::__construct($context); 
}

public function execute()
{
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
       
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        if(!$customerSession->isLoggedIn()) {
                $this->_redirect('/');
                die;
        }
       
        /*get request params */
        $get_customer_id = $customerSession->getCustomer()->getId();
       
        $get_order_id = $this->getRequest()->getParam('order_id');
        /*get request params */
        //die;
        $order = $objectManager->create('Magento\Sales\Model\Order')->load($get_order_id);
        $getcustomerid = $get_customer_id;
        $orderdata  = $order->getData();
        $order_status = $orderdata["status"];
        //print_r($orderdata);
        $cus_id =  $orderdata["customer_id"];
        if($getcustomerid != $cus_id){
            echo "We cant Cancel this order at this time" ;
            //die("go back");
        }
        if($order_status == "pending"){
            $this->orderManagement->cancel($get_order_id); 
            echo "Order Cancelled successfully" ;
        }
        else{
            echo "We cant Cancel this order at this time" ;
           
        }
}


}



thanks. Hope this will help .

13 comments:

  1. Thank you!I had many problems with it. I think it will be easier for me to master this program now.

    ReplyDelete
  2. Keep it up admin...nice blog & nice post in this blog...Magento Sverige

    ReplyDelete
  3. i was really enjoying your blogs and that was really helpful to every developer so just informing we all developers are waiting for your post. Magento Migration Support

    ReplyDelete
  4. If you don’t want to take a chance with your tie and money then hiring a professional agency for the iPhone app development India is the best alternatives. These professional agencies ensure that the clients get the value for money.

    ReplyDelete
  5. Are you wondering about making a career in the field of software development? But don’t know where to start? And which programming language should learn considering current technological trends and career growth? To help you out we have crafted a Learn 7 Programming Languages In 2020. All these languages are appropriate for different use cases like web development, mobile app development, game development and others.

    ReplyDelete
  6. Mobile app development has a variety of options for every business module. Out of all these technologies contributing to app technology,Cross Platform App Development has shown a high raise in the graphs. Many of you have might have heard about the React Native, Flutter and Ionic. But you may some doubts regarding these frameworks.

    ReplyDelete
  7. Thank you, I appreciate that I getting a lot of good and reliable information from your post. Thanks for sharing such kind of nice and wonderful post.

    Php Web Development Company Bangalore | Ecommerce Website Designer India | Internet Marketing Company in Bangalore | Magento Website Developer In India

    ReplyDelete

  8. Awesome blog.I find it interesting and is pretty fascinating, By reading your blog i got very useful information.Thanks for sharing this blog.
    Salesforce Consulting Services
    Ecommerce Development Services
    Mobile App Development Services
    Magento Development Services

    ReplyDelete
  9. Very nice and informative article. Thanks for sharing.

    ReplyDelete
  10. When it comes to react native app development company, we push digital transformation across the enterprise by harnessing the power of the React Native platform.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Great article! It's good to know that Magento 2 has the ability to cancel orders programmatically. I'm sure this will be very helpful for many store owners. If you're looking to Hire Magento Developers to help you with this, I would suggest WebClues Infotech. They have years of experience in Custom Magento development and can surely help you with your project.

    ReplyDelete