Create a Customised Magento Search Form
The Magento software is okay for searches… It has the main basic search form, which can be used to search for keywords in pretty much every attribute that a product has attached to it. Then you have the advanced search form, allowing you to pick and choose which attribute values should be searched for. Attributes in this search form again can be user defined completely. So then I hear you cry, why would I want to create my own search form?
The situation arose when a client I was working for required a search form which did a calculation based on the numeral values entered, then compared this data against attributes in the product catalog to bring back the correct results. Although writing your own code into the Magento framework can sometimes be as painful as trying free yourself from the jaws of a crocodile, the solution came to me quite quickly and easily. As the code I used was quite long and complicated, here’s the simple version.
1. I made a file called ‘quote-results.phtml’, which is then echoes on a CMS page to show the results – I called mine ‘quote-results’…. I know, creative stuff right?
2. I created a search form which would sit on the homepage, I called this file ‘quote-form.phtml’. This echoed out on the homepage CMS page. Simple.
OK, this is what it’s gonna do – told you it was simplified. The client enters a width & a height into the search form.The products hypothetically have an attribute called ‘Max Length’. Which ever is bigger, it will use this figure and bring back only the products with a greater max length than what was calculated by the search form. Practically, this would probably never be useful code, but once you have the groundwork in place you can construct entire buildings! Before I drown everyone in a succession of David Brent-style metaphors, here’s my search form code:
<form action="/quote-results" method="post"> <label for="height"> <span>Height (cm): </span> <input name="height" type="text" /> <label for="width"> <span>Width (cm): </span> <input name="width" type="text" /> </label> <button type="submit" name="quotesubmit">Submit</button>
Now that we can post the data to the quote results page where, the results will be generated by this bit of code (obviously this has no style to it):
<h1>Your Details</h1>
if (isset($_POST['width'])) { $width = $_POST['width']; }
if (isset($_POST['height'])) { $height = $_POST['height']; }
if (isset($_POST['quotesubmit'])) { $quotesubmit = $_POST['quotesubmit']; }
$postlength = 0;
if ($height > $width) { $postlength = $height; } else { $postlength = $width; }
if (isset($quotesubmit)) {
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult = $write->query("SELECT DISTINCT product_id FROM catalog_category_product ORDER BY product_id");
while ($row = $readresult->fetch() ) {
$prodid = explode(" ", $row['product_id']);
foreach ($prodid as $id) {
$_product = new Mage_Catalog_Model_Product();
$_product->load($id);
$maxlength = $_product->getAttributeText('maxlength');
if ($postlength >= $maxlength) { ?>
<p><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_product->getName(); ?>"><?php echo $_product->getName(); ?></a> - <span>£<?php echo number_format(round($_product->getPrice(), 2), 2) ?><span></p>
<?php if($_product->isSaleable()): ?>
<p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('/checkout/cart/add/product/<?php echo $_product->getId(); ?>/')">Add to Cart</button></p>
<?php else: ?>
<p><span><?php echo $this->__('Not Available') ?></span></p>
<?php endif; ?>
<?php } } ?>
That’s all there is to it. By using the Mage_Catalog_Model_Product(); script, we can load the product catalog from anywhere and have access to this data anywhere. Thanks for reading the Magento Blog at Ecommerce Web Design – look out for our new design in the next few weeks!
