How to apply OR conditions to collection in Magento 2?

In Magento 2, You can apply OR conditions on collection. By default, When you work with collection, Your conditions will be AND.

AND Condition
Fetch collection of only those products whose sku is like `24MB%` AND `type_id` is equal to simple.

$collection = $this->productCollectionFactory->create();
$collection->addAttributeToFilter('sku', array('like' => '24MB%'));
$collection->addAttributeToFilter('type_id', array('eq' => 'simple'));

Output:
SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` LIKE ’24MB%’) AND (`e`.`type_id` = ‘simple’); Continue reading “How to apply OR conditions to collection in Magento 2?”

Get list of Layout XML called for a current page Magento 2.

You can get a list of called Layout XML for specific pages by Magento\Framework\App\View class.

Call getLayout() method from Magento\Framework\App\View.php class.

getLayout()->getUpdate()->getHandles() used for getting all the handles of a page.

You need to keep below code at the end of root index.php file to check the available layout XML files for a page. Continue reading “Get list of Layout XML called for a current page Magento 2.”

Display success and error messages using ManagerInterface Magento 2

Magento 2, You can show Success and Error Message Using Interface Magento\Framework\Message\ManagerInterface.

You can display success message Using addSuccessMessage( ) and display error message using addErrorMessage( ) function.

addError( ), addSuccess( ), addWarning( ) and addNotice( ) methods are deprecated.

Continue reading “Display success and error messages using ManagerInterface Magento 2”