Showing posts with label how to. Show all posts
Showing posts with label how to. Show all posts

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
 
 
 

Wednesday, 16 December 2015

How to use helpers in magento ?

In magento helpers is very useful functionality.Basically we can call helper functions anywhere in the site .

Each module has its own helper function

like in magento you use a below cart helper function to get cart url 

Mage::helper('checkout/cart')->getCartUrl()

the above helper function is created at below location

app/code/core/Mage/Checkout/Helper/Cart.php 

Lets create one new helper function in the end of Cart.php 

let say we need a function in which we need to get all ordered product items data 

 
For this i have create a new function as below

public function getordered_products_data($order_id){
        $cart =  Mage::getModel('sales/order')->load($order_id);
        print_r($cart->getData());die;
        foreach ($cart->getAllItems() as $item) {
        $loadproduct =     Mage::getModel('catalog/product')->load($item->getProduct()->getId());
        print_r($loadproduct->getData());
        }

}


and now we can use this function anywhere in the site as below

Mage::helper('checkout/cart')->getordered_products_data($your_order_id); // pass your order id here

and with this you can see all products data in order anywhere in the site by using above function.

thanks and hope it will help the magento comunity