1

i am using Magento 2 , In my case i want to change programmatically the instance's logo and favicon , but i can't find the right file to change them ,

can anyone help me do that please

2

For set Logo

app\code\VendoreName\ModuleName\view\frontend\layout

default.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceBlock name="logo">
        <action method="setLogoFile">
            <argument name="logo_file" xsi:type="string">VendoreName_ModuleName::images/logo.png</argument>
        </action>
    </referenceBlock>
  </body>
</page>

Put your logo image to this path app\code\VendoreName\ModuleName\view\frontend\web\images here my image file name is logo.png.

For set fevicon icon

  1. Add your icons in the <your_theme_dir>/Magento_Theme/web/ directory.
  1. In the <your_theme_dir>/Magento_Theme/layout/default_head_blocks.xml layout file specify the paths to the icons and their sizes. For example, if you added a favicon-32x32.png icon and want it to be used as a 32px x 32px favicon, your default_head_blocks.xml would be like following:
<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <link src="Magento_Theme::images/logo.png" rel="icon" sizes="32x32" />
    </head>
</page>
  1. Put your fevicon image into <your_theme_dir>/Magento_Theme/web/images/logo.png

*Note:- For your changes to be applied, clear the browser cache, and the following directories on the server (do not delete the .htaccess file!): - pub/static - all directories under var

I Hope This Helps You.*

0

Just inject the class \Magento\Config\Model\ResourceModel\Config $_resourceConfig and set value for web/default/cms_home_page to desired page ‘youridentifier’.

public function __construct(\Magento\Config\Model\ResourceModel\Config $_resourceConfig)
    {
        $this->_resourceConfig = $_resourceConfig;
    }

        $this->_resourceConfig->saveConfig('web/default/cms_home_page', 'youridentifier', 'default', 0);
8
  • Thank you for your response , this work for me but just in my PC , i want to do this for all instances so i need to do it by code ? – code 7 Sep 21 '20 at 12:24
  • No it will for all , if website is in live environment. – Hamendra Sunthwal Sep 21 '20 at 12:26
  • Clear Cache in magento – Hamendra Sunthwal Sep 21 '20 at 12:26
  • no it"s in my local now , so if i want to share the config it won't work there in prod – code 7 Sep 21 '20 at 12:29
  • Just follow the same steps in production also, it will work there. – Hamendra Sunthwal Sep 21 '20 at 12:29
0

You can create a DataPatch to update the path to both the logo and the favicon:

<?php declare(strict_types=1);

namespace VENDOR\DataPatches\Setup\Patch\Data;

use Magento\Config\Model\ResourceModel\Config;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Store\Model\ScopeInterface;

class UpdateFavicon implements DataPatchInterface
{
    /**
     * @var Config
     */
    protected $config;

    public function __construct(
        Config $config
    ) {
        $this->config = $config;
    }

    public function apply()
    {
        $config = $this->config;
        $path = 'PATHTOFILE';
        $config->saveConfig(
            'design/head/shortcut_icon',
            $path,
            ScopeInterface::SCOPE_WEBSITES,
            0
        );
    }

    public static function getDependencies()
    {
        return [];
    }

    public function getAliases()
    {
        return [];
    }
}

Above is an example for changing the favicon. You can also do this for Logo by using design/header/logo_src.

However, in this example it is assumed there's already a logo / favicon in your server.

5
  • Thank you sir for your reply , after doing this , and setting up the path of the custom favicon there are no changes on the favicon , still get Magento's favicon!! , where is the problem here ? – code 7 Sep 21 '20 at 15:38
  • Please check if the patch has run (patch_list table) and if the data has been updated in database (core_config_data table. Did the patch run and has the data been updated? – Frank Bokdam Sep 21 '20 at 15:53
  • there is no patch_list table in my database , i have change the module version in module.xml and i have run setup:upgrade , no result !! – code 7 Sep 21 '20 at 16:23
  • Which version of Magento are you using? Datapatches (and the patch_list table) are available as of magento 2.3. However, If you are using an older version you can either update or apply the same logic in an UpdgradeData. – Frank Bokdam Sep 22 '20 at 7:30
  • yes my version is older than 2.3 , can you update for me your question to show me how it works with upgradedata – code 7 Sep 22 '20 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.