Jump to content
kokosmin

[SOLVED] How to make UPC code validate letters?

Recommended Posts

I introduced about 60 of my products with about 7000 variations in total. Up until now all the supplier product codes had 7 numbers. But now, there are products with 5 numbers and 2 letters.

I put them always in the UPC code input area. As you know, UPC does not validate letters.

 

What can I do to make UPC validate my new product codes? I don't want to change the 7000 I already inputed to EAN-13 or something else, as this will be very time consuming.

I need the UPC field because one of my modules reads the code and displays the supplier's stock.

Share this post


Link to post
Share on other sites

For this you probably have to edit /classes/Validate.php (Make backup!!!!!)

 

Find function:

/**
* 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);
}
 
 
And change the preg_match to something like this:
 
return !$upc || preg_match('/^[0-9A-Z]{0,12}$/', $upc);
 
This will allow anything from 0-12 characters of any number and letter combination, like 1A3, 134D, 1456743, ADFHERWEDE, AD4EDF89ED, A, 4,  etc.
 
or, if you want exactly 7 characters (little extra check of correctly entered)
 
return !$upc || preg_match('/^[0-9A-Z]{7}$/', $upc);
 
 
 
You could make it more stringent, like ONLY 7 numbers or ONLY 5 Numbers+2Letters, but as it looks like you add them only yourself, this seems overkill.
 
 
Hope this helps,
pascal
  • Like 1

Share this post


Link to post
Share on other sites

This is so simple when you know what you're doing.

 

THANK YOU!

 

The solution works, UPC field validates.

There's no need to overcomplicate with the other restrictions, but it's good you put the example, because someone else's project might need it.

Share this post


Link to post
Share on other sites

Hello,

I have the same thing with EAN 13.

I want to use it as product part number.

What I must change in order to accept letters (Greek and English) numbers, dashes (-) and dots(.) and space betewwn letters?

 

Thank you very much!

 

Stavros

  • Thanks 1

Share this post


Link to post
Share on other sites

For example : Chance length UPC code to 64 and add symbols

 

1. In database change length UPC field to 64   ( ps_product UPC field varchar 12 => 64)

 

2.     /classes/Validate.php  
-
return !$upc || preg_match('/^[0-9]{0,12}$/', $upc);
+
 return !$upc || preg_match('/^[0-9a-zA-Z]{0,64}$/', $upc);

 

 

3.   /classes/Product.php  
 -
 'upc' =>                         array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
 +
 'upc' =>                         array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 64),

 

 

4. /www/admin/themes/default/template/controllers/products/informations.tpl
-
<input maxlength="12" type="text" id="upc" name="upc" value="{$product->upc|escape:html:'UTF-8'}" />
+
<input maxlength="64" type="text" id="upc" name="upc" value="{$product->upc|escape:html:'UTF-8'}" />

Share this post


Link to post
Share on other sites
On 10/31/2014 at 2:13 PM, StavrosL said:

Hello,

I have the same thing with EAN 13.

I want to use it as product part number.

What I must change in order to accept letters (Greek and English) numbers, dashes (-) and dots(.) and space betewwn letters?

 

Thank you very much!

 

Stavros

 

Hello,

I have the same problem, did anyone solve it? Y tried to change the Validate.php, but not all the codes get saved.

Edited by DOKI4ever (see edit history)

Share this post


Link to post
Share on other sites
Posted (edited)
Le 24/04/2018 à 1:51 PM, DOKI4ever a dit :

 

Hello,

I have the same problem, did anyone solve it? Y tried to change the Validate.php, but not all the codes get saved.

works for me for space and special characters 

add in this code each characters you want to allow with \

example you want allow (   put  \(  

like 

/classes/Validate.php 

return !$upc || preg_match('/^[a-zA-Z 0-9-\(\)-]{0,64}$/', $upc);

 

/www/admin/themes/default/template/controllers/products/informations.tpl

<input maxlength="64" type="text" id="upc" name="upc" value="{$product->upc|escape:'html':'UTF-8'}" />

/public_html/themes/default-bootstrap/product.tpl 

insert wher your want to see UPC

<div id="product_upc"{if empty($product->upc) || !$product->upc} style="display: none;"{/if}>
				<label> <i class="fas fa-industry" aria-hidden="true"></I> {l s='UPC:'} </label>
				<span class="editable" itemprop="upc"{if !empty($product->upc) && $product->upc} content="{$product->upc}"{/if}>{if !isset($groups)}{$product->upc|escape:'html':'UTF-8'}{/if}</span>


 

 

Edited by jeromecollection (see edit history)

Share this post


Link to post
Share on other sites

hello

I would like to remove the validation of the UPC field of a shop Prestashop1.7
can you tell me what are the files to edit? Thank you

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More