Magento Tutorial
-
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.
-
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
-
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
-
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(
-
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 -
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"
-
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
-
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
-
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 folder -
February 16, 2024
This article shows how to get shopping cart items/products, subtotal and grand total of cart, and shipping and billing address entered while doing checkout in Magento 2.
I will be using both Dependency Injection (DI) and Object Manager in the below example code.
Using Object Manager
– Get products id, name, price, quantity, etc. present in your cart.
– Get number of items in cart and total quantity in cart.
– Get base total price and grand total price of items in cart.
– Get billing and shipping addresses entered during checkout.Get all items information in cart
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); // retrieve quote items collection $itemsCollection = $cart->getQuote()->getItemsCollection(); // get