Wednesday, March 7, 2012

Add files to products - no extension required

Here is how you can add files to products without using an extension. It can be useful for adding specification files, pdf catalogs or any other files you want. Put all your files in one folder let's say (media/productfiles/). Now in the product description you just add a simple link to that file using the WYSIWYG editor. If you don't want to use the description field for this you can create a new attribute (let's call it 'attachment') type 'Textarea', scope 'Store view' (in case you want to have different files for different languages), 'Enable WYSIWYG' Yes, 'Allow HTML Tags on Frontend' Yes. If you want to show the link(s) to file(s) in the product list not only the product details page set 'Used in Product Listing' to 'Yes'. If not set it to 'No'. Now add your new attribute to the attribute set that you need and start editing products. Add links to your files just like described above, but use the 'Attachment' attribute instead of Description. now all you have to do is edit the app/design/frontend/{interface}/{theme}/template/catalog/product/view.phtml and add this line where ever you want the links to appear:
<?php echo $this->getProduct()->getAttachment();?>
If you want to add the links to the list of products also edit app/design/frontend/{interface}/{theme}/template/catalog/product/list.phtml and add this piece of code inside the foreach loop for the products:
<?php echo $_product->getAttachment();?>
Cheers, Marius.

8 comments:

  1. thank you, it's awsome. How to change this code, when i would like enable only the loggedn user to download the attached file?

    it's possible with your solution?

    thank you

    ReplyDelete
    Replies
    1. Hello Tiborg.
      It's not really possible. The only thing you can do is to hide the link to the attachments if the user is not logged in. But if the user has the direct link to the file he can download it.
      To hide the link, do this:
      <?php if (Mage::getSingleton('customer/session')->isLoggedIn()) {
      echo $_product->getAttachment();
      }?>

      Delete
  2. Hello dear,
    you must hide a link from a guest user when ever user signin than he see a link for hide a link use this code..

    isLoggedIn()) {
    echo $_product->getAttachment();
    }?>

    Magento Plugins

    ReplyDelete
  3. Salut,
    Foarte utile informatiile, insa n-am inteles "Add links to your files just like described above, but use the 'Attachment' attribute instead of Description"
    Cum adaug un link cu attachment in campul (arie text) al atributului.

    Multumesc,
    George

    ReplyDelete
    Replies
    1. Salut George.
      Iti uploadezi fisierele in folderul media si in attributil attachments adaugi linkul catre fisier {{media url="path/to/fille.pdf"}}

      Delete
  4. Multumesc, functioneaza :)

    ReplyDelete
  5. Salut,
    Revin cu o problema noua, scuze dar ar trebui sa fie un toping nou.
    Nu ma mai pot conecta la panoul admin; adaug user si parola si degeaba.
    Am citit diverse soluti de clean cash sau sesions din magento/var insa dupa ce am reusit sa ma conectez, problema a aparut din nou.
    Site-ul este activ de cateva luni. Pot mentiona si site-ul daca este nevoie.
    Astept un sfat, mersi.

    ReplyDelete
    Replies
    1. Nu sunt sigur care ar putea fi problema, dar mie mi s-a intamplat cand aveam un site principal si un side de demo pe acelasi domeniu, iar site-ul de demo era pe un subdomeniu al site-ului principal. Nu am gasit o solutie, alta decat sa folosesc 3 browsere diferite.

      Delete