Magento 2 Development

  1. Magento 2 Object Manager

    Magento 2 Object Manager is a PHP class responsible for creating and retrieving objects in Magento 2. It also manages to create factories and proxies.

    To get the object manager instance use the code:

    <?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

    Using the ObjectManager you can get a singleton object (method "get") of PHP class or create a new one (method "create"). Example:

    <?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    /* Create a new product object */
    $product = $objectManager->create(\Magento\Catalog\Model\Product::class);
    Read more »
  2. Create Configurable Product in Magento 2 Programmatically

    Creating configurable products you just` create simple products with some configurable options for customers to choose from. So, to create a configurable product programmatically you should start by creating a simple product and then assigning some features to it. 

    Follow these steps to create a configurable product in Magento 2 programmatically:

    Read more »
  3. How to Create Simple Product Programmatically in Magento 2?

    Magento 2 being a multifunctional e-commerce platform allows you to create products from the admin panel. There are plenty of options to fill out to create a simple product in Magento 2, which obviously takes some time.

    And what if you have to create a huge amount of products, especially during the development or testing? 

    The easiest, in this case, would be to create products programmatically. And that is exactly what you're going to learn in this article.

    Use the following method to create a simple product in Magento 2 programmatically:

    getObjectManager();
    $state = $objectManager->get('\Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    Read more »
  4. How to Pass Magento 2 Certification [Useful Tips]

    Magento has developed several certificates for Magento 2, one of which is — Magento 2 Certified Professional Developer.

    To get this certificate, you need to take an exam to test your knowledge and skills required to modify Magento 2 in the following areas: user interface, databases, admin panel, ordering process, catalog and extensions functionality.

    The Magento 2 Developer Exam is for those who have a deep understanding of the Magento concepts and experience of developing Magento 2. It is recommended that you have not less than 1.5 years of experience in customizing Magento 2 before applying for the exam.

    The exam consists of 60 single and multiple choices questions in English. The duration time is 90 minutes (1.5 hour). To successfully pass the exam you need to receive 64% or higher.

    In this article, we will walk you through the low-cost options to prepare for the Magento 2 Certification without courses such as, for example, Magento U for $ 950.

    Read more »
  5. How to Create Custom Cron Job in Magento 2?

    Magento 2 is a complex unity of features that help you to build a successful, easily-manageable store. There are a lot of processes to be handled in Magento 2 admin that require automation and scheduling. And that is exactly where cron job comes in handy. So...

    Let's find out what exactly is it, why you need it and how to create custom cron job in Magento 2.

    What is Magento cron job?

    Magento cron job — is one of the most important Magento 2 features. It helps to configure commands or script that systematically runs and performs the tasks you intend it to.

    With the cron job you don't need to manually reindex, generate google sitemaps, send Magento emails, update currency rates etc. The cron job does it all for you automatically, on a specific date and time if you configure it correctly. 

    Read more »
  6. How to Run Magento 2 Code in External File/Script?

    Sometimes you need to run Magento 2 code externally, in the following cases:

    • Magento 2 integration with other frameworks or platforms that are installed on the same web server,
    • quick test execution of some method, for example, cron job.

    Read more »
  7. How to Create Plugin (Interceptor) in Magento 2?

    Now, Magento 2 has the plugins, interceptors, which allow you to execute your own code before/after calling the public class method.

    Following steps to create plugin in Magento 2:

    1. Create the etc/di.xml file in your module folder and add the following code there:

    <?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="VendorName\ModuleName\Foder\SomeModel">
            <plugin name="mycompany_mymodule_plugin_modulename_foder_somemodel"
                    type="MyCompany\MyModule\Plugin\ModuleName\Foder\SomeModelPlugin" sortOrder="10" />
        </type>
    </config>
    Read more »
  8. Load Data from Magento 2 Database

    In the previous article you learned how to create new tables in Magento 2 database. In this one, you will learn more about the models used to work with the Magento 2 database which allows you to read, edit and delete the data. 

    Magento 2 and Magento 1 uses the Model/ResourceModel/Collection ORM (Object-relational mapping) for these purposes. To implement this concept you need to create 3 files (model, resource model, collection).

    Read more »
  9. Override Magento 2 Storefront View Files in Theme

    When developing an online store based on Magento 2, you might face the problem of extending the standard Magento 2 or Magento 2 modules functionality.

    Editing Magento core files or modules is not allowed because these changes will be overwritten during the upgrade.

    So, how to make changes to the storefront (frontend) template file, css, js?

    WARNING! The following instructions cannot be used to override the layout files.

    Override module view-files in the app/code folder.

    Let's say you need to change this file:

    app/code/Magefan/Blog/view/frontend/templates/post/view.phtml

    Make its copy in the theme folder with the following path:

    app/design/frontend/ThemeVendor/themename/Magefan_Blog/templates/post/view.phtml

    Make the necessary changes to the newly created theme file.

    If your own theme is missing from your Magento installation, create one.

    Read more »
  10. Change Attribute from DROPDOWN to TEXT in Magento 2

    If you want to change product attribute type from dropdown to text in Magento 2, all you need to do is to run the following SQL queries (please make a Database backup before execution):

    1. This query will change the attribute settings and convert it to a text attribute.

    UPDATE eav_attribute SET
    backend_type = "varchar",
    frontend_input = "text",
    source_model = ""
    WHERE attribute_code = "MY_ATTRIBUTE_CODE";

    2. This query will copy dropdown attribute data to the text attribute value table and replace the option IDs with their actual labels (text).

    INSERT INTO catalog_product_entity_varchar
    SELECT null as value_id, pei.attribute_id, pei.store_id, pei.entity_id, aov.value as value
    FROM catalog_product_entity_int pei
    LEFT JOIN eav_attribute_option ao ON pei.attribute_id = ao.attribute_id
    LEFT JOIN eav_attribute_option_value aov ON ao.option_id = aov.option_id
    WHERE
    pei.value IS NOT NULL
    AND pei.attribute_id
    Read more »
  11. Magento 2 Get Current Store ID, Code, Name, Website ID, URL

    Sometimes, during Magento 2 customization you need to get the store Information programmatically. You may need to get a current Store ID, Store Code, Name, Website ID, or current URL. To retrieve this data use the singleton instance of the following class:

    \Magento\Store\Model\StoreManagerInterface

    For example, you can include it in your class constructor and then call:

    <?php

    namespace \MyCompany\ExampleModule\Model;

    class Example
    {
    private $storeManager;
    public function __construct( ... \Magento\Store\Model\StoreManagerInterface $storeManager, ... ) {
    ... $this->storeManager = $storeManager; ... }     /**
        * Examples
        */
        public function execute()
        {
        /* Get Current Store
    Read more »
  12. Get URLs in Magento 2

    Get URL in the .phtml template files

    1. Get URL of the store homepage (http://domain.com/):

    <?= $this->getUrl() ?>

    2. Get URL of some page, e.g. "Contact Us" (http://domain.com/contacts/):

    <?= $this->getUrl('contacts') ?>

    3. Get URL of the theme static file, e.g.:

    app/design/frontend/ThemeVendor/theme_name/web/css/custom.css
    app/design/frontend/ThemeVendor/theme_name/Magefan_Blog/web/js/lazyload.js
    app/code/Magefan_Blog/view/frontend/web/js/lazyload.js
    vendor/magefan/module-blog/view/frontend/web/js/lazyload.js

    use the following code:

    <?= $this->getViewFileUrl('css/custom.css') ?>
    <?= $this->getViewFileUrl('Magefan_Blog::js/lazyload.js') ?>
    Read more »
  13. List of Magento 2 Admin Panel Menu Identifiers

    Menu Title [identifier]

    Dashboard [Magento_Backend::dashboard]
    Sales [Magento_Sales::sales] Operations [Magento_Sales::sales_operation] Orders [Magento_Sales::sales_order] Invoices [Magento_Sales::sales_invoice] Shipments [Magento_Sales::sales_shipment] Credit Memos [Magento_Sales::sales_creditmemo] Billing Agreements [Magento_Paypal::paypal_billing_agreement] Transactions [Magento_Sales::sales_transactions]
    Read more »
  14. How to Add New Element to Magento 2 Admin Panel Menu?

    The etc/adminhtml/menu.xml file is used to control the Magento 2 admin panel menu and add new items to it, in particular.

    Меню адмін-панелі Magento 2

    Read more »
  15. Default Values for the Magento 2 Configuration Page

    In the previous article we described how to create your own section on the Magento 2 configuration page (Stores > Configuration).

    In order to set the default values for the configuration fields, you need to create the following file in the module folder:

    etc/config.xml

    and add this code:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <default>
            <section_id>
                <group_id>
                    <field_id>default_value</field_id>
                </group_id>
            </section_id>
        </default>
    </config>

    Read more »
  16. Extend the Magento 2 Access Control List

    In the previous article we explained how to configure and delimitate access rights for the Magento 2 admin panel users. In this article, you will learn how to create your own access rules (Role Resources).

    User Role Reasources in Magento 2

    You need to create ACL file (ACL - Access Control List) in your module folder:

    etc/acl.xml

    Read more »
  17. How to Create Configuration Section (system.xml) in Magento 2?

    To find the Magento 2 configuration page navigate to Magento 2 Admin Panel > Stores > Configuration.

    Налаштування Magento 2

    All tabs and forms on this page are customized using this file

    etc/adminhtml/system.xml

    Read more »
  18. How to Create Database Tables in Magento 2?

    To add a new table to the Magento 2 database, you need to create a file in the module folder:

    app/code/<VendorName>/<ModuleName>/Setup/InstallSchema.php

    add the following code to it:

    <?php
    namespace VendorName\ModuleName\Setup;
    use Magento\Framework\Setup\InstallSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;
    use Magento\Framework\DB\Adapter\AdapterInterface;
    class InstallSchema implements InstallSchemaInterface
    {
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            $installer = $setup;
            $installer->startSetup();
            //new table script will be there
            $installer->endSetup();
        }
    }

    Read more »
  19. How to create the first block in Magento 2 (Block.php + template.phtml + layout.xml)

    In the previous article we showed how to display the "Hello World" text on your own page. In this article we will show you how to display it in the new block.

    1. Add a new PHP class block.

    Create this file:

    app/code/<VendorName>/<ModuleName>/Block/SomeName.php

    and add the following code to it:

    <?php
    namespace VendorName\ModuleName\Block;
    class SomeName extends \Magento\Framework\View\Element\Template
    {
        public function getWelcomeText()
        {
            return 'Hello World';
        }
    }

    where, 

    SomeName is a random name in CamelCase format.
    \Magento\Framework\View\Element\Template — a class from which you inherit your own block that interacts with the template.
    getWelcomeText — a public method we created to return the text "Hello World". You can create a name for it yourself.

    Read more »
  20. First Controller. Magento 2 "Hello World"

    To display "Hello World" on your own page in Magento 2, follow these steps:

    1. Register a router for the storefront.

    Create this file:

    app/code/<VendorName>/<ModuleName>/etc/frontend/routes.xml

    and add the following code there:

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
            <route id="VendorName_ModuleName" frontName="path">
                <module name="VendorName_ModuleName" />
            </route>
        </router>
    </config>

    You can use the developer name (VendorName) associated with the module name (ModuleName) as the router id. The frontName is used in the URL to access your controllers.

    Both names have to be unique.

    Read more »
Posts loader
^Top
Chat