Jump to content

[SOLVED] How to make UPC code validate letters?


kokosmin

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.

Link to comment
Share on other sites

  • 2 weeks later...

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
Link to comment
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.

Link to comment
Share on other sites

  • 3 weeks later...

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
Link to comment
Share on other sites

  • 1 year later...
  • 2 years later...

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'}" />

Link to comment
Share on other sites

  • 1 month later...
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)
Link to comment
Share on other sites

  • 1 year later...
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)
Link to comment
Share on other sites

  • 2 months later...
  • 3 months later...
  • 1 year later...
On 10/6/2019 at 1:34 PM, richo said:

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

 

On 1/17/2020 at 6:09 PM, giraldillo said:

I'm interested on it too. Could you help us? Regards

src/PrestaShopBundle/Form/Admin/Product

           ->add('upc', FormType\TextType::class, [
                'required' => false,
                'label' => $this->translator->trans('UPC barcode', [], 'Admin.Catalog.Feature'),
                'constraints' => [
<!--                    new Assert\Regex('/^[0-9]{0,12}$/'), -->
                        new Assert\Regex('/^[a-zA-Z 0-9]{0,64}$/'),
                ],
                'empty_data' => '',

src/PrestaShopBundle/Form/Admin/Product

           ->add('upc', FormType\TextType::class, [
                'required' => false,
                'label' => $this->translator->trans('UPC barcode', [], 'Admin.Catalog.Feature'),
                'constraints' => [
<!--                    new Assert\Regex('/^[0-9]{0,12}$/'), -->
                        new Assert\Regex('/^[a-zA-Z 0-9]{0,64}$/'),
                ],
                'empty_data' => '',

and

 

classes/Validate.php

    public static function isUpc($upc)
    {
<--     return !$upc || preg_match('/^[0-9]{0,12}$/', $upc); -->
		return !$upc || preg_match('/^[a-zA-Z 0-9-\(\)-]{0,64}$/', $upc);
    }


 

Link to comment
Share on other sites

  • 2 years later...

To rename the references

You can change below script in product-details.tpl to separately rename the references.

 

  {* if product have specific references, a table will be added to product details section *}
  {block name='product_specific_references'}
    {if !empty($product.specific_references)}
      <section class="product-features">
        <p class="h6">{l s='Specific References' d='Shop.Theme.Catalog'}</p>
          <dl class="data-sheet">
            {foreach from=$product.specific_references item=reference key=key}
              
             {if $key == 'upc'}
                <dt class="name">{'Barcode for Case'}</dt>
              {elseif $key == 'mpn'}    
                 <dt class="name">{'Barcode for Pack'}</dt>
              {elseif $key == 'ean13'}    
                 <dt class="name">{'Barcode for Each'}</dt>
              {else}
                <dt class="name">{$key}</dt>
              {/if}
              <dd class="value">{$reference}</dd>
            {/foreach}
          </dl>
      </section>
    {/if}

Link to comment
Share on other sites

  • 4 months later...
  • 4 months later...

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...