Jump to content

How to add UPC code on product detail and the url?

Jian Luo

Recommended Posts

To add the UPC on the product page, you can simply do this:


edit file:

themes/<your theme folder>/product.tpl:


find the following code and add the red code (maybe search for product_reference on the page to locate it easily):


<h1 itemprop="name">{$product->name|escape:'html':'UTF-8'}</h1>
  <p id="product_reference"{if empty($product->reference) || !$product->reference} style="display: none;"{/if}>
    <label>{l s='Reference:'} </label>
    <span class="editable" itemprop="sku">{if !isset($groups)}{$product->reference|escape:'html':'UTF-8'}{/if}</span>
  <p id="product_upc"{if empty($product->upc) || !$product->upc} style="display: none;"{/if}>
    <label>{l s='UPC:'} </label>
    <span class="editable" itemprop="sku">{$product->upc|escape:'html':'UTF-8'}</span>
Standard the upc code is not available to add to the url (you can see the default keywords you can choose from in preferences->SEO&URLs, scroll all the way down (Friendly-URL must be turned on of course)
I'll check if it is possible to add the upc ode here. I'll com back to that.
Do you also really want to show/print the bar code? That may ned some extra coding, or maybe there's a module that does this. Not sure about that. Let me check for you.
For now,
Link to comment
Share on other sites

To put upc in the URL definition, do this:


edit, or better, override, file:  classes/Dispatcher.php   (Make backup!)


In the variable:

    * @var array List of default routes
    public $default_routes = array(

add this red line:


'product_rule' => array(
    'controller' => 'product',
    'rule' => '{category:/}{id}-{rewrite}{-:ean13}.html',
    'keywords' => array(
    'id' => array('regexp' => '[0-9]+', 'param' => 'id_product'),
    'rewrite' => array('regexp' => '[_a-zA-Z0-9\pL\pS-]*'),
    'ean13' => array('regexp' => '[0-9\pL]*'),
    'upc' => array('regexp' => '[0-9\pL]*'),
    'category' => array('regexp' => '[_a-zA-Z0-9-\pL]*'),
this will give the opportunity, and it shows in the list of possibilities, to add {-:upc} to the URL definition in Preferences->SEO&URLs (When Friendly URLs is turned on, you can edit the product-URLs and others at the bottom of the page)
Now we only have to really insert the upc code when generating the URL.
This is done in the classes.Link.php file. So again, edit, or better, override, the classes/Link.php (Make backup!)
in function:
    public function getProductLink($product, $alias = null, $category = null, $ean13 = null, $id_lang = null, $id_shop = null, $ipa = 0, $force_routes = false, $relative_protocol = false)

add this red line:


// Set available keywords
    $params = array();
    $params['id'] = $product->id;
    $params['rewrite'] = (!$alias) ? $product->getFieldByLang('link_rewrite') : $alias;
    $params['ean13'] = (!$ean13) ? $product->ean13 : $ean13;
    $params['upc'] = $product->upc;
Then go to your front page and start browsing. When you get to a product page, you might see the upc code in there (if defined for that product)
URL With UPC code and UPC code on product detail page.
Hope that is what you needed.
About a bar code generator, I found some modules on the web. I believe, but check first, that this one is free:
P.S. Always check with an SEO [spam-filter] when, how, and/or if you should add the UPC code. Especially if you have an existing shop, your current back links etc may get messed up/lost, resulting in lower ranking etc., so make sure you have a plan for that.
Edited by PascalVG (see edit history)
  • Thanks 1
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...