Fme Extensions Blog

Latest news, tutorials, and best practices for Magento

December 13, 2023Magento: Extensions, Custom Development or Both? Choose Wisely

Hey there, fellow e-commerce enthusiasts! Let's talk about spicing up your online business game with some top-notch solutions. If you've been on the lookout for ways to boost your online store's efficiency, you're in luck! Enter Magento - the superhero among e-commerce platforms that's been hitting the mark.

December 11, 2023Top Magento Alternatives and Competitors in 2024

Hey, savvy e-commerce wizards! Are you ready for a whirlwind adventure into the vibrant world of online selling? Picture this: you're about to embark on a quest to find the ultimate e-commerce platform that'll skyrocket your business to the stars. Today, we're spilling the beans on the hottest topic around – the top alternatives to Magento in 2024.

December 6, 2023Magento Security: Measures & Best Practices

Hey there, fellow Magento store managers!

April 28, 2024Magento 2: How to Get Product Collection Filter by Category ID?

Magento offers a wide range of product management related functionality. One of these functionalities is the Magento 2 get product collection by category id. Although not a very frequently used feature, as a Magento developer, knowing it will come in handy at some point. This article discusses the get product collection by category id in Magento 2. Before doing so, let’s talk about why filtering by product ID can be useful. Importance of Magento 2 Get Product by Category ID 1. Custom Category Pages Suppose you need to create a new page that only displays products from categories A, B, and C. One option is to select each product manually. This won’t be a problem if you have very little products. But if you have 100s or 1000s of products, doing so manually can be frustrating and time consuming. By filtering products through category ID, you can easily set up a new page with the relevant products. 2. Marketing Purposes Generic marketing campaigns serve no purpose. In today’s eCommerce market, only personalised and targeted marketing campaigns create valuable. Using the Magento 2 get product by category ID functionality, you can create marketing campaigns based on specific IDs. 3. Reporting and Analytics You cannot run a successful eCommerce business without making informed decisions. By filtering products by category ID in Magento 2, you can easily create performance reports for specific product categories. This gives you detailed insight into how each category is performing and to improve sales performance. Of course, there are countless other reasons why you must know this Magento 2 functionality. Nevertheless, the above 3 points do give you an idea about its importance. So here we are with the solution to Get Product Collection Filter by Category ID using Object Manager in Magento 2. Get Product Collection from Single Category ID in Magento 2 The following is the example to get the list of products from one category with details like product name, url, price etc. in Magento 2 using Object Manager. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryId = 10; $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create(); $category = $categoryFactory->load($categoryId); $products = $category->getProductCollection() ->addAttributeToSelect('*'); foreach ($products as $product) { echo $product->getId() . "<br />"; echo $product->getName() . "<br />"; echo $product->getProductUrl() . "<br />";  } Get Product Collection from Multiple Category ID’s in Magento 2 If you want to get product collection from multiple category ids, apply the following code. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $ids = [7,8,9]; $collectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create(); $products = $collectionFactory->addAttributeToSelect('*') ->addCategoriesFilter(['in' => $ids]); foreach ($products as $product) { echo $product->getId() . "<br />"; echo $product->getName() . "<br />"; echo $product->getProductUrl() . "<br />";  } Final Thoughts on Get Product Collection by Category ID in Magento 2 This concludes our article on getting product by category ID in Magento 2. As you can see, the steps are quite easy. If you have any questions or need help, please don’t hesitate to contact our support team . Related Articles: Load Product by SKU in Magento 2 Load Product by ID in Magento 2 Magento 2 URL Rewrites - Complete Overview Reindexing Magento 2 - Complete Guide

April 28, 2024How to Set, Retrieve & Unset Session Variable in Magento 2?

If you wish to personalise the user experience and ensure your store remains ahead of the competition, you need to understand about session variables. A session is the time that a user spends on a website from logging in to logging out. It temporarily stores information related to the user’s activities while they browse the site. When a user logs into a website, a session is created with unique session ID. Session variables are used to store temporary information, sometimes for retrieving and viewing data on multiple web pages. Knowing to how to retrieve this information is important if you are running an online store. This article demonstrates the code to set and unset different types of sessions (Catalog, Customer, Checkout) in Magento 2 and how to retrieve information from them. Why Are Sessions Important? Why do you need to know about Magento 2 set session variable or the Magento 2 unset session variable? For this, you need to understand the importance of sessions. 1. User Differentiation One of the top reasons you need to know about session variables is to identify the users. Whenever a session is started, the website assigns a unique ID to the user. As they interact with your website, you can easily map their journey with the session variable. 2. Personalisation The next thing you would be wondering is why would anyone want to track a visitor’s journey. Well, personalisation is the key to success in eCommerce. A session variable helps the website store user-specific information. This could include their product preferences and other online shopping behaviour. With the help of this information, websites can target the user with relevant ads and product recommendations. 3. Authentication Without session data storage, a user will need to provide authenticate at every step. It means they would need to keep adding their username and password. It can become extremely frustrating and undermine the user experience. Once the user authenticates their credentials at the start, a session helps ensure that there is no need to provide the same information repeatedly. 4. Shopping Cart Functionality In eCommerce, sessions ensure seamless management of the shopping cart. A user will add, remove, or modify products in their cart as they browse your store. Without session variables, it is not possible. Given how important the shopping cart functionality is in eCommerce, we can safely conclude that knowing about sessions is mandatory. Magento 2 Session Type The following are the different types of session classes in Magento 2. vendor/magento/module-catalog/Model/Session.php vendor/magento/module-newsletter/Model/Session.php vendor/magento/module-persistent/Model/Session.php vendor/magento/framework/Message/Session.php vendor/magento/module-customer/Model/Session.php vendor/magento/module-backend/Model/Session.php vendor/magento/module-checkout/Model/Session.php Below is the code to deal with Catalog, Customer, and Checkout sessions using Dependency Injection (DI) and Object Manager methods. Method 1: Dependency Injection The following code will call Catalog, Customer, and Checkout sessions. <?php namespace FMEextensions\HelloWorld\Block; class HelloWorld extends \Magento\Framework\View\Element\Template { protected $_catalogSession; protected $_customerSession; protected $_checkoutSession; public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\Session $catalogSession, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, array $data = [] ) { $this->_catalogSession = $catalogSession; $this->_checkoutSession = $checkoutSession; $this->_customerSession = $customerSession; parent::__construct($context, $data); } public function _prepareLayout() { return parent::_prepareLayout(); } public function getCatalogSession() { return $this->_catalogSession; } public function getCustomerSession() { return $this->_customerSession; } public function getCheckoutSession() { return $this->_checkoutSession; } } ?> Now, we set and get sessions from a template (.phtml) file. $block->getCatalogSession()->setMyName('FME Extensions'); echo $block->getCatalogSession()->getMyName() . ' '; // output: FME Extensions $block->getCheckoutSession()->setTestData('Sample Data'); echo $block->getCheckoutSession()->getTestData() . ' '; // output: Sample Data $block->getCustomerSession()->setTestHello('Test Sample Data'); echo $block->getCustomerSession()->getTestHello() . ' '; // output: Test Sample Data You can unset these sessions using the following code. $block->getCatalogSession()->unsMyName(); $block->getCheckoutSession()->unsTestData(); $block->getCustomerSession()->unsTestHello(); From customer session, we can fetch customer information like customer name and email. // get customer data if ($block->getCustomerSession()->isLoggedIn()) { $customerId = $block->getCustomerSession()->getCustomerId(); $customerData = $block->getCustomerSession()->getCustomer(); echo $customerId . ' '; echo $customerData->getFirstname() . ' ' . $customerData->getLastname() . ' '; echo $customerData->getEmail() . ' '; print_r($block->getCustomerSession()->getCustomer()->getData()); } From the checkout session, we can fetch quote information. // get checkout session data echo $block->getCheckoutSession()->getQuoteId(); print_r($block->getCheckoutSession()->getQuote()->getData()); Method 2: Using Object Manager Use the following code to deal with session variables using Object manager. $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $catalogSession = $objectManager->get('\Magento\Catalog\Model\Session'); $customerSession = $objectManager->get('\Magento\Customer\Model\Session'); $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session'); // set session variables and their value $catalogSession->setMyName('FME Extensions'); $checkoutSession->setTestData('Sample Data'); $customerSession->setTestHello('Test Sample Data'); // print session variables value echo $catalogSession->getMyName() . ' '; // output: FME Extensions echo $checkoutSession->getTestData() . ' '; // output: Sample Data echo $customerSession->getTestHello() . ' '; // output: Test Sample Data // Unset session $catalogSession->unsMyName(); $checkoutSession->unsTestData(); $customerSession->unsTestHello(); Conclusion These are easy and straightforward ways to set and unset sessions in Magento 2. If you have any questions, please don’t hesitate to contact us . Our Magento development experts will get back to you promptly. Other Articles: How to Disable Add to Compare Button in Magento 2? How To Import and Export Product Reviews in Magento 2? How To Redirect Customers to the Previous Page After Login in Magento 2? How to Check if Customer is Logged in or Not in Magento 2?