Setting up an e-commerce website with Magento is not too complicated, the install and global configuration are pretty easy and shouldn’t cause too many problems. For more advanced configuration and template tweaks, things can get a bit more hectic, but with the following Magento tips it should already get a little easier.
1. Add a custom “add to cart” button on CMS pages
Sometimes you’ll want to show a different presentation of your products or introduce it on a CMS page for some reason. If you want to do that it’s actually not so complicated, in your page editor, just add the following HTML code where you want it to appear in the page.
[HTML]<button onclick=”location.href =’{{config path=”web/unsecure/base_url”}}/checkout/cart/add?product=1&qty=1′”>Buy It Now</button>[/HTML]
Save the page and refresh the cache. Now if you open the page in you should be able to see “Buy It Now” button. When clicked it adds 1 product with ID=1 to your shopping cart.
To make it a little more advanced, you can add the quantity of products you want to let the customers add, in that case use this code (example for five products).
[HTML]<button onclick=”location.href =’{{config path=”web/unsecure/base_url”}}/checkout/cart/add?product=1&qty=5′”>Buy 4 Get 1 Free</button>[/HTML]
2. Add custom comment box to each product on the cart page
If you create more than one website with Magento for clients, you get 90% chances that one of your clients will ask to have a comment box for products on orders. It makes sense, the client could have a specific requirement or question regarding the order or the shipping and a comment box is the best way to let him comment.
This tip is a bit long, so take a look at it on Magento Coder’s website.
3. Hide shopping cart sidebar when it is empty
Having the shopping cart in the sidebar or elsewhere on the page is very useful for your visitors. However, displaying the shopping cart when the user has nothing in it is not necessary. To hide the cart in the sidebar, try the following steps.
1. Open: app/design/frontend/default/<your template>/template/checkout/cart/sidebar.phtml
2. Look for the following div (normally on line 32):
[HTML]<div class="box base-mini mini-cart">[/HTML]
3. Add this php code right before that opening div tag
[php]$_cartQty1 = $this->getSummaryCount()
if ($_cartQty1 >0):[/php]
4. Add this php code to the bottom of the file
endif
4. Add Facebook Like on a product page in Magento
Having a Facebook button on your product is an easy way to take advantage of the power of Facebook. If a user clicks on it, he shares your page with all his friends.
To add this button, add the following code anywhere in this template file: app/design/frontend/theme/theme/template/catalog/product/view.phtml
[php]<a name=”fb_share” type=”button”></a>
<script src=”http://static.ak.fbcdn.net/connect.php/js/FB.Share” type=”text/javascript”>
</script>
<?php elseif($_share == ‘facebook_like’): ?>
<script src=”http://connect.facebook.net/en_US/all.js#xfbml=1″></script><fb:like show_faces=”true” width=”450″></fb:like>
<?php endif; ?>[/php]
5. Add a language pack as a store view
Creating a store view for a language is not too hard, but you shouldn’t have any problem doing it in Magento if you follow these instructions.
1. Download the language pack you need and then unpack it to your Magento install folder. It copies two folders: one goes to app\design\frontend\default\default\locale and the other one to app\locale.
2. Go to System > Configuration in your admin. On the left top corner in Current Configuration Scope dropdown box, you can see Default Config, and Main Store in English store view.
3. Now let’s go and add the French store view. Under the dropdown box, there’s Manage Stores link or you can directly go to System > Manage Stores. Click the Create Store View link on top right, and type these in store view information form:
Store: Main Store
Name: French
Code: french
Status: Enabled
Sort order: 0
4. Save, go back to System > Configuration. In Current Configuration Scope dropdown box, you now see there’s French store view. Click that link.
On Locale options tab on the left, uncheck the “use website” checkbox then change the locale to French (France). Save.
Now you can have your website in French.
6. Tip to hide the price of the product if Magento user is not logged in
Tier pricing is great, but you don’t want to display every price to every customer. To show tiered pricing to logged in visitors only, go to: app/design/frontend/default/default/template/catalog/product/view/tierprices.phtml
On this file, add this PHP function
[PHP]<?php if(Mage::getSingleton(‘customer/session’)->isLoggedIn()): ?>[/PHP]
The above function should be added before the following piece of code
[PHP]<?php if (count($_tierPrices) > 0): ?>[/PHP]
Then add this code at the end.
[PHP]<?php endif; ?>[/PHP]
7. Add a contact form to a Magento CMS page
Of all Magento tips, this one solve a very frequent problem. Your clients will want to get a contact form to which they can add bits of text, they’ll also want to have the breadcrumb menu shown for that contact page. Out of the box, the Magento contact form doesn’t allo this, to do it you just have to follow this procedure.
Go to your CMS> Manage Pages interface
Once there, input your HTML as you normally would on any other page
Once you are happy with HTML part, add this lines:
[HTML]<!– CONTACT FORM –>
{{block type=”core/template” name=”contactForm” form_action=”/contacts/index/post” template=”contacts/form.phtml”}}
<!– END OF CONTACT FORM –>[/HTML]
8. How to Remove Credit Card Information From Sales Email
Depending on what payment system you allow to your visitors, Magento sends the credit card number openly by email, which is not secured at all. Here is a Magento tip to avoid displaying that info in emails.
1. Open the file app/design/frontend/default/your_theme/template/payment/info/cc.phtml
2. Find the lines that read:
[PHP]<?php echo $this->__(’Credit Card Number: xxxx-%s’, $this->htmlEscape($this->getInfo()->getCcLast4())) ?><br/><?php echo $this->__(’Expiration Date: %s/%s’, $this->htmlEscape($this->getCcExpMonth()), $this->htmlEscape($this->getInfo()->getCcExpYear())) ?>[/PHP]
3. Comment this code out, so it looks like this:
[PHP]<!–<?php echo $this->__(’Credit Card Number: xxxx-%s’, $this->htmlEscape($this->getInfo()->getCcLast4())) ?><br/><?php echo $this->__(’Expiration Date: %s/%s’, $this->htmlEscape($this->getCcExpMonth()), $this->htmlEscape($this->getInfo()->getCcExpYear())) ?>–>[/PHP]
4. Save the file.
9. Debug your layout in Magento
Your layout files and templates are not showing up on front? You can see what’s going on with your layouts using Mage::log() method in your controller. The code below would log loaded layout handles and compiled layout update string into var/log/layout.log file.
Here is how to enable the debug mode in your index.php file.
[PHP]public function testAction()
{
$this->loadLayout();
// …
//Somewhere within controller action after loadLayout() was called
//The code below logs loaded layout handles to “var/log/layout.log” file
Mage::log(
$this->getLayout()->getUpdate()->getHandles(),
null,
‘layout.log’
);
//The code below logs merged layout to “var/log/layout.log” file
Mage::log(
$this->getLayout()->getUpdate()->asString(),
null,
‘layout.log’
);
}[/PHP]
10. Display a product’s category name
When on a product page in Magento, it can be a bit tricky to display the name of the category the product is in. Actually it’s not that hard, all you have to do is to follow these Magento tips.
The template file you’ll have to edit is this one catalog/product/view.phtml
and add the next code where you want to display the category
[PHP]<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel(‘catalog/category’)->load($_category_id) ?>
<a href=”<?php echo $_category->getUrl() ?>”><?php echo $_category->getName() ?></a>
<?php endforeach; ?>[/PHP]
Have any Magento tips to share?
The Magento tips mentionned in this article are only a few, but they really helped in creating my Magento sites and templates. Do you work frequently with Magento? If so, do you have any cool Magento tips you could share?