Jump to content

Changing the length of the UPC Field in Products


Recommended Posts

On the Products page, when it gives me the option to enter a UPC barcode, it limits me to 12 numbers. How do I change the length of the characters that can be entered into the UPC barcode box?


 


I went and changed the UPC length in Product.php (I changed it to 20), but it still won't let me enter more than 12 characters. Does anyone know how to make it so that you can enter more characters in the UPC text box under the products section?


Link to comment
Share on other sites

Hmm, problem is that in the database, the field is only 12 long, so you should start there. Only problem is then that any update of PrestaShop may cause problems, when they decide to alter this table. So I actually recommend you to make a special, extra field that you store in an associated table, (with fields: product_id, upc_extended or so) and use that instead. More work, but maybe better in the long run

That said, I don't think they will alter this field too often, so you can take the risk, and extra carefully backup this table before you upgrade

 

 

If you know what you're doing, and still want to change the database field size, do this:

 

1) Backup database!!!!!!!

2) Backup all your files

 

3) Go to phpMyAdmin, go to your PrestaShop database, and run this commend on the SQL tab:

ALTER TABLE `ps_product` CHANGE `upc` `upc` VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;

 

(N.B.1: 20 is the new size of the field. Change as desired)

(N.B. 2: If your tables have another prefix than ps_, change accordingly)

 

Then in the classes/Product.Php,  (Make extra backup of this file)

 

you indeed have to modify the field definition:

'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
 
 
Here you also see the validation function isUpc that checks the validity of the input. This is also limited to 12 numbers. To modify this, you have to edit file:
    classes/Validate.php: (Make extra backup of this file)
 
/**
* Check for barcode validity (UPC)
*
* @param string $upc Barcode to validate
* @return boolean Validity is ok or not
*/
public static function isUpc($upc)
{
    return !$upc || preg_match('/^[0-9]{0,12}$/', $upc);
}
 
 
Finally file to edit is :
     /<your admin folder>/themes/default/template/controllers/products/informations.tpl (Make backup!!)

 

In here is, for some strange reason hard coded, the upc field size for the input string:

<div class="form-group">
  <label class="control-label col-lg-3" for="upc">
    <span class="label-tooltip" data-toggle="tooltip"
      title="{l s='This type of product code is widely used in the United States, Canada, the United Kingdom, Australia, New Zealand and in other countries.'}">
      {$bullet_common_field} {l s='UPC barcode'}
    </span>
  </label>
  <div class="col-lg-3">
    <input maxlength="12" type="text" id="upc" name="upc" value="{$product->upc|escape:'html':'UTF-8'}" />
  </div>
</div>
 
 
I think this should do the trick.
 
Make sure you TEMPORARILY change the Advanced Parameters->Performance:
- Clear smarty cache (there is a button for this on this page, top right)
- force compilation -> ON
- Smarty Cache -> OFF
 
then reload the product input page and check if it shows afterwards on the product detail page in your shop.
(Then revert the force compilations and smarty cache back to their previous values)
 
pascal
Edited by PascalVG (see edit history)
  • Like 4
Link to comment
Share on other sites

  • 9 months later...
  • 2 weeks later...

 

 

This has helped me so much PascalVG! Thank you! Can you please tell me how to extend the upc field for combinations as well?

That's very similar to product's UPC with some differences:

1. Step 1

Instead of altering table product, let's do that with product_attribute

 

2. Step 2

Instead of changing in Product.php, now, do that with Combination.php

 

3. Step 3 

Validate::isUPC() - that's done already => move on the next step

 

4. Step 4

Instead of modifying informations.tpl, do that with combinations.tpl

		<div class="form-group">
			<label class="control-label col-lg-3" for="attribute_upc">
				{l s='UPC barcode'}
			</label>
			<div class="col-lg-3">
				<input maxlength="12" type="text" id="attribute_upc" name="attribute_upc" value="" />
			</div>
		</div>

Enjoy!

 

We develop RockPOS and we understand that, barcode (UPC or EAN) is very important to POS transaction. So highly recommend to not change that unless you have a good reason to do so.

Cheers,

Link to comment
Share on other sites

  • 4 years later...
  • 1 year later...
On 5/12/2015 at 10:35 AM, Tung at RockPOS.com said:

I'm not sure why you have to change to 20 instead of 12, as these are the standards and we should follow.

e.g. for using it as a secondary EAN field ;) Because prestashop ignores the possibility that products can have multiple ean codes, ;)  so where's the additional fields for a second or third ean? Ah yes, nowhere to be found.. so let's use upc as secondary ean field if you don't use upc ;)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...