Mask for meta tags of products and categories in OpenCart


For better SEO optimization of products and their categories, sometimes you just need to make a mask so that everything at once has something at the start of the promotion, and then promote a particular product or category, changing tags under requests. The following describes how to quickly make a mask for products and categories.

This method has been tested on OpenCart 2.3.0.2. Work on other versions is not 100% guaranteed and may require modifications.
All changes in this example will writing in module with the standard ocmod mechanism. You can make all changes directly in the engine files, then as you prefer.

Create a module file, as an example, meta_teg_mask.ocmod.xml, which will be located in the /system/ folder. It is in this file will make all changes to the engine.

<?xml version="1.0" encoding="utf-8"?>
<modification>
<name>Mask meta tags for categories and products</name>
<code>Mask meta tags for categories and products</code>
<version>2.3.0.2</version>
<date>15.03.2018</date>
<author>Bobroed</author>
<link>https://en.bobroed.ru</link>


</modification>

1. Create special fields in the admin area, in which we will register our mask.

<file path="admin/language/en-gb/setting/setting.php">
    <operation>
        <search>
        <![CDATA[
            $_['entry_comment']                   = 'Comment';
		]]>      
        </search>
		<add position="after">
        <![CDATA[
            $_['entry_mask_meta_title_category']       = 'Mask title for category';
            $_['entry_mask_meta_description_category'] = 'Mask description for category';
            $_['entry_mask_meta_keyword_category']     = 'Mask keyword for category';

            $_['entry_mask_meta_title_product']        = 'Mask title for product';
            $_['entry_mask_meta_description_product']  = 'Mask description for product';
            $_['entry_mask_meta_keyword_product']      = 'Mask keyword for product';
		]]>      
        </add>
    </operation>
</file>

<file path="admin/controller/setting/setting.php">
    <operation>
        <search>
        <![CDATA[
            $data['entry_comment'] = $this->language->get('entry_comment');
		]]>      
        </search>
		<add position="after">
        <![CDATA[
            $data['entry_mask_meta_title_category']       = $this->language->get('entry_mask_meta_title_category');
            $data['entry_mask_meta_description_category'] = $this->language->get('entry_mask_meta_description_category');
            $data['entry_mask_meta_keyword_category']     = $this->language->get('entry_mask_meta_keyword_category');

            $data['entry_mask_meta_title_product']        = $this->language->get('entry_mask_meta_title_product');
            $data['entry_mask_meta_description_product']  = $this->language->get('entry_mask_meta_description_product');
            $data['entry_mask_meta_keyword_product']      = $this->language->get('entry_mask_meta_keyword_product');
		]]>      
        </add>
    </operation>

    <operation>
        <search>
        <![CDATA[
            if (isset($this->request->post['config_comment'])) {
		]]>      
        </search>
		<add position="before">
        <![CDATA[
            if(isset($this->request->post['config_mask_meta_title_category'])){
    			$data['config_mask_meta_title_category'] = $this->request->post['config_mask_meta_title_category'];
    		}else{
    			$data['config_mask_meta_title_category'] = $this->config->get('config_mask_meta_title_category');
    		}

            if(isset($this->request->post['config_mask_meta_description_category'])){
    			$data['config_mask_meta_description_category'] = $this->request->post['config_mask_meta_description_category'];
    		}else{
    			$data['config_mask_meta_description_category'] = $this->config->get('config_mask_meta_description_category');
    		}

            if(isset($this->request->post['config_mask_meta_keyword_category'])){
    			$data['config_mask_meta_keyword_category'] = $this->request->post['config_mask_meta_keyword_category'];
    		}else{
    			$data['config_mask_meta_keyword_category'] = $this->config->get('config_mask_meta_keyword_category');
    		}

            if(isset($this->request->post['config_mask_meta_title_product'])){
    			$data['config_mask_meta_title_product'] = $this->request->post['config_mask_meta_title_product'];
    		}else{
    			$data['config_mask_meta_title_product'] = $this->config->get('config_mask_meta_title_product');
    		}

            if(isset($this->request->post['config_mask_meta_description_product'])){
                $data['config_mask_meta_description_product'] = $this->request->post['config_mask_meta_description_product'];
            }else{
                $data['config_mask_meta_description_product'] = $this->config->get('config_mask_meta_description_product');
            }

            if(isset($this->request->post['config_mask_meta_keyword_product'])){
                $data['config_mask_meta_keyword_product'] = $this->request->post['config_mask_meta_keyword_product'];
            }else{
                $data['config_mask_meta_keyword_product'] = $this->config->get('config_mask_meta_keyword_product');
            }
		]]>      
        </add>
    </operation>
</file>

<file path="admin/view/template/setting/setting.tpl">
    <operation>
        <search>
        <![CDATA[
            <textarea name="config_comment" rows="5" placeholder="<?php echo $entry_comment; ?>" id="input-comment" class="form-control"><?php echo $config_comment; ?></textarea>
		]]>      
        </search>
		<add position="after" offset="2">
        <![CDATA[
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_title_category"><?php echo $entry_mask_meta_title_category; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_title_category" rows="5" placeholder="<?php echo $entry_mask_meta_title_category; ?>" id="input-mask_meta_title_category" class="form-control"><?php echo $config_mask_meta_title_category; ?></textarea>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_description_category"><?php echo $entry_mask_meta_description_category; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_description_category" rows="5" placeholder="<?php echo $entry_mask_meta_description_category; ?>" id="input-mask_meta_description_category" class="form-control"><?php echo $config_mask_meta_description_category; ?></textarea>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_keyword_category"><?php echo $entry_mask_meta_keyword_category; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_keyword_category" rows="5" placeholder="<?php echo $entry_mask_meta_keyword_category; ?>" id="input-mask_meta_keyword_category" class="form-control"><?php echo $config_mask_meta_keyword_category; ?></textarea>
              </div>
            </div>
            
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_title_product"><?php echo $entry_mask_meta_title_product; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_title_product" rows="5" placeholder="<?php echo $entry_mask_meta_title_product; ?>" id="input-mask_meta_title_product" class="form-control"><?php echo $config_mask_meta_title_product; ?></textarea>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_description_product"><?php echo $entry_mask_meta_description_product; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_description_product" rows="5" placeholder="<?php echo $entry_mask_meta_description_product; ?>" id="input-mask_meta_description_product" class="form-control"><?php echo $config_mask_meta_description_product; ?></textarea>
              </div>
            </div>
            <div class="form-group">
              <label class="col-sm-2 control-label" for="input-mask_meta_keyword_product"><?php echo $entry_mask_meta_keyword_product; ?></label>
              <div class="col-sm-10">
                <textarea name="config_mask_meta_keyword_product" rows="5" placeholder="<?php echo $entry_mask_meta_keyword_product; ?>" id="input-mask_meta_keyword_product" class="form-control"><?php echo $config_mask_meta_keyword_product; ?></textarea>
              </div>
            </div>
		]]>      
        </add>
    </operation>
</file>

In the language file of the settings page setting.php, located path “admin/language/en-ru/setting/setting.php” we add the name of the future fields under the tags.
In the controller of the settings page setting.php, located path “admin/controller/setting/setting.php”, we define language variables and program initialization and assignment of values to the fields when saving.
In the file of the visual presentation of the settings page setting.tpl, located path “admin/view/template/setting/setting.tpl” we program the output of fields.

As a result, in the settings, in the “Store” tab, the created fields should appear, which, later, we will use for storage the mask of meta tags.

2. Now to assign a mask for categories and products in the controller of the site header.

<file path="catalog/controller/common/header.php">
    <operation>
        <search>
        <![CDATA[
            $data['name'] = $this->config->get('config_name');
		]]>      
        </search>
		<add position="after">
        <![CDATA[
            if(isset($this->request->get['route']) && $this->request->get['route']=='product/category'){
				$this->load->model('catalog/category');
				$parts = explode('_', (string)$this->request->get['path']);
				$total=count($parts);
				$category_info=$this->model_catalog_category->getCategory($parts[$total-1]);

				if($this->document->getTitle()==$category_info["name"]){
					$config_mask_meta_title_category = $this->config->get('config_mask_meta_title_category');

					if($config_mask_meta_title_category!=''){
						$config_mask_meta_title_category = str_replace("%h1%", $category_info["name"], $config_mask_meta_title_category);

						$data['title'] = $config_mask_meta_title_category;
					}
				}

				if($this->document->getDescription()==''){
					$config_mask_meta_description_category = $this->config->get('config_mask_meta_description_category');

					if($config_mask_meta_description_category!=''){
						$config_mask_meta_description_category = str_replace("%h1%", $category_info["name"], $config_mask_meta_description_category);

						$data['description'] = $config_mask_meta_description_category;
					}
				}

				if($this->document->getKeywords()==''){
					$config_mask_meta_keyword_category = $this->config->get('config_mask_meta_keyword_category');

					if($config_mask_meta_keyword_category!=''){
						$config_mask_meta_keyword_category = str_replace("%h1%", $category_info["name"], $config_mask_meta_keyword_category);

						$data['keywords'] = $config_mask_meta_keyword_category;
					}
				}
	        }

	        if(isset($this->request->get['route']) && $this->request->get['route']=='product/product'){
	        	$this->load->model('catalog/product');
	        	$product_info=$this->model_catalog_product->getProduct($this->request->get['product_id']);
	        	$temp_title=$this->document->getTitle();
				if($product_info["name"]==$temp_title){
					$config_mask_meta_title_product = $this->config->get('config_mask_meta_title_product');

					if($config_mask_meta_title_product!=''){
						$config_mask_meta_title_product = str_replace("%h1%", $product_info["name"], $config_mask_meta_title_product);

						$data['title'] = $config_mask_meta_title_product;
					}
				}
				
				if($this->document->getDescription()==''){
					$config_mask_meta_description_product = $this->config->get('config_mask_meta_description_product');

					if($config_mask_meta_description_product!=''){
						$config_mask_meta_description_product = str_replace("%h1%", $product_info["name"], $config_mask_meta_description_product);

						$data['description'] = $config_mask_meta_description_product;
					}
				}
				
				if($this->document->getKeywords()==''){
					$config_mask_meta_keyword_product = $this->config->get('config_mask_meta_keyword_product');

					if($config_mask_meta_keyword_product!=''){
						$config_mask_meta_keyword_product = str_replace("%h1%", $product_info["name"], $config_mask_meta_keyword_product);

						$data['keywords'] = $config_mask_meta_keyword_product;
					}
				}
			}
		]]>      
        </add>
    </operation>
</file>

In the controller, the header of the site header.php, located path “catalog/controller/common/header.php”, make the definition of meta tags.
In blocks
if(isset($this->request->get[‘route’]) && $this->request->get[‘route’]==’product/category’)
if(isset($this->request->get[‘route’]) && $this->request->get[‘route’] ==’product/product’)
check which page is open – the page of the category or product.

Then for each meta tag we determine if it is not filled in the admin panel, we substitute a mask instead. If it is full, go to the one in the admin panel.
With title, the situation is slightly different. It is always filled when creating a product or category. Therefore, we first check whether the title is the same as the category / product name, and if it’s the same, put the nurse.
As a substitute for the word in the mask on the name of the product / category in this example is used %h1%
$config_mask_meta_title_product = str_replace(“%h1%”, $product_info[“name”], $config_mask_meta_title_product);
You can use your own.

That’s basically all, the result will be

Download module finish file.




Connect

If you have any questions, want to get individual advice or you have an interesting offer, you can contact via the feedback form.

Or write to the email info@bobreod.ru





By submitting the form, you agree to our privacy policy