Magento: Show only specific attributes in layered navigation
In Magento, if you have lots of attributes (e.g. Type, Size, Price, Color, etc.) defined for categories, all are shown in the layered navigation on the left side. This may be sometimes annoying as they may break the website design. It is possible to disable the desired attributes from displaying on frontend.
The solution is, go to:
– Magento Backend -> Catalog Tab -> Attributes -> Manage Attributes
– Click the attributes you do not want to display in layered navigation
– Select NO where it says “Use in Layered Navigation”
– Save
Now all the attributes with Use in Layered Navation as Yes will only show in frontend Shopping options.
Magento: Can’t login/add items in Chrome and IE
Problem: In Chrome and IE, user as well as admin are unable to login as well as add items to cart. In Firefox, everything works fine.
Solution: This is due to the cookie problem, not in browser but in Magento itself. In Magento, by default cookie’s lifetime is set to 3600 (1 hour). But if the end users computer time runs ahead of server’s time, cookies will not get set for magento frontend as well as backend. For example, end user’s computer time is 1 hour forward than server’s time, that means the cookie (holding user’s session id) will expire as soon as user logs in or tries to add an item.
To solve this, set cookie’s lifetime to 86400 (1 day) instead of 1 hour and everything will work as expected. You can also set cookie lifetime to 0, so that cookie will only expire when the user’s browser is closed.
Go to: Magento backend -> Sytem -> Configuration -> Web -> Session and Cookie Management
Set cookie lifetime to 86400 and save. Everything will work as expected now.
Magento: Get customer details : customer id, name, email
Magento get the logged in customer details like customer id, name, email, etc. Check if customer is already logged in or not, if they are logged in then get all the data using customer session object and use it as per the requirement.
if (Mage::getSingleton('customer/session')->isLoggedIn()) { | |
$customer = Mage::getSingleton('customer/session')->getCustomer(); | |
$customerData = Mage::getModel('customer/customer')->load($customer->getId())->getData(); | |
Mage::log($customerData); | |
} |
Output:
Array | |
( | |
[entity_id] => 1 | |
[entity_type_id] => 1 | |
[attribute_set_id] => 0 | |
[website_id] => 1 | |
[email] => john.doe@example.com | |
[group_id] => 1 | |
[increment_id] => 000000001 | |
[store_id] => 1 | |
[created_at] => 2007-08-30 23:23:13 | |
[updated_at] => 2008-08-08 12:28:24 | |
[is_active] => 1 | |
[firstname] => John | |
[lastname] => Doe | |
[password_hash] => 204948a4020ed1d0e4238db2277d5:eg | |
[prefix] => | |
[middlename] => | |
[suffix] => | |
[taxvat] => | |
[default_billing] => 274 | |
[default_shipping] => 274 | |
) |
Getting customer details from the $customerData object:
$customerID = $customerData->getId(); //entity_id can be used as id | |
$name = $customerData->getFirstname() . ' ' . $customerData->getLastname(); | |
$isActive = $customerData->getIsActive(); | |
$email = $customerData->getEmail(); | |
$incrId = $customerData->getIncrementId(); |
Similarly, we can get lastname, email, etc. ( [website_id] = getWebsiteId() )
Magento: Get and set variables in session
Suppose you want to set data in the magento session, so that you can retrieve it from other pages. Session has been very useful global variable in PHP and using it we can easily retrieve and add data to it and get it from the entire site.
Here is the code that will set / add your custom data into session.
Mage::getSingleton('core/session')->setMyCustomData('blah'); |
here, setMyCustomData can be replaced with the meaningful name as per your need. One thing to be cautious while setting session variables is to make sure it doesn’t conflict with magento’s internal variables, example setData.
Code to retrieve the session variable
$session = Mage::getSingleton('core/session', array('name' => 'frontend')); | |
echo $session->getMyCustomData(); |
Magento: Some important functions
Here I will show you some important functions/methods in Magento that will make your development easy.
Get the path of your magento page.
echo $this->getUrl('mypage'); |
Get the path of the image in your skin folder.
echo $this->getSkinUrl('images/yourimage.gif'); |
Get the product link.
echo $this->getProductData()->getProductUrl(); |
Get the product name.
echo $this->htmlEscape($this->getProductData()->getName()); |
Call a static block in .phtml file.
echo $this->getLayout()->createBlock('cms/block')->setBlockId('YOURBLOCKID')->toHtml(); |
Get Image url of current category.
echo $this->getCurrentCategory()->getImageUrl(); |
Check whether the current category is Top category.
echo $this->IsTopCategory(); |
Get description of current category.
echo $this->getCurrentCategory()->getDescription(); |
Display products list page (list.phtml).
echo $this->getProductListHtml(); |
Display CMS block page.
echo $this->getCmsBlockHtml(); |
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)