We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    Magento 2: Magento 2.2.6 “Clear Shopping Cart” button not working on cart page

    I have encountered that in Magento 2.2.6 “Clear Shopping Cart” button is not working on cart page.

    I have found out that in vendor/magento/module-checkout/view/frontend/templates/cart/form.phtml -line 27, Magento Team seems missed to put a dot for the ‘action’ classname.

    <table id="shopping-cart-table"
    class="cart items data table"
    data-mage-init='{"shoppingCart":{"emptyCartButton": "action.clear",
    "updateCartActionContainer": "#update_cart_action_container"}}'>

    Replace above code with below code:

    <table id="shopping-cart-table"
    class="cart items data table"
    data-mage-init='{"shoppingCart":{"emptyCartButton": ".action.clear",
    "updateCartActionContainer": "#update_cart_action_container"}}'>

    It’s a small bug that creates a big problem.

    Hopefully, this will be fixed in next release.

    Magento 2: Change Default Logo Of Admin Panel

    In Magento, there is no setting to change Magento’s backend logo.

    For that, first, you need to create a custom admin theme and then change the logo.

    Steps are listed below:

    1. app/code/[VendorName]/[ModuleName]/registration.php
      MagentoFrameworkComponentComponentRegistrar::register(
          MagentoFrameworkComponentComponentRegistrar::MODULE,
          '[VendorName]_[ModuleName]',
          __DIR__
      );
    2. app/code/[VendorName]/[ModuleName]/etc/module.xml
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
          <module name="[VendorName]_[ModuleName]" setup_version="1.0.0">
              <sequence>
                  <module name="Magento_Theme"/>
              </sequence>
          </module>
      </config>
    3. app/code/[VendorName]/[ModuleName]/etc/di.xml
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
          <type name="MagentoThemeModelViewDesign">
              <arguments>
                  <argument name="themes" xsi:type="array">
                      <item name="adminhtml" xsi:type="string">[VendorName]/[themename]</item>
                  </argument>
              </arguments>
          </type>
      </config>
    4. app/design/adminhtml/[VendorName]/[themename]/registration.php
      MagentoFrameworkComponentComponentRegistrar::register(
          MagentoFrameworkComponentComponentRegistrar::THEME,
          'adminhtml/[VendorName]/[themename]',
          __DIR__
      );
    5. app/design/adminhtml/[VendorName]/[themename]/theme.xml
      <theme xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Config/etc/theme.xsd">
          <title>Theme Title</title>
          <parent>Magento/backend</parent>
      </theme>
    6. app/design/adminhtml/[VendorName]/[themename]/Magento_Backend/layout/admin_login.xml
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-login" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <update handle="styles" />
          <body>
              <referenceBlock name="logo">
                  <arguments>
                      <argument name="logo_image_src" xsi:type="string">images/login-logo.svg</argument>
                  </arguments>
              </referenceBlock>
          </body>
      </page>
    7. Upload app/design/adminhtml/[VendorName]/[themename]/web/images/login-logo.svg                                                                     
    8. app/design/adminhtml/[VendorName]/[themename]/Magento_Backend/layout/default.xml
      <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
          <body>
              <referenceContainer name="header">
                  <referenceBlock name="logo">
                      <arguments>
                          <argument name="logo_img_width" xsi:type="number">300</argument> 
                          <argument name="logo_img_height" xsi:type="number">300</argument>
                          <argument name="show_part" xsi:type="string">logo</argument>
                          <argument name="edition" translate="true" xsi:type="string">Community Edition</argument>
                          <argument name="logo_image_src" xsi:type="string">images/menu-logo.svg</argument>
                      </arguments>
                  </referenceBlock>
              </referenceContainer>
          </body>
      </page>
    9. Upload app/design/adminhtml/[VendorName]/[themename]/web/images/menu-logo.svg                                                     
    10. Magento CLI
      php bin/magento setup:upgrade
      php bin/magento setup:di:compile

      Note: You can change the names of logo files in the configuration file if you want to use PNG files instead of SVG for the logo.

    Add new attribute in the category in Magento 1.X

    First of all, we have to create a module for adding custom category attribute.

    Step 1. Create a new module.
    We should let Magento know about our new module. The initial configuration file is located at ‘app/etc/modules/Namespace_Modulename.xml’.
    Namespace_Modulename.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Namespace_Modulename>
                <active>true</active>
                <codePool>local</codePool>
            </Namespace_Modulename>
        </modules>
    </config>

    Step 2.
    Module configuration file is located at ‘app/code/<code_pool>/<name_space>/<module_name>/etc’ and its name is config.xml – so this path looks like ‘app/code/local/Namespace/Modulename/etc/config.xml’.

    <?xml version="1.0"?>
    <config>
        <modules>
            <Namespace_Modulename>
                <version>1.0.0</version>
            </Namespace_Modulename>
        </modules>
    
        <global>
            <resources>
                <add_category_attribute>
                    <setup>
                        <module>Namespace_Modulename</module>
                        <class>Mage_Catalog_Model_Resource_Setup</class>
                    </setup>
                    <connection>
                        <use>core_setup</use>
                    </connection>
                </add_category_attribute>
                <add_category_attribute_write>
                    <connection>
                        <use>core_write</use>
                    </connection>
                </add_category_attribute_write>
                <add_category_attribute_read>
                    <connection>
                        <use>core_read</use>
                    </connection>
                </add_category_attribute_read>
            </resources>
        </global>
    </config>

    <add_category_attribute> presence that script must be located in the folder with the same name. ‘app/code/local/Namespace/Modulename/sql/add_category_attribute’

    Step 3.
    Create and install a script file in the folder ‘add_category_attribute’, and the file name depends on the module version.
    mysql4-install-1.0.0.php

    <?php
    $this->startSetup();
    $this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'attribute_custom', array(
        'group'         => 'General Information',
        'input'         => 'textarea',
        'type'          => 'text',
        'label'         => 'Custom attribute',
        'backend'       => '',
        'visible'       => true,
        'required'      => false,
        'visible_on_front' => true,
        'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    ));
    
    $this->endSetup();
    ?>

     

     

    Magento remove out of stock items from shopping cart through observer

    Magento automatically removes items from the shopping cart page which are out of stock. What happens if  the product goes out of stock and that product is already in other customer’s cart who have not yet checked it out?

    With this script, Magento will auto-check if all the items in the cart are available in-stock before proceeding for checkout page.

    In config.xml file:

    <events>
      <controller_action_predispatch_checkout_cart_index>
        <observers>
          <namespace_module_autoremove_outofstock>
            <type>singleton</type>
            <class>namespace_module/observer</class>
            <method>autoRemoveOutOfStockItems</method>
          </namespace_module_autoremove_outofstock>
        </observers>
      </controller_action_predispatch_checkout_cart_index>
    </events>

    In observer.php file:

    public function autoRemoveOutOfStockItems($observer) {
        $quote = Mage::getModel('checkout/session')->getQuote();
        $cartItems = $quote->getAllItems();
        foreach ($cartItems as $item)
        {
            //$productType = $item->getProduct()->getTypeId();
            //if($productType!='configurable') {
            $productId = $item->getProductId();
            $product = Mage::getModel('catalog/product')->load($productId);
            $stockItem = $product->getStockItem();
            if(!$stockItem->getIsInStock())
            {
                    Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
            }
            //}
        }
    
    }

     

    Magento 2 Install / Uninstall Sample Data via Command Line

    In this tutorial, we will talk about how to Install/Uninstall Sample Data via Command Line . As you know, Magento 2 have many commands in bin/magento folder. But it may be difficult to get approach with this, so let me explain about it in more detail.

    Install / Uninstall Sample Data via command line

    Go to Magento root folder and see usages:

    php bin/magento maintenance:enable
    

    Result:

    Usage:
      sampledata:deploy [options]
    
    Options:
          --no-update       Update composer.json without executing composer update
      -h, --help            Display this help message
      -q, --quiet           Do not output any message
      -V, --version         Display this application version
          --ansi            Force ANSI output
          --no-ansi         Disable ANSI output
      -n, --no-interaction  Do not ask any interactive question
      -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Help:
      Deploy sample data modules

    Install Sample Data Syntax

    php bin/magento sampledata:deploy [options]
    

    By default, it will update latest version of sample data via composer, then install it. You can disable it with parameter: --no-update

    php bin/magento sampledata:deploy --no-update
    

    Uninstall Sample Data Syntax

    Go to Magento root folder and see usages:

    php bin/magento sampledata:remove -h
    

    Output:

    Usage:                                                                                                                     
      sampledata:remove [options]                                                                                              
                                                                                                                               
    Options:                                                                                                                   
          --no-update       Update composer.json without executing composer update                                             
      -h, --help            Display this help message                                                                          
      -q, --quiet           Do not output any message                                                                          
      -V, --version         Display this application version                                                                   
          --ansi            Force ANSI output                                                                                  
          --no-ansi         Disable ANSI output                                                                                
      -n, --no-interaction  Do not ask any interactive question                                                                
      -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug 
                                                                                                                               
    Help:                                                                                                                      
      Remove all sample data packages from composer.json

    Syntax:

    php bin/magento sampledata:remove [options]   
    

     

    Magento 2 How to Add Admin User via Command Line

    In this tutorial, we will talk about How to Add Admin User via Command Line. As you know, from Magento 2, they add many commands in bin/magento. It may be difficult to get approach with this, so let me explain about ‘How to Add Admin User via Command Line’  in more detail .

    How to Add Admin User via Command Line

    Go to Magento root folder and see the usage:

    php bin/magento admin:user:create --help
    

    Output:

    Usage:
      admin:user:create [options]
    
    Options:
          --admin-user=ADMIN-USER                    (Required) Admin user
          --admin-password=ADMIN-PASSWORD            (Required) Admin password
          --admin-email=ADMIN-EMAIL                  (Required) Admin email
          --admin-firstname=ADMIN-FIRSTNAME          (Required) Admin first name
          --admin-lastname=ADMIN-LASTNAME            (Required) Admin last name
          --magento-init-params=MAGENTO-INIT-PARAMS  Add to any command to customize Magento initialization parameters
                                                     For example: "MAGE_MODE=developer&MAGE_DIRS[base][path]=/var/www/example.com&MAGE_DIRS[cache][path]=/var/tmp/cache"
      -h, --help                                     Display this help message
      -q, --quiet                                    Do not output any message
      -V, --version                                  Display this application version
          --ansi                                     Force ANSI output
          --no-ansi                                  Disable ANSI output
      -n, --no-interaction                           Do not ask any interactive question
      -v|vv|vvv, --verbose                           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    
    Help:
      Creates an administrator

    Let’s create a admin account with the following information:

    User: magemonkeys
    Password: WeLoveMagento123
    Email: hi@magemonkeys.com
    First Name: magemonkeys
    Last Name: Family
    See the command line is:

    php bin/magento admin:user:create --admin-user=magemonkeys --admin-password=WeLoveMagento123 --admin-email=hi@magemonkeys.com --admin-firstname=magemonkeys --admin-lastname=Family
    

    And thus we have successfully created a new admin account, it returned message:

    Created Magento administrator user named magemonkeys 
    

     

    Overriding controller in Magento 1.9

    Your controller rewrite XML part should look like this:

    <frontend>
        <routers>
            <contacts>
                <args>
                    <modules>
                        <Namespace_Modulename before="Mage_Contacts">Namespace_Modulename</Namespace_Modulename>
                    </modules>
                </args>
            </contacts>
        </routers>
    </frontend>

    As you can see instead of using <contactsmodule> node we are here using <contacts> node. <contacts> is the router node which is used by Mage_Contacts module.

    After this change is done, do not forget to flush the cache again.

    It is a good practice to keep dependency in your module as shown below.

    File : Namespace_Modulename.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Namespace_Modulename>
                <active>true</active>
                <codePool>local</codePool>
                <depends>
                    <Mage_Contacts />
                </depends>
            </Namespace_Modulename>
        </modules>
    </config>

    This clearly indicates that your module has a dependency on Mage_Contacts module and thus Magento will load your module only after Mage_Contacts is loaded.

     

    How to show CMS static block in Magento 2

    Are you wondering, how to show CMS Static Block in Magento 2?

    Here, you will find effective instruction about this topic.

    Call from Phtml File:

    echo $this->getLayout()
              ->createBlock('MagentoCmsBlockBlock')
              ->setBlockId('your_block_identifier')
              ->toHtml();

    Call from CMS page or block:

    {{block class="MagentoCmsBlockBlock" block_id="your_block_identifier"}}
    

    Call from XML File:

    <referenceContainer name="content">
        <block class="MagentoCmsBlockBlock" name="block_identifier">
            <arguments>
                <argument name="block_id" xsi:type="string">block_identifier</argument>
            </arguments>
        </block>
    </referenceContainer>

    Hope this helps you!

    How to Override a Helper File in Magento 2

    To override Magento 2 Helper file, follow below steps.

    First, you need to create a di.xml file at below desired location.

    Go to Vendor/Extension/etc/di.xml And add the following code into di.xml

    Magento 2: Creating and Calling Custom Popup using Modal

    Here we are discussing how to create and call custom popup using Magento 2’s Popup Modal Library.

    You should create a module first, then after create a requirejs-config.js file at app/code/CustomVendor/CustomModule/view/frontend/

    var config = {
        paths: {            
             'myjs': "CustomVendor_CustomModule/js/customfile"
          },   
        shim: {
        'myjs': {
            deps: ['jquery']
        }
      }
    }

    You need to create customfile.js at app/code/CustomVendor/CustomModule/view/frontend/web/js/

    <script>
        require(
            [
                'jquery',
                'Magento_Ui/js/modal/modal'
            ],
            function(
                $,
                modal
            ) {
                var options = {
                    type: 'popup',
                    responsive: true,
                    innerScroll: true,
                    buttons: [{
                        text: $.mage.__('Continue'),
                        class: 'mymodal1',
                        click: function () {
                            this.closeModal();
                        }
                    }]
                };
    
                var popup = modal(options, $('#custompopup'));
                $("#click-here").on('click',function(){
                    $("#custompopup").modal("openModal");
                });
            }
        );
    </script>

    Add following code in your phtml file:

    <div data-mage-init='{"myjs": {}}'>
        <a href="#" id="click-here">Click Here</a>
    </div>
    
    
    <div id="custompopup" >
        This is custom popup using magento popup modal.
    </div>