Tuesday, 7 February 2012

Show an image in any .phtml file with a RELATIVE link, i.e. not hardcoded!

Thing about magento you learn pretty soon in...

...how you code stuff is VERY dependent on where you're putting the code.

This works in .phtml files. Or I guess, any files where php will be parsed. Maybe.

<img src="<?php echo $this->getSkinUrl('images/btn_trash.gif') ?>" alt="" />

Of course, the image is in your skin/frontend/<yourtemplate>/images/ folder

nice, neat way of making your site not break if you move it ;)


Monday, 6 February 2012

Another way of doing a bog standard link

Sometimes an <a> tag isn't appropriate.

You can also use:


onClick="location.href='index.html'"


inside any other tag. 

example

<img src="photo of a dog eating a cow.jpg" onClick="location.href='index.html'">

awesome. 

Sunday, 5 February 2012

Search boxes anywhere!

A quickie:

<?=$this->getLayout()->getBlock('top.search')->toHtml()?>

will put a search box anywhere you want!

 

Backing up your Magento DB. Go Take a Dump. Literally

Pun intended.

Cpanel db backup, mysql export, they're all rubbish with large databases (i.e. >10mb). I don't know why they bother.

To do it properly, you *need* ssh access to the server, or some other shell access. If you dont have it, get it. Seriously.

Once you're in the shell:

~> # mysqldump -u [your cpanel login] -p [FULL_dbname] > backup.sql


Then you have yourself a proper reliable sql file.

Remember to turn off the foreign key check before you import!

Oh no! Easy tabs messes up my product view page!

Now, this one can be a little infuriating.

After installing the awesome easy tabs plugin
http://www.magentocommerce.com/magento-connect/magento-easytabs.html

You suddenly find that any mods you've previously done to your product view page disappear.

Like say, a lightbox mod.

Oh no!

Simple answer: easy tabs uses this file:

app\design\frontend\yourtemplate\default\template\easytabs\catalogproductview.phtml

instead of what you would have been using before:

app\design\frontend\yourtemplate\default\template\catalog\product\view.phtml.


So there you go. Copy your code across.

Friday, 3 February 2012

Jquery and Magento

OK, a quick one. Magento and Jquery can work together very well, but there's a couple of things you have to do.

1) Jquery, make sure you have the latest version

2) prototype.js (part of magento) has to also be the latest version [on magento 1.6 it's v1.7 of prototype which IS the latest version. woop.]

Once you have the latest Jquery (1.6 at time of writing I think?) - open the file, and add:
jQuery.noConflict();

right down at the bottom.

I believe that the order you call scripts may be important - so if you still have gripes, try to make sure the <script> tag for jquery is ABOVE the one for prototype.

Thursday, 2 February 2012

What's My Base URL? How to use a relative path on an image or file in magento!

Simple question really. Lets say you have an image file in:

/home/you/magento/media/myimage.jpg

Now, http://www.you.com points to magento/

But we all know that absolute links like
<img src="http://www.you.com/media/myimage.jpg"> 
are naughty

so you really want to say is

<img src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);?>media/myimage.jpg">

And I just gave it away.

So here's the lowdown for an html file in magento.

Mage::getBaseUrl is the magento root folder.

Mage::getSkinUrl and Mage::getImageUrl should also work, to the skin and mediaimages folders respectively.

(note the "should" - I havent tried them yet!)

Note this only works in html files, for the scripty type files you would use something else, like
$this->getSkinUrl()
Or thats what I'm guessing.

Fun when copying magento installs to local WAMP installation

Here we go again. All I want to do is some quick mods to the skin on a magento installation, so I install WAMP locally on my PC.

I do an sql dump and as usual I create a db in WAMP and go to import.

c:\wamp\bin\mysql\mysqlx.xx.xx\bin> mysql -u root <dbname>
mysql>
mysql> source <filename.sql>;

So it's failing again. It always fails. What now?
Foreign keys. Of course. Error 150. Foreign Key Constraints.
So I paste this into the top:


SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT;
SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS;
SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION;
SET NAMES utf8;
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0;

pretty standard fare.

Still not working. Why?

Try all sorts. Still not working.

Well guess what 2 hours has taught me. Don't use source.

This is how you import a magento sql dump:

c:\wamp\bin\mysql\mysqlx.xx.xx\bin> mysql -u root [dbname] < [filename.sql]

Then it actually uses the disable foreign key thing !!!

Why it ignores it using source I don't know.

So - great, then we get this:


ERROR 2006 (HY000) at line 6027: MySQL Server has gone away.

Fantastic.


Well the solution to this is to increase the max_allowed_packet to 10M (from 1M) in my.ini (right click your WAMP icon and go to it that way)

Worked. Finally.

Hopefully this will save someone else 2 hours of work.