Category Archives: Magento 2.0 Tutorial

Magento 2 check product type is Virtual


Magento 2 check product type is Virtual– We sometimes need to check the product type is “Virtual product” or not. We can check get product type Virtual using product type id property. Here in this tutorial, we are going to explain how you can check whether a product is Virtual or not in Magento 2.


Magento 2 check product type is Virtual Example

Magento 2 check product type is Virtual

You can check if product type is Virtual or not simply as below-

Example:

$productId = 199;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$productType = $product->getTypeID();

if($productType == "virtual")
{   
  echo "Product is Virtual Product";
} 

$productType will give you “virtual” which means product type is virtual.

Magento 2 check if product type is grouped


Magento 2 check if product type is grouped– We sometimes need to check the product type is “group product” or not. We can check get product type grouped using product type id property. Here in this tutorial, we are going to explain how you can check whether a product is grouped or not in Magento 2.


Magento 2 check if product type is grouped Example

Magento 2 check if product type is grouped

You can check if product type is bundle or not simply as below-

Example:

$productId = 199;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$productType = $product->getTypeID();

if($productType == "grouped")
{   
  echo "Product is Group Product";
} 

$productType will give you “grouped” which means product type is grouped.

Magento 2 check if product type is bundle


Magento 2 check if product type is bundle– We sometimes need to check the product type is “bundle” or not. We can check get product type bunlde using product type id property. Here in this tutorial, we are going to explain how you can check whether a product is bundle or not in Magento 2.


Magento 2 check if product type is bundle Example

Magento 2 check if product type is bundle

You can check if product type is bundle or not simply as below-

Example:

$productId = 199;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$productType = $product->getTypeID();

if($productType == \Magento\ConfigurableProduct\Model\Product\Type\Bundle::TYPE_CODE)
{   
  echo "Product is Bundle";
} 

$productType will give you “bundle” which means product type is bundle.

You can directly compare the type simply below

if($productType == "bundle")
{   
  echo "Product is bundle";
} 

You can check the product type using any of the above method in magento 2.

Magento 2 check if product type is simple


Magento 2 check if product type is simple– We sometimes need to check the product type is “simple” or not. We can check get product type simple using product type id property. Here in this tutorial, we are going to explain how you can check whether a product is simple or not in Magento 2.


Magento 2 check if product type is simple Example

Magento 2 check if product type is simple

You can check if product type is simple or not simply as below-

Example:

$productId = 199;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$productType = $product->getTypeID();

if($productType == \Magento\ConfigurableProduct\Model\Product\Type\Simple::TYPE_CODE)
{   
  echo "Product is simple";
} 

$productType will give you “simple” which means product type is simple.

You can directly compare the type simply below

if($productType == "simple")
{   
  echo "Product is simple";
} 

You can check the product type using any of the above method in magento 2.

Magento 2 check if product is configurable


Magento 2 check if product is configurable– It is very simple to check that product type is configurable or simple. We can get product type form product object in magento 2. Here in this article we are going to explain how you can check whether product is configurable or not.


Magento 2 check if product is configurable Example

Magento 2 check if product is configurable

You can check if product is configurable or not simply as below-

Example:

$productId = 199;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$productType = $product->getTypeID();

if($productType == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE)
{   
  echo "Configurable Product";
} 

$productType will give you “configurable” which means product is configurable.

Magento 2 get product stock detail


Magento 2 get product stock detail– We can get product stock information using \Magento\CatalogInventory\Model\Stock\StockItemRepository object. We can inject this object to class or object manager. Here in this article, we are going to explain both the methods.


Magento 2 get product stock detail | Availability Example

You can get the following product stock information –

  • Quantity(Qty)
  • Minimum Quantity(min_qty)
  • Minimum Sale Quantity(min_sale_qty)
  • Maximum Sale Quantity(max_sale_qty)
  • Stock Availability(is_in_stock)

Magento 2 get product stock detail

Method 1

Module name is – Tutorialsplane_HelloWorld, Inject object \Magento\CatalogInventory\Model\Stock\StockItemRepository in class constructor of block. Here is sample block-

_stockItemRepository = $stockItemRepository;
        parent::__construct($context, $data);
    }
    
    public function getStockItem($productId)
    {
        return $this->_stockItemRepository->get($productId);
    }
}

Now you can load product by id and get stock information in template file-

$productId = 120;
$stock = $block->getStockItem($productId);
$qty = $stock->getQty(); 
$minQty = $stock->getMinQty();
$minSaleQty = $stock->getMinSaleQty(); 
$maxSaleQty = $stock->getMaxSaleQty(); 
//checks product is in stock or not
$isInStock = $stock->getIsInStock();

Method 2 Using Object Manager

You can use object manager to get stock information in Magento 2 simply as below-

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance(); 
$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend'); 
$stockItem = $objectManager->get('\Magento\CatalogInventory\Model\Stock\StockItemRepository');
 
$productId = 120;
 
$stock = $stockItem->get($productId);

$qty = $stock->getQty(); 
$minQty = $stock->getMinQty();
$minSaleQty = $stock->getMinSaleQty(); 
$maxSaleQty = $stock->getMaxSaleQty(); 
//checks product is in stock or not
$isInStock = $stock->getIsInStock();

You can use any of the above method to get the product stock information in Magento 2. You can use the above method to check whether a product is in stock or out of stock.

Magento 2 get cart Shipping Cost


We can get shipping amount from quote object. You can use magento di(dependency injection) or object manager to get the shipping cost from quote session. Here in this tutorial, we are going to explain how you can get cart shipping cost in Mgento 2 from cart session.


Magento 2 get cart Shipping Cost | Amount Example

You can get shipping cost from cart object simply as below-

Example:

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

First get the object manager instance, using object manager to access the Magento Checkout Model Cart object. After getting quote object get shippingAddress object to access the shipping amount.

On the same way you can get other details about shipping.

Magento 2 Join 3 Tables


Magento 2 Join 3 Tables collection –We can join more than two table collections in Magento 2 which will return data collectively from three tables using join. You can also add conditions to the collection as per your need. Here in this tutorial, we are going to explain how to join 3 tables in Magento 2.


Magento 2 Join 3 Tables Colleaction Example

Here is an example of 3 table joins-

Example:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$collection->getSelect()
->join( array('order_item'=> 'sales_order_item'),
'order_item.order_id = main_table.entity_id', array('order_item.sku'))
->join( array('quote'=> 'quote'),
'quote.quote_id = main_table.quote_id', array('quote.grand_total')); 

Magento 2 Right Join Collection


Magento 2 Right Join Collection– It is very simple to right join two collections or two tables in Magento 2. Let us create a simple example to understand the right join query in Magento 2.


Magento 2 Right Join Collection Example

You can right join two or more than simply as below-

Magento 2 Right Join Collection Example:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$collection->getSelect()
->joinRight( array('order_item'=> 'sales_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku')); 

On the same way you can join any collection.