If you are an Opencart developer or you have gone through one of Opencart’s controller then there, you would find a repetitive code of line for fetching the language or performing some validations. Here, I’m providing some tips in order to minimise the code from several lines to 5-6 lines.
Here, I’m covering a few examples to make your OPpencart code optimised.
Here’s the example of language in the product page and the appropriate solution to it. You would find a code like this on many pages.
$this->load->language('catalog/product'); $data['text_enabled'] = $this->language->get('text_enabled'); $data['text_disabled'] = $this->language->get('text_disabled'); $data['text_none'] = $this->language->get('text_none'); $data['text_yes'] = $this->language->get('text_yes'); $data['text_no'] = $this->language->get('text_no'); $data['text_plus'] = $this->language->get('text_plus'); $data['text_minus'] = $this->language->get('text_minus'); $data['text_default'] = $this->language->get('text_default'); $data['text_option'] = $this->language->get('text_option'); $data['text_option_value'] = $this->language->get('text_option_value'); $data['text_select'] = $this->language->get('text_select'); $data['text_percent'] = $this->language->get('text_percent'); $data['text_amount'] = $this->language->get('text_amount');
So, this code is can be made very minimised by using just these line of codes.
$language_array = $this->load->language('catalog/product'); foreach ($language_array as $language_key => $language_value) { $data[$language_key] = $language_value; }
or, just writing,
$data = array(); $data = array_merge($data, $this->load->language('catalog/product'));
or, simply,
$data = $this->load->language('catalog/product');
Similarly, we have another code which delivers data to form whether for add or edit.
if (isset($this->request->post['model'])) { $data['model'] = $this->request->post['model']; } elseif (!empty($product_info)) { $data['model'] = $product_info['model']; } else { $data['model'] = ''; } if (isset($this->request->post['sku'])) { $data['sku'] = $this->request->post['sku']; } elseif (!empty($product_info)) { $data['sku'] = $product_info['sku']; } else { $data['sku'] = ''; } if (isset($this->request->post['upc'])) { $data['upc'] = $this->request->post['upc']; } elseif (!empty($product_info)) { $data['upc'] = $product_info['upc']; } else { $data['upc'] = ''; } if (isset($this->request->post['ean'])) { $data['ean'] = $this->request->post['ean']; } elseif (!empty($product_info)) { $data['ean'] = $product_info['ean']; } else { $data['ean'] = ''; }
This code can be made optimised by putting all the indexes in one array and then iterating the code for those set of values like this.
$form_keys = array('model','sku','upc','ean','location','jan','isbn','mpn');// more values can be added to this array foreach ($form_keys as $form_value) { if (isset($this->request->post[$form_value])) { $data[$form_value] = $this->request->post[$form_value]; } elseif (!empty($product_info)) { $data[$form_value] = $product_info[$form_value]; } else { $data[$form_value] = ''; } }
Similar will be the case with getting config values. For that too, you can use the above technique.
These were a few optimisations. Other techniques will be posted in the upcoming blogs.
Be the first to comment.