Magento Tutorial
-
- April 24, 2025
I am running a magento 2.4 project but current not in position to update mysql version. any way to skip that error ?
Magento 2 error Current version of RDBMS is not supported. Used Version: 10.1.37-MariaDB. Supported versions: MySQL-8, MySQL-5.7, MariaDB-(10.2-10.4)
-
- March 06, 2024
This article shows how we can search Magento Repositories using SearchCriteriaBuilder that implements SearchCriteriaInterface. An example is provided of how we can perform search on Product repository in Magento. Similarly, we can search for other entities like Category, Customer, Order, Invoice, CMS Page, CMS Block, etc.
We can add different conditions to build custom search criteria.
Filters
Filter class is used to add custom field, value, and condition type to the search criteria.
$filter ->setField("name") ->setValue("%laptop%") ->setConditionType("like");
condition_type
is optional.- If
condition_type
is not provided then the defaultcondition_type
is set aseq
.
Different condition types:
CONDITION NOTES eq Equals gt Greater than gteq Greater than or equal lt Less than lteq Less than or equal moreq More or equal neq Not equal in The value can contain a comma-separated list of values. nin Not in. The value can contain a comma-separated list of values. like The value can contain the
-
- March 06, 2024
Here we learn how to write custom mysql query in Magento2.
Suppose we have table ’employee’ with fields emp_id, emp_name, emp_code and emp_salary.
Custom Mysql Query
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $tableName = $resource->getTableName('employee'); //gives table name with prefix //Select Data from table $sql = "Select * FROM " . $tableName; $result = $connection->fetchAll($sql); // gives associated array, table fields as key in array. //Delete Data from table $sql = "Delete FROM " . $tableName." Where emp_id = 10"; $connection->query($sql); //Insert Data into table $sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')"; $connection->query($sql); //Update Data into table $sql = "Update " . $tableName . " Set emp_salary = 20000 where emp_id = 12";
-
- February 28, 2024
We had a requirement to check if one of our modules is enabled or not as two of our modules were sharing the same attribute code. We wanted to make sure that we don’t try to create an attribute again if the other module already installed or enabled.
We have done this many times in Magento 1 by using the following code snippet -:
Mage::helper('core')->isModuleEnabled('vendor_modulename');
So we were wondering there must be a simple way of doing this in Magento 2 as well and yes we were not wrong. It is not a single line of code as we had in Magento 1 but there is nothing in Magento 2 which can be implemented in Magneto 2 ???? You have to add dependency to get access to the functions.
Anyways let’s crack on with the code to find out if the module exists or enabled in Magento 2
<?php class Custom { /** * @var \Magento\Framework\Module\Manager */ protected $_moduleManager; /** * @param \Magento\Framework\Module\Manager $moduleManager */ public function __construct( \Magento\Framework\Module\Manager
-
- February 26, 2024
Third Party Platform interact with Magento 2 by API. When you are working for API related stuff and using SearchCriteriaBuilder Object for your query and if you want to apply OR conditions for custom query, You need to define setFilterGroups([Object]) for OR query.
Whenever you are dealing with SearchCriteriaBuilder in Magento 2, You might be need to query with OR conditions for custom requirement, You can give OR conditions to Magento\Framework\Api\SearchCriteriaBuilder by simple code snippets.
Let’s imagine, We built a query using OR conditions for Products.
Fetch collection of only those products whose sku is like `MSH01%` OR `type_id` is equal to simple.
<?php namespace Rbj\Training\Block; class Training extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Api\FilterBuilder
-
- February 26, 2024
MySQL database query logging can be enabled/disabled from command line. This will help in debugging database queries.
Enable database query logging
The following CLI command enabled database/mysql query log:
bin/magento dev:query-log:enable
Flush cache
bin/magento cache:flush
After that, browse your Magento site.
The database queries logs are saved in the filevar/debug/db.log
.Note:
The database query log file size should be checked. Thedb.log
file can take a lot of space as the file size goes on increasing based on the visit and activity on the Magento site.By default, all the database queries are logged in the
db.log
file.
It will also include the call stack for the database query.Here’s a sample from
var/debug/db.log
:## 2024-02-25 16:15:37 ## 56786 ## CONNECT TIME: 0.0134 TRACE: #1 Magento\Framework\DB\Logger\File[Magento\Framework\DB\Logger\LoggerAbstract]#000000002566b81400000000201f09a9#->getStats('connect', '', array(), NULL) called at [vendor/magento/framework/DB/Logger/File.php:67]
-
- February 23, 2024
You can create a Plugin for the method SavePaymentInformation() in Magento 2 to add your logic before or after Payment information is saved.
The method will be called after a click on the Place order button from the second step of the Checkout page.
Original Class to create plugin:
Magento\Checkout\Model\PaymentInformationManagementYou can create a before or after plugin as per your requirement.
Create a global scope di.xml file,
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Checkout\Model\PaymentInformationManagement"> <plugin name="set_payment_data_before_save" type="Samdoit\SavePayment\Plugin\Model\SavePaymentPlugin" sortOrder="10"/> </type> </config>
Create a Plugin Class for the Before saves payment information, We have used Before Plugin type,
<?php namespace Samdoit\SavePayment\Plugin\Model; use Magento\Quote\Api\Data\AddressInterface;
-
- February 20, 2024
As Magento 2 store owner, you may want to treat logged in and non-logged in customers differently. For example, you may want to offer exclusive features or discounts to logged in customers only. To implement this, you first need to confirm whether a Magento 2 customer is logged in or not?
Methods To Check If A Customer Is Logged-In In Magento 2
There are 2 ways you can check this. By injecting class and by using object manager. Although the 2nd method (object manager) is not recommended, it’s still used as an alternative.
Method 1: By Injecting Class (Dependency Injection)
In this method, first, you need to inject the following class in the constructor method:
/Magento/Customer/Model/Session
protected $_session; public function __construct( ... \Magento\Customer\Model\Session $session, ... ) { ... $this->_session = $session; ... }
Then in your class, use the following code in any function to check if the customer is logged in or not:
if ($this->_session->isLoggedIn()) { // Customer is logged
-
- February 18, 2024
With Advanced Content Manager for Magento 2, it’s possible to index and make your content searchable.
The default catalog search layout is overridden by our extension, in order to add our results to the search.
The extension is adding a Result Search Block into the Catalog Search Result page layout:Samdoit/ContentManager/view/frontend/layout/catalogsearch_result_index.xml
<page xmlns:xsi="..." layout="2columns-left" xsi:noNamespaceSchemaLocation="..."> <body> <referenceContainer name="content"> <block class="Samdoit\ContentManager\Block\Search\Result" name="search.content.result" after="search.result" template="Samdoit_ContentManager::content/search/result.phtml" cacheable="false"> <referenceContainer> <body> <page>
The Result Block retrieves the current query and create the search filter. The block will load a ContentList Collection from the ids retrieved in the results.
This block has the following child:
<block class="Samdoit\ContentManager\Block\Content\Widget\ContentList" name="search_content_result_list"
-
- February 16, 2024
With the support of seamless customization and integrations, Magento 2 e-commerce stores can serve excellent experiences to customers. In many cases, the store owners are willing to allow the shoppers to add the product to the cart with a custom price.
Frequently, the store owner is prepared to set a fixed product pricing even if the consumer selects various options/add-ons from the front end for all or some specific products. Instead of manually changing all store products, we’ll need to manually code to override all store product prices with your preferred custom price.
In this article, I will instruct you to set a custom price of the product when adding the product to the cart by using Observe.
Let’s explore two straightforward steps below!
Step 1: Create Events/xml File
Firstly, you need to create
events/xml
in the folderMageplaza/HelloWord/etc/frontend
and use eventcheckout_cart_product_add_after
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"