Category Archives: Magento 2.0 Tutorial

Magento 2 get category Url


Magento 2 get category Url Path By Id: We can get category url from category object. Here in this tutorial, we are going to explain how you can get category url using category id.


Magento 2 get category Url Path By Id Example

You can get category url using category id simply as below-

Get Cetgory Url Example:

$categoryId = 5;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
$url = $category->getUrl();

Magento 2 get Invoice from Order


Magento 2 get Invoice from Order– We can get invoice collection from order. Here in this tutorial, we are going to load order using order id then we will get invoice collection using order id.


Magento 2 get Invoice from Order Id | Invoice Collection Example

You can get Invoice Collection from order simply below-

Magento 2 get Invoice from Order Example:

$orderId = 9999;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')                           ->load($orderId);
$invoiceCollection = $order->getInvoiceCollection();
foreach($invoiceCollection as $invoice){
 $invoiceIncrementID = $invoice->getIncrementId();// invoice increment id
 // same way get other details of invoice
}

Magento 2 Create Customer Programmatically


Magento 2 Create Customer Programmatically Sometimes we need custom code to create customer such as creating custom regirstration page or creating some custom customer import functionality. Here in this article, we are going to explain how you can create customer using custom program.


Magento 2 Create Customer Programmatically Example

Magento 2 Create Customer Programmatically

You can create customer in Magento2 using custom code simply as below-

Magento 2 Create Customer Programmatically Example:

storeManager     = $storeManager;
        $this->customerFactory  = $customerFactory;

        parent::__construct($context);
    }

    public function execute()
    {
        // Get Current Website ID
        $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();
        $customer   = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->setEmail("john@example.com"); 
        $customer->setFirstname("John");
        $customer->setLastname("Dee");
        $customer->setPassword("123XYZ");
        $customer->save();
        $customer->sendNewAccountEmail();
    }
}
?>

So you can create customer in Magento2 programmatically.

Magento 2 Redirect From Observer


Magento 2 Redirect From Observer We can use setRedirect($redirectUrl)->sendResponse(); to redirect to specified page from observer. Here in this article we are going to explain this with simple example.


Magento 2 Redirect From Observer Example

You can redirect from observer simply as below-

Observer-

Here is a simple observer which redirects to specified url in Magento2.

Magento 2 Redirect From Observer Example:

_responseFactory = $responseFactory;
        $this->_url = $url;
    }
    public function execute(Observer $observer) {
             $event = $observer->getEvent();
             $customUrl = 'customer/login';// your custom url
            $this->_responseFactory->create()->setRedirect($customUrl)->sendResponse();
             exit();
    }
}
?>

Magento 2 redirect Customer to Custom Page After Login


Magento 2 redirect Customer to Custom Page After Login– Sometimes we need to redirect customer to some specific page after login. It can be done in many ways like extending the core login functionality, using observers or using a plugin. Here in this article we are going to explain how you can implement this functionality.


Magento 2 redirect Customer to Custom Page After Login Plugin Example

The best way to implement this functionality is plugin Let us see how to do this using plugin.

Step 1 : Create di.xml

First create di.xml located at the below path-

/Vendor/Module/etc/frontend/di.xml

Magento 2 redirect Customer to Custom Page After Login Example:



  
    
  


Step 2 : Create Plugin

Now create plugin which will handle the redirect action.

/Vendor/Module/Plugin/LoginPostPlugin.php

Magento 2 redirect Customer to Custom Page After Login Example:

setPath($redirectPath);
        return $result;
    }

}

So you can do anything whatever you want after login action.

Magento 2 get Payment Method from Quote


Magento 2 get Payment Method from Quote– We can get current Payment method that user selected on frontend from quote session. Let us understand how to get current payment method applied on cart in Magento2.


Magento 2 get Payment Method from Quote Session Example

You can get current payment method selected from quote using object manager simply as below-

Get Payment Method in Magento2 Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$quote = $cart->getQuote();
$paymentMethod = $quote->getPayment()->getMethod();

The above example will give you Payment Method that user selected during checkout on frontend.

Magento 2 get shipping method from quote


We can get current shipping method selected from quote session in Magento2. We can use object manager or dependency injection to get access the current quote object from session. Let us understand how to get current shipping method applied on cart in Magento2.


Magento 2 get shipping method from quote Example

You can get shipping method from quote using object manager simply as below-

Get Shipping Method in Magento2 Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

$quote = $cart->getQuote();
$shippingMethod = $quote->getShippingAddress()->getShippingMethod();

First get the object manager instance, then load magento checout model cart object to access the quote object. Now using the Quote object you can get the current shipping method from shipping Address object. On the same way you can get other shipping details.

The above example will give you shipping method that user selected during checkout.

Magento 2 Get out of stock Products


Magento 2 Get out of stock Products– We can use product collection and joinField to get the out of stock items. Here in this tutorial, we are going to explain how you can get out of stock items in Magento2.


Magento 2 Get out of stock Products Collection Query xample

You can get out of stock items in Magento 2 Simply as below-

Magento 2 Get out of stock Products Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollectionFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory
    ->create()
    ->addAttributeToSelect('*')
    ->addAttributeToSort('created_at', 'DESC')
    ->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0')
    ->setPageSize(10)
    ->load();

The above example will give you list of items that are not in stock.

Magento 2 Apply Coupon Code Programmatically


Magento 2 Apply Coupon Code Programmatically– Sometimes we need to apply coupon code programmatically in Magento2. It is a little bit different than magento 1.x. Here in this tutorial, we are going to explain how you can apply coupon code in magento2 programmatically.


Magento 2 Apply Coupon Code Programmatically Example

Magento 2 Apply Coupon Code Programmatically

You can use dependency injection or object manager to apply coupon code in Magento 2. Here is an example-

Magento 2 Apply Coupon Code Programmatically Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');
$couponCode = 'your_coupon_code';
$quote = $cart->getQuote()->setCouponCode($couponCode)
                                  ->collectTotals()
                                  ->save();

You can apply any custom coupon simply as above example.