strife's devLog

prestashop-logoHow to Add a New Custom Field in Prestashop

I’ve figured out how to add new field to database using Prestashop (1.4.3, but I think for the newer versions would be the same). This solution gives you new field in database and you will be able to edit / save data to this field in your admin panel, for my example I needed a field which was called “number of photos”. Below you can see how it works.

When you want to get it you should make some improvements. First of all just create a new field in your database it will be called “nb_photos” in my case.

And then:

1. Open AdminProducts.php file (/[admin_panel]/tabs/AdminProducts.php) and add this line (line 2194):

2. Open Product.php (classes/Product.php) and add:

Line 130

Line 231

Line 340

And that’s all.

If you need some more information about it just let me know using comments box :)

Share

30 ResponsesLeave one →

  1. Miguel Rojas

     /  March 16, 2012

    hello, great job !!!

    any question… for modify ?????

    Reply
  2. Miguel Rojas

     /  March 16, 2012

    but if field is a text field ???
    ‘my_new_field’ => ‘isUnsignedInt’, // this field ?????
    ‘my_new_field’ => ‘isText’ OR I NEED Tools:GetValue() ????????

    Reply
  3. Giacomo

     /  April 2, 2012

    Thank a lot for your guide!

    and to display this value in front office?

    Reply
    • Hi,

      I think you’ve already found the solution for it but let me anwer (maybe somebody else will need this information).

      In frontend you should use something like that:

      product.tpl

      Let me know if it’s working or if you have any questions.

      Thanks!

      Reply
  4. sepahan

     /  April 10, 2012

    how to show this field in fo????

    Reply
  5. pradyuman

     /  April 20, 2012

    Thank You very much…
    I have used your instruction and it is working fine.

    Reply
  6. pradyuman

     /  April 24, 2012

    Now I have just inserted a field as wholesaler price at admin panel and that is inserted at product table but how the wholesaler price will go to product page and how when a wholesaler will login at that time how he/she can buy their product with their wholesaler price not the retailer price.
    I have already tried for grouping but grouping is not fullfilling my requirements then how will I do?
    Please help me asap…I expect help from you.

    Reply
    • I assume the issue is after getting a payment I am right? You want to put the new price instead of old one? :>

      The ugly solution for this is … to make changes in the model. You can simply go to the model and put the condition if person is a wholesaler and then return the appropriate price.

      Does it make sense?
      Let me know if it’s still ready to solve :)

      Best,
      M

      Reply
  7. so how to make new field for ‘dollar price’, i use prestashop v.1.3.6.
    please help me

    thanks before

    Reply
    • Hi,

      That’s pretty simple if I understand well what you’ve said. What you’re gonna do is go to the product details and click to the “Prices” tab. And then you’ll see what you wanna get.

      However, I am not sure if this is what you wanted. In other words it’s not necessary to make a new field for this dollar price coz you can configure it already (All products can have a specific price for each currency).

      How I see that

      Even though, if you really want to create a new field you can just do it in the way that I wrote and print it out in the frontend this way (http://strife.pl/2012/06/how-to-change-the-original-price-in-prestashop/).

      Let me know if this helps or not :)

      Best,
      M

      Reply
  8. VERO20

     /  July 31, 2012

    Hello! how would you do that for text?

    Reply
    • Hi VERO20,

      You can do it by the same token as you’re doing it for integer. Check the whole Product.php and try to find any text fields and copy it. If you had problems let me know.

      Best,
      M

      Reply
  9. zounars

     /  August 7, 2012

    Hello,
    Thank you for your tutorial.
    How can I add a datepicker to the custom field?
    Thanks

    Reply
  10. Julius

     /  August 11, 2012

    zounars

    Try to use jquery ui datepicker. It is very easy to integrate.

    Reply
  11. antony

     /  August 27, 2012

    add custom field in Combination tab

    Reply
  12. arnold

     /  September 5, 2012

    Hello,

    I tried your tutorial, the only difference is that custom field is an ENUM just like condition, with Prestashop 1.4.9 i’m getting:
    An error occurred while updating object. product (), did anyone encounter this?

    Reply
    • Hi arnold,

      That’s interesting. Thanks for this spot. Do you have any more errors occurred? It’s quite less information to help you without working on your version …

      M

      Reply
  13. Hernan

     /  September 5, 2012

    Hello,

    where insertion is performed on data, ie , the data insert.
    in what php document or is in the same product.php

    Reply
  14. Hello every one, I am trying to integrate a date picker in order-carrier.php
    But don’t it don’t seems to work. It is as if the css is not taken into consideration.
    Any help will be welcome.
    Thank you

    Reply
  15. Lucas

     /  September 17, 2012

    Hi,

    Im in big need of adding a text field to the check out-process. I need them to write a number (for identification) and I need to receive it.

    Can I use any of your code for this?

    Thanks.

    Reply
  16. strife

     /  December 6, 2012

    Sorry for late reply.
    Yes sure you can do whatever you do with this.

    Best,
    M

    Reply
  17. RBucci

     /  December 12, 2012

    Strife,

    I’m really grateful for your tutorial, however taking a closer look, I was wondering if you could give me a hand with what I need to do;

    I’m creating a cart for a company that doesn’t have clients but employees. The employees are salesmen that will be connected to the website via a tablet each time they visit a client in order to send the order to the main office. The problem is that they need to be able to have the main price as well as a blank field in which they can input the price with which they agreed on with the final customer.

    I’m in need to enable a blank textfield (modifiable by the employees) under the price in all the products pages before the “Add to Cart” button so that afterwards, in the Shopping Cart Summary it will behave like the normal price (adding the total to the bottom) and storing it as a record for the employees to keep track of their comissions in the order history. And if possible, including those prices in the confirmation email.

    If there’s any way to do this, I will be able to sleep at night once again.. I’ve been trying very hard to do this but so far I got nowhere near..

    Thank you so much for your time!!!

    Reply
  18. Hi RBucci,

    Ok. So I can suggest you to modify the model in the Prestashop. What you need to do is:

    Find the model for products and modify it in the way that enables you to affect to the price. I think you can use sessions because each of employers would put the price manually.

    I mean something like this:

    < ?php
    function getPrice() {
    // and here you can check if something is in the sessions
    // and replace it instead of actual product price
    }
    ?>

    I don’t know if you want to replace the current price or just only add the new field above/below it. However, I understand that you want to replace it. And this solution should fix your problem. If not let me know.

    Any questions feel free to ask.

    Best,
    Maciej

    Reply
    • RBucci

       /  December 12, 2012

      I really don’t understand what you mean with that code, Maciej. Maybe you could exemplify?

      Thanks!!

      Reply
  19. carlos soratto

     /  February 8, 2013

    PERFECT! THANKS!

    Reply
  20. Bruna

     /  February 27, 2013

    Congratulation for your tutorial, it work very well!

    You save my day Strife, thanks a lot!

    (VERO20) FOR TEXT:

    Instead of adding this:

    ‘nb_photos’ => ‘isUnsignedInt’, // this field

    add this:

    ‘nb_photos’ => ‘isString’, // this field

    and on the first step you need to alter the data type(INT for VARCHAR(100) or char(50))
    ALTER TABLE ps_product ADD nb_photos VARCHAR(100) UNSIGNED NOT NULL DEFAULT ’0′

    TKS

    Reply
  21. Bruna

     /  March 4, 2013

    Hi how can i add fields in a module tpl?

    Thanks

    Reply
  22. muhsin

     /  April 4, 2013

    Hai
    i am new to prestashop and now i am creating custom API for a mobile app.
    i want a custom sql query to get cart and cart products of a loged customer.

    Reply

Leave a Reply