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
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.
Magento 2 Download Files
Magento 2 Download Files Script– We can create controller action script to download files in Magento2. Let us understand how we can create controller action in magento2 to download the files.
Magento 2 Download Files Programmatically Example
Create controller which extends \Magento\Backend\App\Action. Here is an example of download controller action
–
Magento 2 Download Files Example:
_downloader = $fileFactory; $this->directory = $directory; parent::__construct($context); } public function execute() { $fileName = 'myTextFile.csv'; $file = $this->directory->getPath("media")."/myfolder/".$fileName; $content = @file_get_contents($file); /**Do Your Stuffs**/ return $this->_downloader->create( $fileName, $content ); } } |
In the above example Http File Factory is used to force and download the file. You can set the file path, file name and file content that needs to be downloaded simply as in the above example.
Magento 2 Send email programmatically
Magento 2 Send email programmatically transactional emails– Sending emails is the important functionality of any framework, Magento2 has its own to handle the mail functionality. Here in this post we are going to explain how you can send transactional emails programmatically with or without template from your custom module.
Magento 2 Send email programmatically | Transactional Emails Example
You can use the following steps to send emails easily-
- 1. Create Template
- 2. Create Config
- 3. Create Function
1. Create Template
Now create template with some sample text located at the below specified path.
/app/code/VendorName/ModuleName/view/frontend/email/modulename/myTemplate.html
Magento 2 Send email programmatically Template:
|
2. Create Config
Now create configuration file to declare template-
/app/code/VendorName/ModuleName/etc/email_templates.xml
Magento 2 Send email configuration Example:
|