The header of our theme will look as shown in the following screenshot:
As you can see, there are three main sections that I will call the top header (the black line on the top), the main header (the white one), and the navigation bar.
To customize our header, open the header.phtml file located at app/design/frontend/bookstore/default/page/html and create the basic structure with the Bootstrap scaffolding. Our header file code will look as follows:
We insert the block into the reference name header.
The as="" statement is by which a template calls the block. In PHP, you will see <?php echo $this->getChildHtml('topbar_cmslinks') ?>, which indicates that it correlates to <block . . . as='topbar_cmslinks'> in local.xml.
Declaring the CMS block with a PHP statement in header.phtml
We can declare the CMS block with a PHP statement in header.phtml, as shown in the following code:
<!-- Top Bar -->
<div id="topbar">
<div class="container">
<div class="row">
<div class="col-md-8">
<?php echo $this->getChildHtml('topbar_cmslinks') ?>
</div>
</div>
</div>
</div>
To explain better what $this-> is, I quote Nick Jones's explanation:
"All Magento templates have a corresponding block instance. The $this statement exposed to us is the block instance, as if we were inside a method of the object."
The getChildHtml('topbar_cmslinks') statement loads the block defined in local.xml.
The left section is now completed.
The right part of the top header
Now let's work on the right part, the user area.
About this block, please note that we will not use the default Magento top links, but a custom piece of code that will display only the conditional links for the user.
The idea is to display the welcome message and two links, Register and Login, for the users who are not logged in, and two links, My Account and Logout links, for the users who are logged in. To do this, Magento has a function that checks the status of the user's session. We will replace the User Area text that we used before in the header code with the following condition:
Now, add a little bit of CSS to customize it a little. Insert the following CSS code in your styles.css file in the skin folder (skin/frontend/bookstore/default/css/):
The default logo image is logo.gif and is located in the images folder at skin/frontend/bookstore/default/images/logo.gif. The filename can be changed by navigating to System | Configuration | Design | Header, as shown in the following screenshot:
The following code generates the logo URL and the link to the home page:
The logo link's title attribute and the logo image's alt attribute will be filled in with the Logo Image Alt field of the configuration that you can set in the same section of the admin, as shown in the preceding screenshot.
The top cart
We want to put the cart to the right of the header. We can do this in the following two ways:
By adding a class pull-right to the cart div, but in this way we lose the responsiveness of the block (that we can fix with a media query)
By adding an empty div, col-md-4, between the logo div element and the cart div element
Therefore, we will have three boxes of four columns where the latest box, the cart div element, will be placed at the end of the row.
I will use this method because in future, we can use the intermediate div element to insert some additional information or banner in the header after the logo.
The following is the div class that contains the cart in the header.phtml file, located at app/design/frontend/bookstore/default/template/page/:
The base theme doesn't include a top cart, but we can duplicate the sidebar mini cart and place it in the header by performing the following steps:
Duplicate the sidebar.phtml file. You can find it at app/design/frontend/base/default/template/checkout/cart in your theme folder in the same path, and call it in the topcart.phtml file. Then change the code with the following simplified version of the file:
Now, you should be able to see the top cart in your theme header.
Add a little bit of CSS code to customize it. Insert the following CSS code in your styles.css file in the skin folder (skin/frontend/bookstore/default/css/):