Suppose we have the customer email id and we want to fetch all details of that customer in Magento, its very easy and you can use below code it will give you all information with the email address which you have added in $email.
$customerExist = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('email', $email )
->getFirstItem();
Model-View-Controller MVC
Model-View-Controller MVC
=> Model
Model is the classes providing data, service related to data and business logic. This class works directly with data and provides data for other elements. In a module, these classes are contained in Model folder.
=> View
View is the classes which define data presented method (not update data). These classes are contained in the folder Block of module.
=> Controller
Controller is the classes which control application stream. They receive input which is requirement of users through HTTP request, transfer those requests to the classes which process the requests directly. From the link, Router will get to Controller which controls that link. In Magento module, these classes are contained in controller folder.
Check customer is login or not in magento
Magento have different session. You can check customer is login or not using customer/session.
You can check it using this piece of code:
if(!Mage::getSingleton('customer/session')->isLoggedIn()){
//not logged in
}else{
// logged in
}
Call phtml file in Magento
1) Magento: call phtml file in CMS page.
When you want to display dynamic content on home page or any other CMS page. You just need to create block of specific module which create an object of that module and call predefined methods of that module to fetch date and call that modules phtml file.
Below is code by using you can call phtml file in CMS page.
{{block type="core/template" template="templateFolder/your_template.phtml"}}
2) Magento: call phtml file in static block.
You can use same syntax as CMS page
{{block type="core/template" template="templateFolder/your_template.phtml"}}
3) Magento: call one phtml file in another phtml file.
To call any phtml fine in another phtml file you just need to add below code in phtml file.
echo $this->getLayout()->createBlock('core/template')->setTemplate('templateFolder/your_template.phtml')->toHtml();
4) Magento: call phtml file using XML file.
To call any phtml file using XML you need to add below code in reference block where you want to show that block.
i.e if you want to add phtml file in footer at that time you need to add below code in <reference name=”footer”> in page.xml
<reference name="footer">
<block type="core/template" name="unique_name" template="templateFolder/your_template.phtml"/>
</reference>
Call static block in Magento
Static block is use to allow store Admin to update content or any block without changing code. Admin can update content from admin-panel easily. We can call static block in different location in Magento. I.e. in CMS page,
any of phtml file, any block etc.
Below are syntax to call static block in different location.
1) Magento: call a static block in a phtml file.
To call a static block in a phtml file you just need to replace static block id in below code. Put this code where you want to call.
echo $this->getLayout()->createBlock('cms/block')->setBlockId('static_block_id')->toHtml();
2) Magento: call a static block in CMS page.
use below code to call static block in CMS page.
{{block type="cms/block" block_id="static_block_id"}}
3) Magento: call static block in xml.
syntex to call static block using xml file
<block type="cms/block" name="home-page-block">
<action method="setBlockId"><block_id>home-page-block</block_id></action>
</block>
Steps to activate logs in magento using admin panel
using below steps you can enable logs in magento
1) Login in magento back-end
2) Go to System -> Configuration
3) from left section click on Advanced -> Developer
4) you can able to see Log Settings in main section click on it
5) Enabled set this option ‘YES’ and click on ‘save’ button on right top corner.
Now you can able to see log files in var/log folder
Improve performance of Magento store using htaccess
Magento store have 300+ default database tables. Also Magento using EAV database structure that make magento store very slow. To improve magento store performance using htaccess. we have to do some changes in .htaccess file.
############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
# Insert filter on all content
SetOutputFilter DEFLATE
# Insert filter on selected content types only
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
# Netscape 4.x has some problems…
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Don’t compress images
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
# Make sure proxies don’t deliver the wrong content
Header append Vary User-Agent env=!dont-vary
Script to get all product magento
Below is the script which display all magento product
create new file in root of the magento source and add below code in that file
require_once('app/Mage.php');
umask(0);
Mage::app();
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSort('created_at', 'DESC')
->addAttributeToSelect('*')
->load();
foreach ($_productCollection as $_product){
echo $_product->getId().'</br>';
echo $_product->getName().'</br>';
echo $_product->getProductUrl().'</br>';
echo $_product->getPrice().'</br>';
}
Add JQuery in Magento
To add Jquery in magento we need to add below code in layout file.
/app/design/frontend/package/your theme/layout/page.xml
<reference name="head">
<block type="page/html_head" name="head" as="head">
<action method="addItem">
<type>skin_js</type>
<name>Jquery path with name</name>
</action>
</block>
</reference>
Script to get all category in magento.
External script to Display All category which are created in magento.
Below is the code to display all category.
create new file in root of the magento source and add below code in that file
require_once 'app/Mage.php';
$app = Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categoriesArray = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->load()
->toArray();
$categories = array();
foreach ($categoriesArray as $categoryId => $category) {
if (isset($category['name']) && isset($category['level'])) {
$categories[] = array(
'level' => $category['level'],
'label' => $category['name']
);
}
}
echo "<pre>";
print_r($categories);
Above code will display all categories as below screenshot: