Add code

Wednesday 27 January 2016

Magento Static Blocks – The Definitive Guide


Magento Static Block:

There is more than one way to skin a hippo and adding CMS static blocks in Magento is no exception.
In case you are unfamiliar with CMS static blocks, they are powerful little buggers in Magento’s admin that allows the site is the administrator to add and control chunks of HTML that can be displayed throughout the site. They are perfect for seasonal banners, sale blocks, return policies, size charts, and anything that would make sense to modularize to make maintaining your site easier.
But wait, aren’t there already ‘callouts’ in Magento? Well, if you’re talking about those annoying graphics of the dog and chalkboard that take editing multiple files to update then yes. Magento’s built-in callouts are a terrible way of handling regularly updated content.
Your Magento website should be as updatable as possible to keep you from getting phone calls every time a client wants to advertise a new sale. Which is exactly why we want to control these blocks from the admin. Keep in mind Magento’s upcoming release of 1.4 will be implementing a WYSIWIG editor so clients can handle their own changes instead of pestering you.

Creating a Static Block

  1. Log into your Magento store’s admin
  2. Navigate to CMS>Static Blocks
  3. Click Add New Block in the top right corner
  4. Give your block a recognizable Block Title such as Social Media Links or “Fall Sale Banner”
  5. Give your block an Identifier which will be used to call the block. Make sure the Identifier is all lowercase and separated by underscores to follow Magento’s nomenclature i.e. your_block_id
  6. Choose what store view the block belongs to. Just leave as All Store Views unless you have a good reason not to
  7. Set Status to Enabled
  8. Enter your HTML in the Content field. The editor is currently a raw HTML editor, but 1.4 will support a WYSIWIG editor. Alternately, there is a Magento WYSIWIG extension to help out.
  9. Click Save Block or Save and Continue Edit to save your settings.
You’ve set up your block, so how do you plug it into your site? Well it depends on how you need it to function, but you have several options at your disposal:


1. XML 

Adding a static block to a page template is a great way to control global elements of your site, such as footer links, custom callouts in the sidebar (ultimately replacing that damn dog) and more. You can embed this code in app > design > frontend > default > your_theme > layout. Open the appropriate the file, lets say catalog.xml and plunk the following code in the block for our category view:
<block type="cms/block" name="your_block_id" before="-">
      <action method="setBlockId"><block_id>your_block_id</block_id>
      </action>
</block>

This code will place the block “your_block_id” that you have created in the admin above the content on the category pages (notice the before=”-“ attribute, which makes sure your block gets displayed before the rest of the content). This is perfect for a seasonal banner that could advertise a current sale on all product listings.
Controlling static blocks with XML is geared for content that will remain in a consistent position in your theme.
Sometimes however you gotta get down and dirty and place your CMS static block inline in your template. That’s where the next method comes in.

2. PHP 

Adding your static block inline with PHP is the quickest way to get your block in your template. Let’s say you want to add a quick blurb about your return policy right after the “Add to Cart” button. The client needs to be able to occassionaly update this blurb from time to keep it current. So you open your template file that contains the “Add to Cart” button app > design > frontend > default > your_theme > template > catalog > product > view > addtocart.phtml. Find the <button> tag and right afterwards add the following code:
[cc lang="php" tab_size="2" lines="40"]
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your_block_id')->toHtml(); ?>
[/cc]

This code will add the block “your_block_id” right after the button. Jobs done. This method is perfect for getting into those nooks and crannies in Magento’s vast and awkward file structure.

3. Shortcode 

This method is used when you need to pull in a static block while in Magento’s admin creating CMS pages or other static blocks. A possible example would be injecting contact information into multiple CMS pages. So you create a contact static block, and then can insert the contact info on the contact us page, your privacy policy page, customer service page, etc. If the contact info changes, you simply update the static block and the changes will be reflected across all your CMS pages.
{{block type="cms/block" block_id="your_block_id"}}
This code will place the block “your_block_id” inline in your CMS page.

Conclusion 

The whole idea of creating these static blocks is to streamline the amount of time it takes to update your site. Clients won’t have to bother you to change their 800 number. Your design team or site administer can simply FTP a new image and update the image path. Or if you own the site, you don’t have to go dumpster diving through your template files to find where you put that couple paragraphs of content.

Saturday 23 January 2016

What are the product types in Magento ?

Magento Simple products :

The most used product type for the Magento store is Simple products. It is the most common product type of them all. A Magento simple product should be used to show a single item without any specific selectable differences.

Magento grouped products :

A Magento grouped products should be used for a combination of Magento's simple products. Think about a coffee cup that is sold together with a saucer, a silver spoon, a breakfast plate or whatever. You can’t define a specific price for a Magento grouped product but you can define a discount amount. 

Magento configurable products :

These products should be used for a single item with a specific selectable variation. Check once about a teacup available in different colours and sizes, a woman’s handbag available in different materials, a light boll available in different watts, etc. Each selectable difference can have its own additional costs based on the available features. 

Magento virtual products :

These products are used for a virtual (not touchable) item. Consider insurance, a booking or a reservation, an extra product guarantee, etc. A virtual product does not allow you to select a shipping method at checkout simply because there is nothing to be shipped. 

Magento bundle products :

It is used for a bundle of simple (or virtual) products that are not to be sold separately. Check for a laptop where the customers can choose various items such as RAM, hard disk, processor, internal memory, or whatever. Each of these items are simple (or virtual) products but these can only be sold within the bundle products. 

Magento downloadable products :

It is used for online software items. Consider an mp3 file, a PowerPoint presentation, a Magento extension, or whatever theme. A downloadable product does not allow you to select a shipping method at the checkout step simply because there is no option and no need to ship.

How to remove the customer's middle name (initial/name) from the Magento checkout page ?

Here, I explain about the Magento version 1.9.2.1.

Go to System -> Configuration -> Customers -> Customer configuration ->
 Name and Address Options Show Middle Name (initial) = No

 
 
remove middle name image

Magento - Proceed to Checkout button location in theme

Open this path in your root directory:

public_html/app/design/frontend/base/default/template/checkout 
/onepage/link.phtml

Here, you can find the following code:


<?php if ($this->isPossibleOnepageCheckout()):?>
    <button type="button" title="<?php echo Mage::helper('core')->
 quoteEscape($this->__('Proceed to Checkout')) ?>
   " class="button btn-proceed-checkout ban-checkout 
<?php if ($this->isDisabled()):?> 
    no-checkout
<?php endif; ?>"
<?php if ($this->isDisabled()):?> 
     disabled="disabled"
<?php endif; ?> 
    onclick="window.location='<?php echo $this->getCheckoutUrl() ?>';">  
      <span>
        <span>
          <?php echo $this->__('Proceed to Checkout') ?>
        </span>
      </span>
  </button>
<?php endif?>
 

What are “magic methods” in Magento?

Magento uses :

__call(), __get(), __set(), __uns(), __has(), __isset(), __toString(), __construct(), etc. magic methods.

You can find more details inside class Varien_Object
For more information about magic methods:


http://php.net/manual/en/language.oop5.magic.php

How to run Custom Mysql Query in Magento?

 Magento uses Zend framework in very standard manner . It uses singleton methodology to access database.Like many other standard frameworks Magento also uses the concept of ORM (Object Relation Model) approach. To access the database Magento uses resource model where each magento model is divided into two categories
1. Active Record type
2. EAV (Entity Attribute Value) Model
With this resource model we can manipulate the data from our database.
But still if we want to run our own custom mysql query Magento provides a way to do this also. Here are the way to do


Because we need to read the data then we will get connection for core_read.
Suppose you need to insert some data into database then you need to take core_write.

$db = Mage::getSingleton('core/resource')->getConnection('core_read');
//Write your mysql query here
$result = $db->query("SELECT * FROM customerhistory WHERE customer_id = ".$customer->getId()." AND status <> 0 ORDER BY created_time DESC LIMIT 0,100");
//After running check whether data is available or not
if(!$result) {
echo 'No data found';
}
else
{
//Here we are fetching the data
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
echo $row["content"];
}
}

What Are Blocks in Magento?

In the Magento architecture, a "Block" is one of the first class elements in the structure of Magento layouts. Every page in Magento is decorated by the "Layouts" file and the content is filled up by the "Blocks" of the different modules. Magento blocks are a really powerful and flexible way to plug­ your content into the already existing layouts. On the other hand, you could also use layout XML files to easily remove or Reposition any existing blocks.
We could say that "Structural Blocks" are the containers holding the "Content Blocks". Structural blocks themselves don't have any actual content to display, but they in turn display the content blocks at the end. As the name suggests, they're used to structure the content of the whole page.
For example, "Header", "Footer", "Left" and "Right" are structural blocks in Magento. "Content blocks" are assigned to the different structural positions of the layout of the Magento page, which in turn displays the actual content of the content blocks.
"Content Blocks" are the real fire power, generating the actual content for the display. As we've just discussed in the previous section, you need to assign the content block to one of the structural blocks for the front­-end display. There are some other ways which allow us to insert the content blocks using short codes, but we'll see more on that later.
Content blocks can be of any form, from a simple static content block to a list of the most recent products on the home page! In fact, the majority of the content is generated by the content blocks spread all over the different modules in Magento.
We're going to develop a very basic custom Magento module for the development of our custom block. Our custom block will be a simple block displaying the most recent products available in the store.
I assume that you're familiar with the basic structure and conventions of the Magento module files. Now, let's see the file structure we'll need to implement for our custom block.
  • app/etc/modules/Envato_All.xml: It's a file used to enable our custom module.
  • app/code/local/Envato/Recentproducts/etc/config.xml: It's a module configuration file.
  • app/code/local/Envato/Recentproducts/Model/Recentproducts.php: It's a model file which provides the array of most recent products.
  • app/code/local/Envato/Recentproducts/Block/Recentproducts.php: It's the main block file for our custom block.
  • app/design/frontend/default/default/template/recentproducts/recentproducts.phtml: It's a template file which contains the XHTML-related stuff.

How To Add Lightbox To Magento Theme.

Steps To Add Lightbox To Magento Theme (Prerequisites)

Following are the steps to add Lightbox to your Magento Theme

Download Lightbox from here :

 Make a directory called /lightbox under /skin/frontend/default/default/js/ and copy the entire lighbox code under that directory. Once done, your directory will look like /skin/frontend/default/default/js/lightbox (all code under this directory)
    copy the lightbox.js under /magento/js/lightbox (create a folder under js directory and name it lightbox). If you installation is under root directory then copy it under /root/js/lightbox
    Now, you should copy the lightbox.css to /skin/frontend/default/default/css directory.
    Create a folder called lightbox under /skin/frontend/default/default/images. Your directories for images will finally look like /skin/frontend/default/default/images/lightbox. Copy all the images from source lightbox directory here.

Wednesday 13 January 2016

Download Magento files.

Magento Community Edition : 

Here you can get the all data about Magento download with latest versions, Release archieve and You can learn how to get starts with magento.

https://www.magentocommerce.com/download


Hope you got.

Accordion tabs in magento For layered navigation

Hi ,
In this blog I’m going to explain you how to create accordion tabs in magento for layered navigation as displayed below for attributes.


Just navigate to your layered navigation file as shown:

app/design/frontend/default/{your folder}/template/catelog/layer/view.phtml


Then open that that file & paste the following jquery script at the end of the page:



<script>var $j= jQuery.noConflict();// no conflict method
$j (document).ready(function(){
$j("#narrow-by-list > dt").click(function(){
if(false == $j(this).next().is(':visible')) {
$j('#narrow-by-list dd').slideUp(300);
}
$j(this).next().slideToggle(300);
});
$j('#narrow-by-list dd').hide();
$j('#narrow-by-list dd:eq(0)').show();
});
</script>
 
 
NOTE: 
 
#narrow-by-list is the id used in that file( <dl id=”narrow-by-list“>).
 If you used different id’s, replace this with your id and save the 
file. Refresh the cache and reload the browser to see the results. Use 
no-conflict method in magento. It is a good practice to avoid javascript
 conflicts in the page.

Magento install error - Exception printing is disabled

Here is a known error which can occur when installing Magento:

There has been an error processing your request
Exception printing is disabled by default for security reasons.
Error log record number: XXXXXXX

Here is the solution:
  1. Navigate to the "errors" folder.
  2. Change local.xml.sample to local.xml
  3. You should now see a new list of crazy errors all over the Magento page - this is okay.
  4. Open magento/lib/Zend/Cache/Backend/File.php and look for:  
     protected $_options = array(
    'cache_dir' => 'null',   
  5. Change it to: 
     protected $_options = array(
    'cache_dir' => 'tmp/',  
  6. Save it.
  7. Now the final step is to go create a tmp folder in the root Magento folder.
  8. That's it.

Magento Files and Folders Structure

Learn more about the different files and folders Magento needs to operate :

This part of the Magento tutorial will provide detailed information regarding the Magento's default files and folders structure.

The files and folders included in the main directory are as follows:
  • .htaccess - contains mod_rewrite rules, which are essential for the Search Engine Friendly URLs. There you can also find standard web server and php directives that can improve your web site performance;
  • .htaccess.sample - this is a backup of the .htaccess file. If you modify .htaccess it can be used in order to get the default settings;
  • 404 (directory) - The folder stores the default 404 template and skin for Magento;
  • app (directory) - This folder contains the modules, themes, configuration and translation files. Also there are the template files for the default administrationtheme and the installation;
  • cron.php - a Cron Job should be set for this file. Executing of the file on a defined time period will ensure that the complicated Magento caching system will not affect the web site performance;
  • downloader (directory) - This is the storage of the web downloader files. They are used for the installation and upgrade of Magento through your browser;
  • favicon.ico - the default favicon for Magento. A small icon that is shown in the browser's tool bar once your web site is loaded;
  • index.php - the main index file for Magento;
  • index.php.sample - A backup of the default index file. It can be used to revert the changes in a case of a index.php modification;
  • js (directory) - Contains the pre-compiled libraries of the JavaScript code included in Magento;
  • lib (directory) - The Magento core code is located in this folder. It contains the software's PHP libraries;
  • LICENSE_AFL.txt - The Academic Free License under which the Magento software is distributed;
  • LICENSE.txt - The Open Software License under which the Magento software is distributed;
  • media (directory) - This is the storage of the Magento media files - images out of the box, generated thumbnails, uploaded products images. It is also used as a container for importing images through the mass import/export tools;
  • mage (in versions older than 1.4.2.0 this tool was called pear) - The file controls the automatic update through the downloader script and SSH It handles the update of each individual Magento module;
  • php.ini.sample - This file contains sample php directives that can be used in order to modify your PHP setup. If you want to alter the default setup edit the file and then rename it to php.ini;
  • pkginfo (directory) - Contains files with information regarding the modules upgrades' changes;
  • report (directory) - This folder contains the skin of the Magento errors reports;
  • skin (directory) - There are located the themes files - images, JavaScript files, CSS files, Flash files. Also there can be found the skin files for the installation of skins and administration templates;
  • var (directory) - Cache, sessions, database backups, data exports and cached error reports can be found in this directory;
If you want to modify an existing template or set a new one you should know that the template files are separated in 3 folders : /app/design/frontend/default/YOUR_TEMPLATE_NAME/layout/ - Contains the .xml files that define which modules should be called by the template files and loaded in defined areas on the site
  • /app/design/frontend/default/YOUR_TEMPLATE_NAME/template/ - Contains files and subfolders that structure the final output for the users using the functions located in the layout/ folder
  • /skin/frontend/default/YOUR_TEMPLATE_NAME/ - Contains the CSS, images, JavaScript and Flash files related to the template

Magento : 404 error is showing admin page

Hello, Sometimes we may get the error on admin page once done with the Magento installation. In that scenario, we have to do the following: ...