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 .

Wednesday 11 October 2017

How to upgrade Magento 2

Hi as magento 2 is still in a development phase and there are continuous releases are comming.
So we must have update Magento 2 timely for being safe and updated with all the features.
 
Fortunately upgrading Magento 2 is really very starlight forward and easy to do 
To upgrade Magenbto 2 we can use composer and below are the commads we need to 
run to upgrade magento to our desired version.
  
In this i am upgrading to magento 2.0.2  and you can change the version according to your needs 
 
composer require magento/product-community-edition 2.0.2 --no-update
composer update
rm -rf var/di var/generation
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
 
After upgrade, check your Magento version with the following command:

php bin/magento --version