How to display pre-defined image if product doesn’t have image in OpenCart

OpenCart 2.0 can display a default image for a product if it does not have one. However, in older versions like OpenCart 1.5.6.4 and below, such functionality is missing. We have created a simple vQmod modification, which will allow you to display a pre-defined placeholder image.

In order to modify the necessary files we will use vQmod, which will only override them, instead of modifying them directly. You will need to have vQmod installed in your OpenCart for this to work, you follow the quick guide here - https://github.com/vqmod/vqmod/wiki/Installing-vQmod-on-OpenCart.

Open up your favourite text editor or any code editing program and create a new file called default_product_image.xml Paste the following code into the file, save it and upload it to your /vqmod/xml directory.  

<modification>
    <id>Show a default image for products</id>
	<version>1.4.x and above</version>
	<vqmver>2.5.0</vqmver>
	<author>isenselabs.com</author>
	<file name="catalog/model/catalog/product.php">
		<operation error="skip" info="Default Image">
			<search position="after"><![CDATA[class ModelCatalogProduct extends Model {]]></search>
			<add><![CDATA[
				// Path to your default product image.
				private $default_image = 'no_image.jpg';
			]]></add>
		</operation>
		<operation error="skip" info="Modify Main Image Query">
			<search position="replace"><![CDATA[$query->row['image']]]></search>
			<add><![CDATA[((empty($query->row['image']) && file_exists(DIR_IMAGE . $query->row['image']) && !empty($this->default_image) && file_exists(DIR_IMAGE . $this->default_image)) ? $this->default_image : $query->row['image'])]]></add>
		</operation>
		<operation error="skip" info="Modify Additional Images Query">
			<search position="after"><![CDATA[$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order ASC");]]></search>
			<add><![CDATA[
				if (!empty($query->rows)) { foreach ($query->rows as $k => $query_row) {
					$query->rows[$k]['image'] = ((empty($query_row['image']) && file_exists(DIR_IMAGE . $query_row['image']) && !empty($this->default_image) && file_exists(DIR_IMAGE . $this->default_image)) ? $this->default_image : $query_row['image']);
				} }
			]]></add>
		</operation>
	</file>
</modification>

OpenCart 1.5.6.4 comes with a placeholder image by default - /image/no_image.jpg. This is the image we are using in our vQmod file, however, you can easily change that. Browse the web or create your own empty image placeholder and upload it to the /image/ folder, located in the root of your OpenCart. After that you will only need to modify one line in the vQmod modification file to change the image. Locate this line and replace no_image.jpg with the name of your custom image:

private $default_image = 'no_image.jpg';

How the above modification works out.

We hope that you will find this tutorial helpful.

Download Source

Join 11,000+ subscribers receiving actionable E-commerce advice

* Unsubscribe any time

Trending blogs

comments powered by Disqus