The Uros Likar Blog Just another pointless blog site

15Sep/100

Magento – attribute option list

for a client i had to list all options of a specific attribute, i managed to get it with following code:

1
2
3
4
5
6
7
8
9
10
11
<?
$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes(); $attributeArray=array();
foreach($attributes as $a){?>
  <?if($a->getAttributeCode() == 'priloznost'){?> // "priloznosti" = attribute name
    <ul>
      <?foreach($a->getSource()->getAllOptions(false) as $option){?>
        <li><a href="<?=$this->getUrl('');?>catalogsearch/advanced/result?priloznost[]=<?=$option['value'];?>"><?=$option['label'];?></a></li>
      <?}?>			
    </ul>
  <?}?>
<?}?>
10Sep/101

Magento – Load specific category in pages using custom block

Las time i had to display a custom category in a specific way on front page, i managed to do it in this way:

First i created a custom phtml file called "custom_product_view.phtml" with this code:

1
2
3
4
5
6
<?$_productCollection=$this->getLoadedProductCollection();?>
<?if($_productCollection->count()){?>
   <?$foreach($_productCollection as $_product){?>
      .... product info .....
   <?}?>
<?}?>

then all i have to do, is to call this block in page with indentifier "home"

{{block type="catalog/product_list" category_id="9" template="custom_blocks/template.phtml"}}

i didn't tested this but i think you can also call it in layout with this call:

<block type="catalog/product_list" category_id="9" template="custom_blocks/template.phtml" />
Filed under: Uncategorized 1 Comment
26Aug/101

Magento – display banners on front page

In one project i had to display banners on front page. My client wanted to manualy add banners and assign link to them. One banner had to be visible, and others can be selected with buttons bellow them. I rather to use my own solutions than some magento modules created by community. Magento doesn't give you much choice so i used ordinary products for banners i need it.

First i created a category called "banners" or whatever... i didn't wanted to be visible like other categories, so i turned "Is active" option to off.

Then i created "banner.phtml" file in "template/custom_blocks" directory and used following code in home page located in administration under "CMS/pages"

{{block type="catalog/product_list" category_id="9" template="custom_blocks/banner.phtml"}}
// 9 is my banner category id

To display banners in banner.phtml i used this code: (products = banners)

<?$_productCollection=$this->getLoadedProductCollection();?>

in this way you get all products, and to display then you can use simple foreach:

<?foreach($_productCollection as $_product){ ... }?>

Now i have my block in wich i load the category i need and i display it on front page.

Where here is my final code (explanation is bellow):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?$_productCollection=$this->getLoadedProductCollection();?>
<?if($_productCollection->count()){?>	
<script type="text/javascript">
   document.observe("dom:loaded", function() {			
      $('link').onclick = function() { return false; };
      <?$i=0; foreach($_productCollection as $_product){ $i++;?>
         <?if($i==1){?>
            $('image').setStyle({'background':'url(<?=$this->helper('catalog/image')->init($_product, 'small_image')->resize(740, 192);?>)'});
            $('link').observe('click', function(){ location.href='<?=$_product->getData('url');?>'; });
         <?}?>
         $('show<?=$i;?>').onclick = function() { return false; };   
         $('show<?=$i;?>').observe('click', function(){
            $('image').setStyle({'background':'url(<?=$this->helper('catalog/image')->init($_product, 'small_image')->resize(740, 192);?>)'}); 
            $('link').observe('click', function(){ location.href='<?=$_product->getData('url');?>'; });
         });
      <?}?>
   });		
</script>
<div id="image">		
   <a href="#" id="link">&nbsp;</a>
   <ul>
      <?$i=0; foreach($_productCollection as $_product){ $i++;?>				
         <li><a href="#" id="show<?=$i;?>"><?=$i;?></a></li>
      <?}?>
      <li class="clear"></li>
    </ul>
</div>
<br />
<?}?>

Products i get with first line of code i display them using two foreach loops. First foreach is used to generate javascript and seccond to generate links to change banner.

HTML code from line 19 - 29:
div with id image is main holder wich displays current banner as background, first link with id "link" is used for current banner URL. Bottom UL holds links to change banners.

All images and logic is in javascript from line 3-19, all i had to do is to get image and url from each product. Line 7-10 is used to load first banner, line 12-15 is to load images wich are changed with buttons bellow.

Image you get in line 8 and 13, url you get in line 9 and 14. About URL, i don't remember if is by default, if is not you just create a new attribute with name url, set it to bi visible in procuct listing and get it with code "=$_product->getData('url');"

i used only default magento oprions: category + products + one attribute and prototype javascript ... that's all

24Aug/102

Magento – add multiple quantity to cart in product list

In Magento by default in product list each product have "add to cart" button. I wanted to have the option to add more than 1 product to cart. I found a script on the internet and with some modifications i used this:

1
2
3
4
5
6
7
<form action="<?=$this->getAddToCartUrl($_product);?>" method="post" id="product_addtocart_form_<?=$_product->getId();?>" <?if($_product->getOptions()){?> enctype="multipart/form-data"<?}?>>
   <?if(!$_product->isGrouped()){?>
      <label for="qty"><?=$this->__('Qty') ?>:</label>
      <input type="text" class="input-text qty" name="qty" id="qty" maxlength="12" value="<?echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1);?>" />
   <?}?>
   <button type="button" class="button" onclick="this.form.submit()"><?=$this->__('Add to Cart');?></button>
</form>
23Aug/102

Some Magento Mage tips

Mage is a Magento class wich can give you a lot of data you need. Here are some tips i frequently use:

Get base URL:

Mage::getBaseUrl(); ali $this->getUrl('');

Get media dir URL:

Mage::getBaseUrl('media');

Path to skin dir:

$this->getSkinUrl('images/myimage.jpg')

Check if intro page:

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms' && Mage::getSingleton('cms/page')->getIdentifier() == 'home');

Current page name:

Mage::getSingleton('cms/page')->getTitle();

No. of articles in cart:

Mage::helper('checkout/cart')->getItemsCount();

Total value of items in cart:

strip_tags($this->helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal());

Get quantity of a product in view.phtml:

<?php (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); ?>

Get all categories:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Mage::helper('catalog/category')->getStoreCategories();
$categories = Mage::helper('catalog/category')->getStoreCategories();
foreach ($categories as $_category){ 
    echo $_category->getName(); // name
    if($_category->hasChildren()){
        $_category->getId(); // category id
        foreach($_category->getChildren() as $subcategory){
            if($subcategory->getIsActive()){ // check if category is active
                echo $subcategory->getName(); // name
                echo $subcategory->getId(); // category id
            }        
        }
    }
}

Current category name:

Mage::registry('current_category')->getName();

Get info of a specific category

Mage::getModel('catalog/category')->load(12); // 12 is category id
->getUrl();
->getName();

Get info of current category

1
2
3
4
$_helper = $this->helper('catalog/output');
$_category = $this->getCurrentCategory();
 
$_category->getName(); // name of current category

Get category info in product view.phtml

1
2
$this->getProduct()->getCategory()->getName(); // category name
$this->getProduct()->getCategory()->getUrl(); // category url

There are different aproach to same solutions .... this worked for me

19Aug/100

Magento – get all images in product view

To display all images of a product i use this code:

1
2
3
4
5
6
<?$_images = Mage::getModel('catalog/product')->load($_product->getId())->getMediaGalleryImages();?>
<?if($_images){?>			
   <?$i=0; foreach($_images as $_image){ $i++;?>
      <a href="#"><img src="<?=$this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(200, 130); ?>" width="200" height="130" alt="<?=$this->htmlEscape($_image->getLabel());?>" title="<?=$this->htmlEscape($_image->getLabel());?>" /></a>				
   <?}?>
<?}?>
Tagged as: , No Comments
12Aug/100

SSH connect shortcut in Ubuntu

I use often SSH connection to my server, and it's a bit annoying to every time wrote following command into terminal:

ssh -l username -p 12000 192.189.2.12

My friend gave me a simple solution wich i posted here so i won't forget it.

In /home/myname is a hidden folder ".ssh" in wich we create a file with name "config":

In /home/myname/.ssh/config we put our hosts:

1
2
3
4
5
6
7
8
9
Host xx
Port 12000
User myname
HostName 192.168.1.110
 
Host yy
Port 12100
User myname2
HostName mydomain.com

If you like you can also put following code above all:

1
2
3
4
5
6
Host *
Protocol 2
Compression yes
FallBackToRsh yes
KeepAlive yes
TCPKeepAlive yes

Thia will apply to all hosts bellow

Now all we have to do is to write ssh xx or ssh yy to connect to one or another server.

Tagged as: , , , No Comments
12Aug/100

Magento – display custom block wherever you want it

In Magento you have allready predefined where to display left block, right block or content block. If you want do display a custom block somewhere in magento main template you can do something like this:

In my case i wanted to display a top block wich is above "content" display in 2columns-right.phtml template.

First you add a call in 2columns-right.phtml wherever you want.

<?=$this->getChildHtml('top_block');?>

"top_block" is the name of my custom block. Then you have to put in page.xml wich is in layout directory:

<block type="core/text_list" name="top_block" as="top_block"/>

Under "template" dir i made a sub dir called "custom_blocks" in wich i created "top_block.phtml" file with my script in it. Now all you have to do is to display this custom block wherever you want. In my case i wanted to display it only on intro page, so i opened page.xml and wrote following code:

<cms_index_index>
   <reference name="top_block">
      <block type="core/template" name="intro_block" template="custom_blocks/intro_block.phtml" />
   </reference>
</cms_index_index>
6Aug/100

Center thumbail images

If you don't have cropped images and you have to display them in fixed holder is a nice thing to center them vertically and horizontally.

In the past i used multiple DIVs with CSS positioning:

CSS:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.img-h {
   text-align: center; margin: 5px;
   border: 1px solid black; float: left;
}
 
.mid-1 {
   width: 200px; height: 200px;
   display: table; position: relative;
}
 
.mid-2 {
   position: static !important; display: table-cell;
   vertical-align: middle; position: absolute;
   left: 50%; top: 50%;
}
 
.inner-holder {
   position: static !important; position: relative;
   left: -50%; top: -50%
}

HTML:

1
2
3
4
5
6
7
8
9
<div class="img-h">
   <div class="mid-1">
      <div class="mid-2">
         <div class="inner-holder">
            <a href="image.jpg"><img src="thumb_image.jpg" width="145" height="110" border="0" /></a>
         </div>
      </div>
   </div>
</div>

I don't like so much elements for a simple thing like this, so now i use this solution:

CSS

1
2
3
4
5
a.img-h {display: block; width: 190px; padding: 5px;}
a.img-h span {
   display: block; height: 150px; text-indent: -9999px;
   background: no-repeat center center;
}

HTML

1
2
3
<a href="image.jpg" class="img-h">
   <span style="background-image: thumb_image.jpg;">image title</span>
</a>

i just used CSS background positioning for center the image, image title in span is for SEO purpose, wich i hid it with text-indent so the text won't cover the image. Simple and elegant soultion that works for me.

If someone comes to your page with mobile device and has CSS turned off, then the images won't be visible.

This can be solved easly. Just put the thumbnail image into span and add this css:

a.img-h span img {display: none;}
Tagged as: , , No Comments
2Aug/102

Remove buy option in Magento for just one category

One of our client wanted to remove a "add to cart" button in a specific category. I thought that must be simple, so i opened "list.phtml" where are list of products in category and "view.phtml" wich is template for product view. And now what...

I just needed a category id to hide that button, but i couldn't figure out. I didn't have much time at my disposal, so i have solved this problem using a dumb solution:

in list.phtml i used URL data to find where i em:

$url = explode('?',$this->helper('core/url')->getCurrentUrl());
<?if(substr($url[0],-17,17) == 'one-category.html') $hide = 1; else $hide = 0;?>

i had to use explode function, because url changes if you change "sort order" or "per page views".

in product view (view.phtml) i managed to get all categories IDs of current product so i wrote this code:

1
2
$category = $_product->getCategoryIds();
if(in_array('12',$category)) $hide = 1; else $hide = 0;

12 is category id wich i had to hide. In both cases i defined a variable $hide, wich i used to hide "add to cart" button.

It's a dumb solution but it works, if anyone know how to simply get ID of a current category please tell me.

Update:
i found a flaw in this solution, because if you change sort order url changes, so substr function doesn't work. I managed to correct this problem with simple explode funciton: