Page 1 of 212

Malicious / Spam Search Terms in Magento Popular Search Terms

If you’ve got a fairly popular Magento store, you’ve probably got the problem of spam or malicious search terms showing up on the Popular Search Terms page. It’s ugly and you want to get rid of them once and for all, but at the same time leaving legitimate search terms performed by good-will users intact.

Of course you do. Me too. Look at this:

Magento store spam searches

So how to delete spam search terms from Popular Search Terms page?

What I’ve done is to edit the /app/design/frontend/default/your_theme/template/catalogsearch/term.phtml until it looks something like this:

		<?php
		$princessly_search_term = $this->htmlEscape($_term->getName());
		if (strpos($princessly_search_term, '%') !== false
			|| strpos($princessly_search_term, "'") !== false
			|| strpos($princessly_search_term, '`') !== false
			|| strpos($princessly_search_term, '=') !== false) {
			continue;
		}
		?>
            <li><a href="<?php echo $this->getSearchUrl($_term) ?>" style="font-size:<?php echo $_term->getRatio()*70+75 ?>%;"><?php echo $princessly_search_term ?></a></li>

The PHP function strpos() checks if a specific character is existent in the string $princessly_search_term which contains the originally raw search phrase. If it does, it’s not displayed (continue to the next phrase and check it to see if it does).

Most malicious / spam search attempts contain ‘%’, “‘”, or ‘=’ which normal users wouldn’t use in a legitimate search for your products. Now the Popular Search Terms page is a lot more clean and user friendly.

Display all product images on the product page (view.phtml)

Lots of online stores nowadays are blatantly showing all the sharp and tempting images of the product on the product page. They are all there for you and the visitors to examine one after another simply by window scrolling – no gimmicks such as cloud zooming or lightbox, etc. While technical cleverness such as cloud zooming or lightbox may seem pretty, they are hardly any more usable than the plain old way of just splashing the product images by simple HTML code.

I prefer the old way which would actually engage the visitors.

However, by default Magento has only narrowly arranged thumbnails of the product images, the visitor has to click those thumbnails to see the large version – waste of user attention and they could leave because it costs more to get what they want (to see a larger picture). What we want to do is to simply display all the product images in large versions on the product page where the visitors are instantly satisfied.

Display all product images WITHOUT watermarks

To do this, find /app/design/frontend/default/your_theme/template/catalog/product/view.phtml and add this snippet anywhere appropriate (change the bold part to your own theme path):

<div class="product_images_exhibit">
	<h2><?php echo $this->__('Product Images') ?></h2>
	<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
	<?php if($_images) {?>            
		<?php foreach($_images as $_image) {?>
			<p><img src="<?php echo $this->helper('catalog/image')->init($_product, 'image', $_image->getFile())->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->setWatermarkImageOpacity(0)->resize(848, null);?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php echo $this->htmlEscape($_image->getLabel());?>" /></p>
		<?php } ?>
	<?php } ?>
</div>

That’s it! See what I’ve done for my store: http://www.princessly.com/trumpet-a-line-v-neck-asym-drop-waist-sleeveless-straps-wedding-dress-w-court-train.html

Display all product images WITH watermarks

With “->setWatermarkImageOpacity(0)” the displayed images will not have watermarks on them. This is better for user experience but may enable image stealing. To add the watermarks, just get rid of “->setWatermarkImageOpacity(0)“:

<div class="product_images_exhibit">
	<h2><?php echo $this->__('Product Images') ?></h2>
	<?php $_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages(); ?>
	<?php if($_images) {?>            
		<?php foreach($_images as $_image) {?>
			<p><img src="<?php echo $this->helper('catalog/image')->init($_product, 'image', $_image->getFile())->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(848, null);?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php echo $this->htmlEscape($_image->getLabel());?>" /></p>
		<?php } ?>
	<?php } ?>
</div>

You would still need to configure watermarks in System -> Configuration -> Design to make this work.

Get Canonical URL of A Product in .PHTML

Canonical URL is a great way to focus SEO power. Without it, search engines would have no idea which particular page among all the similar pages are the most important. Magento, as sophisticated as it can get, does have lots of similar pages regarding the same piece of content such as a product.

When you are adding social buttons, it may not be enough to just add the button and let it detect the current page URL itself. For example, when the user clicks “Edit” of a cart item and gets to the edit page of that item, it’s almost the same as the canonical item page.

If you have social buttons on that page then chances are they won’t be useful at all because they would be promoting the edit page rather than the canonical page. Thus, we need to feed canonical URL rather than the current page URL to them.

How to get the canonical URL of a product programmatically?

It’s as simple as this:

echo $product->getUrlModel()->getUrl($product, array('_ignore_category'=>true));

Make sure you change $product to whatever you are using for the product in your PHTML template.

Resize Watermark by Percentage of the Original Product Image

In the configurations Magento only lets you set an absolute pixel value for the dimensions (width & height) of the watermark image you upload. What if any of the product images are smaller than the specified width of the watermark? An incomplete watermark will be printed on the product image. Everything but pretty.

What we want to do is to resize the watermark image on the fly proportionately to the size of the product image. For example, the width of the watermark will always be 50% of that of the product image. This way, we wouldn’t have such issues as incomplete watermarks.

I have Cloud Zoom extension installed so all I need to edit is the /app/design/frontend/default/forest_fashion/template/magento-team/cloud-zoom/catalog/product/view/media.phtml file. Not sure which file should be edited in your case but should be close.

In media.phtml, you can get the width of the original product image by this:

$_originalWidth = $this->helper('catalog/image')->init($_product, 'image')->getOriginalWidth();

And calculate watermark width on the fly by this:

function iGetWatermarkSize($_originalWidth) {
	// My watermark is 430x90. Change following values accordingly with your own.
	$_watermarkSizeWidth = .5 * $_originalWidth;
	$_watermarkSizeHeight = $_watermarkSizeWidth * 90 / 430;
	$_watermarkSize = $_watermarkSizeWidth.'x'.$_watermarkSizeHeight;
	return $_watermarkSize;
}
$_watermarkSize = iGetWatermarkSize($_originalWidth);

And then the watermarked image URL should be:

$url = $this->helper('catalog/image')->init($_product, 'image')->setWatermarkSize($_watermarkSize)

That’s it. Now $url should contain the URL to the new image with watermark resized proportionately to the width of the original image by percentage, 50%.

 

Magento: Get Original Width and Height of Product Images

Took me a while to find this but it’s actually in the Image Helper docs: http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Helper_Image.html

To get the width and height of the original product image (not resized ones, but the original ones that are uploaded when creating the product):

$this->helper('catalog/image')->init($_product, 'image')->getOriginalWidth()
$this->helper('catalog/image')->init($_product, 'image')->getOriginalHeight()

This is very useful when you need to make decisions based upon the dimensions of the original product image. For example, if the uploaded product image is smaller than 500px in width, the Zoom View would shrink to the image width; if not, the Zoom View would stay at 500px in width.

Hide and Don’t Display N/A Attributes

There will be some attributes that are not compulsory when you are creating the product. And they will be ‘N/A’ or ‘No’ on the product page if you don’t specify values for them. That doesn’t look pretty. Customers might ask why this product doesn’t have this feature? To save you from the awkwardness, we may be better off by removing those ‘N/A’ or ‘No’ attributes.

How to remove or hide ‘N/A’ attributes on Magento product pages?

It’s simple. Just open and edit this file:

/app/design/frontend/default/my_theme/template/catalog/product/view/attributes.phtml

Change the bold part default/my_theme to wherever your theme resides. If attributes.phtml is not there, copy this one there:

/app/design/frontend/base/default/template/catalog/product/view/attributes.phtml

And then open and edit it:

/app/design/frontend/default/my_theme/template/catalog/product/view/attributes.phtml

You would be greeted by something like this:

<?php foreach ($_additional as $_data): ?>
	<tr>
		<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
		<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
	</tr>
<?php endforeach; ?>

Just add a line in there (bold part):

<?php foreach ($_additional as $_data): ?>
	<?php if ($_data['value'] == 'N/A') {continue;} ?>
	<tr>
		<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
		<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
	</tr>
<?php endforeach; ?>

So if the attribute value is ‘N/A’, the foreach loop would continue to the next round instead of outputting the attribute value.
 

Don’t install too many themes to your Magento store

Magento is extremely versatile and flexible in letting the developers work with its code base. By working with it, I mean easily overwriting it, even the core files.

While it may seem rather appealing for people who want absolute control over their store so they can do whatever they want to shape the look and functionality, it may not be so when they have installed more than one themes or extensions on their Magento site.

Chances are there will be a few unpredictable problems that seem to come out of nowhere. And they are probably hard to diagnose. This is because all the themes are overriding each other. It’s totally possible.

One theme may have a module that’s not completely switched off by the other theme because the other theme doesn’t have a similar module that overrides it, thus the module of the first theme is used when the other theme is enabled.

This would incur more problems when the themes have more functionalities that need to override or extend the Magento code base, rather than just templates, layouts, or styles, like how a WordPress theme would work. A WordPress theme is rigidly restricted within one folder and there’s not much it can do to manipulate WP core logics. But that’s exactly the opposite of a Magento theme that can do a good lot to Magento. A theme / extension that you upload is able to overwrite the entire Magento installation.

I recently got such a problem and eventually solved it by the help of Jake, who created the Avalanche theme. After I emailed him about the problem and about the potential collision that I believed existed between my current theme and his theme, he got back to me with the right solution – renaming one of the folders of his theme so as to disable that module. And that’s it. The problem was immediately gone.

So remember, don’t install too many themes to your Magento store – it’ll mess things up. At least do your best to just install the necessary ones. For me, mere 2 themes are enough to create a problem that wasted me several hours looking for an answer. For those who are looking for a premium Magento theme, look no further than Jake. He’s the best. Check out his latest post about how to manage & debug Magento extensions.

Add Custom Product Tabs to Magento – Simplest Way

Some themes come with switch tabs on the product page such as the Modern theme shipped with the official Magento download (as of 1.6.2). Time from time you may wonder how to add your custom ones there with more information regarding the product. The Easy Tabs extension doesn’t actually work with themes that already come with tabs. However, with themes that don’t have tabs already, you should try it.

So how to accomplish this? Here’s a dead simple way.

Adding Custom Tabs on Product Pages

Just follow these simple steps, assuming you are using the Modern theme which is located at /app/design/frontend/default/modern:

  1. Open /app/design/frontend/default/modern/layout/catalog.xml (or whatever your theme, just change the bold part to your own theme path), and find lines with
    <action method="addTab" 

    that are basically layout directives to add tabs to the product page.

  2. Duplicate one of the “addTab” <action></action> tags and change the properties according to your needs, such as to this:
    <action method="addTab" translate="title" module="catalog"><alias>your_tab_name</alias><title>Your Tab</title><block>catalog/product_view</block><template>catalog/product/view/your_tab.phtml</template></action>
  3. Save and overwrite the original catalog.xml.
  4. Create your_tab.phtml that will contain the actual tab content when the user clicks the “Your Tab” tab on the product page. Upload it to /app/design/frontend/default/modern/template/catalog/product/view/your_tab.phtml
  5. Refresh all caches.

Now you should be seeing a new “Your Tab” on the product pages of your store. Add some text or HTML to your_tab.phtml and refresh cache so that tab will display the content you added.

Add External JavaScript / CSS to Header or Remove JavaScript / CSS from Header

In /app/design/frontend/base/default/layout/page.xml, Magento uses addJs and addCss action methods to add internally hosted JavaScript and CSS files. Your attempts to add external JS or CSS files with these methods would eventually fail because the generated URL will always start with your store’s absolute URL path such as http://www.yourstore.com/, making it impossible to include JavaScript or CSS hosted on another domain / sub-domain.

What if you want to add an externally hosted JavaScript such as one from a sub-domain CDN or one from Google’s hosted libraries? Such as prototype.js?

Add External JavaScript to Magento Header

Just edit the head.phtml located at here:

/app/design/frontend/default/your_theme/template/page/html/head.phtml

Open it and you should see the entire HTML header that Magento uses to render all pages of your store. Simply add a hard coded line of script tag anywhere appropriate:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>

Refresh the cache and you would find the external javascript hosted at Google loaded in every page of your store in the HTML header section.

Remove Default JavaScript Files in page.xml

You may also want to get rid of the same version of JavaScript (prototype.js in this case) that’s hosted local on your main site. Duplicate ones would simply mean a waste of bandwidth and unnecessary download time. Because you should always try to serve static content from a separate domain / sub-domain as CDN rather than on the same domain as your site, or it’d be slower to download all the assets for the users.

To remove a local JavaScript from your Magento theme, simply create a new .xml layout file in /app/design/frontend/default/your_theme/layout and name it whatever you want, such as custom.xml or local.xml and put the following snippet in it to remove the prototype.js loaded in the base default theme of Magento:

<?xml version="1.0"?>
<layout version="1.1.1">
	<default>
		<reference name="head">
			<action method="removeItem"><type>js</type><name>prototype/prototype.js</name></action>
		</reference>
	</default>
</layout>

Refresh your store’s cache and you should see no local prototype/prototype.js being loaded in the HTMl header any more.

Differences between {{store direct_url=…}} and {{store url=…}}

When editing the CMS pages or static blocks, one should specify URLs with either {{store url=…}} or {{store direct_url=…}} directive in the HTML you create for them, so that the base URL is correctly and automatically outputted – http:// or https://, appending suffixes, etc.

{{store url=…}}

After some tests, I found that, basically, {{store url=…}} transforms the supplied parameter into a normalized URL. For example, it appends the suffix you specified in System -> Configuration -> Catalog -> Search Engine Optimizations to the end of the URL.

{{store url=’some//weird/page.php‘}} would output:

http://www.yourstore.com/some/weird/

Should you set ‘/’ as the category suffix.

{{store direct_url=…}}

This one simpler as it output whatever you put in the parameter and doesn’t do anything to them.

{{store url=’some//weird/page.php‘}} would output:

http://www.yourstore.com/some//weird/page.php

That’s it.

Page 1 of 212