<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Development, SEO and Project Management &#187; opencart</title>
	<atom:link href="http://www.webopius.com/content/tag/opencart/feed" rel="self" type="application/rss+xml" />
	<link>http://www.webopius.com/content</link>
	<description>Web Design &#38; Development, SEO and Project Management</description>
	<lastBuildDate>Sun, 29 Jan 2012 16:11:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Showing a random product on your homepage in OpenCart</title>
		<link>http://www.webopius.com/content/416/showing-a-random-product-on-your-homepage-in-opencart</link>
		<comments>http://www.webopius.com/content/416/showing-a-random-product-on-your-homepage-in-opencart#comments</comments>
		<pubDate>Sun, 28 Feb 2010 16:46:45 +0000</pubDate>
		<dc:creator>Webopius</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[ecommerce]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://www.webopius.com/content/?p=416</guid>
		<description><![CDATA[Another OpenCart article today (see link to the right). After this, we&#8217;ll be writing some new ASPDotNetStorefront tutorials. Related Links&#8230; OpenCart Open Source shopping This tutorial will show you how to extend OpenCart and retrieve a random product from your product catalogue to display on the homepage. Each time a user refreshes the page, or [...]]]></description>
			<content:encoded><![CDATA[<p>Another OpenCart article today (see link to the right). After this, we&#8217;ll be writing some new <a href="/content/tag/aspdotnetstorefront">ASPDotNetStorefront tutorials</a>.</p>
<blockquote class="citeright"><p>
<b>Related Links&#8230;</b><br />
<a href="http://www.opencart.com/" rel="nofollow">OpenCart Open Source shopping</a>
</p>
</blockquote>
<p>This tutorial will show you how to extend OpenCart and retrieve a random product from your product catalogue to display on the homepage. Each time a user refreshes the page, or a new visitor goes to your site, a different product will be shown.</p>
<p>First, a big thank you to the author of article on <a rel="nofollow" href="http://www.greggdev.com/web/articles.php?id=6">Gregg Website Development</a> who showed a method for quickly retrieving random rows from MySQL on large datasets with minimal performance impact.</p>
<h2>Step 1. Extend your product model with a getRandomProduct() function</h2>
<p>The OpenCart MVC model shows it&#8217;s power here as we add a new function to the product model. Edit the &#8216;product.php&#8217; file located in your site&#8217;s /catalog/model/catalog/product directory and add a new function called getRandomProduct() to the class as follows:</p>
<pre><code>
// Retrieve a random product from the database
public function getRandomProduct() {
   $maxminid = $this-&gt;db-&gt;query(&quot;SELECT MAX(product_id) as max_id, MIN(product_id) as min_id from &quot;. DB_PREFIX . &quot;product p&quot;);
   $maxrow = -1;
   $minrow = -1;
   if ($maxminid-&gt;num_rows) {
        $maxrow = $maxminid-&gt;row[&#039;max_id&#039;];
        $minrow = $maxminid-&gt;row[&#039;min_id&#039;];
    }
   $randomrow = mt_rand($minrow, $maxrow);

   $query = $this-&gt;db-&gt;query(&quot;SELECT DISTINCT *, pd.name AS name, pd.short_description as short_description, p.image, m.name AS manufacturer, ss.name AS stock FROM &quot; . DB_PREFIX . &quot;product p LEFT JOIN &quot; . DB_PREFIX . &quot;product_description pd ON (p.product_id = pd.product_id) LEFT JOIN &quot; . DB_PREFIX . &quot;manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN &quot; . DB_PREFIX . &quot;stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.product_id &gt;= &#039;&quot; . (int)$randomrow . &quot;&#039; AND pd.language_id = &#039;&quot; . (int)$this-&gt;language-&gt;getId() . &quot;&#039; AND ss.language_id = &#039;&quot; . (int)$this-&gt;language-&gt;getId() . &quot;&#039; AND p.date_available &lt;= NOW() AND p.status = &#039;1&#039; order by p.product_id ASC limit 1&quot;);

    if (!$query-&gt;row) {
        $query = $this-&gt;db-&gt;query(&quot;SELECT DISTINCT *, pd.name AS name, pd.short_description as short_description, p.image, m.name AS manufacturer, ss.name AS stock FROM &quot; . DB_PREFIX . &quot;product p LEFT JOIN &quot; . DB_PREFIX . &quot;product_description pd ON (p.product_id = pd.product_id) LEFT JOIN &quot; . DB_PREFIX . &quot;manufacturer m ON (p.manufacturer_id = m.manufacturer_id) LEFT JOIN &quot; . DB_PREFIX . &quot;stock_status ss ON (p.stock_status_id = ss.stock_status_id) WHERE p.product_id &lt; &#039;&quot; . (int)$randomrow . &quot;&#039; AND pd.language_id = &#039;&quot; . (int)$this-&gt;language-&gt;getId() . &quot;&#039; AND ss.language_id = &#039;&quot; . (int)$this-&gt;language-&gt;getId() . &quot;&#039; AND p.date_available &lt;= NOW() AND p.status = &#039;1&#039; order by p.product_id DESC limit 1&quot;);
    }
    return $query-&gt;row;
}
<!--formatted--></code></pre>
<p>What this function does is find the maximum and minimum product ids in your database and generates a random value in this range.</p>
<p>It then looks for a product row with an ID >= the random ID. If none is found, it selects an ID less than the random ID. The product row is returned.</p>
<h2>Step 2. Change your homepage controller to ask for a random product</h2>
<p>Now, you can change any of your OpenCart controller pages to retrieve a random product. In this example, we will use the homepage controller. Find the file &#8216;home.php&#8217; in your site&#8217;s /catalog/controller/common directory.</p>
<p>Insert the following lines into the code:</p>
<pre><code>
$featured = $this-&gt;model_catalog_product-&gt;getRandomProduct();
$this-&gt;data[&#039;featuredproduct&#039;] = array(
    &#039;name&#039; =&gt; $featured[&#039;name&#039;],
    &#039;thumb&#039;   =&gt; image_resize($featured[&#039;image&#039;], 165, 165),
    &#039;href&#039; =&gt; $this-&gt;model_tool_seo_url-&gt;rewrite($this-&gt;url-&gt;http(&#039;product/product&amp;product_id=&#039; . $featured[&#039;product_id&#039;]))
);
<!--formatted--></code></pre>
<p>This example just uses the product&#8217;s name, image thumb and URL but you can use any other available product fields returned by the SQL you created earlier. This code will create an array called $featuredproduct[] containing a single random product that can be used on your home page.</p>
<h2>Step 3. Add the featured product to your home page</h2>
<p>The last step involves rendering the featured product on your home page. How and where you place the product will depend very much on your site&#8217;s template and styles. The example below just shows how to display the results &#8211; you will probably want to wrap the code with css for styling.</p>
<p>Edit the home.tpl file with your site&#8217;s /catalog/view/theme/[your theme]/template/common directory and insert the following lines at the point you want the featured product displayed:</p>
<pre><code>
&lt;b&gt;Featured Product...&lt;/b&gt;
&lt;?php echo &#039;&lt;a class=&quot;image&quot; href=&quot;&#039; . $featuredproduct[&#039;href&#039;] .&#039;&quot; title=&quot;&#039; . $featuredproduct[&#039;name&#039;] . &#039;&quot;&gt;&lt;img src=&quot;&#039;.$featuredproduct[&#039;thumb&#039;].&#039;&quot; alt=&quot;&#039; . $featuredproduct[&#039;name&#039;].&#039;&quot;/&gt;&lt;/a&gt;&#039;; ?&gt;
&lt;?php echo &#039;&lt;b&gt;&lt;a href=&quot;&#039; . $featuredproduct[&#039;href&#039;] .&#039;&quot; title=&quot;&#039; . $featuredproduct[&#039;name&#039;] . &#039;&quot;&gt;&#039;. $featuredproduct[&#039;name&#039;].&#039;&lt;/a&gt;&lt;/b&gt;&#039;; ?&gt;
<!--formatted--></code></pre>
<p>This code uses the $featuredproduct[] variable that you just created in the controller code and places a featured product thumbnail image, link and description on the home page.</p>
<h2>More OpenCart features</h2>
<p>This is just one example of how OpenCart can easily be extended. We have also implemented full SEO friendly URLs, news pages, <a href="/content/406/creating-image-watermarks-in-opencart">product image watermarks</a> and an updated product export/import system.</p>
<p>If you would like to discuss implementing an <a href="/enquiry.php">e-commerce solution based on OpenCart</a>, please get in touch.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webopius.com/content/416/showing-a-random-product-on-your-homepage-in-opencart/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating image watermarks in OpenCart</title>
		<link>http://www.webopius.com/content/406/creating-image-watermarks-in-opencart</link>
		<comments>http://www.webopius.com/content/406/creating-image-watermarks-in-opencart#comments</comments>
		<pubDate>Sat, 27 Feb 2010 19:42:56 +0000</pubDate>
		<dc:creator>Webopius</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[ecommerce]]></category>
		<category><![CDATA[opencart]]></category>

		<guid isPermaLink="false">http://www.webopius.com/content/?p=406</guid>
		<description><![CDATA[We&#8217;ve been using the fantastic OpenCart e-commerce system (see link to the right) for some time now on a number of sites. Related Links&#8230; OpenCart Open Source shopping OpenCart has almost every feature that you need to build an e-commerce solution quickly. More importantly, it can be extended easily and has a strong online community. [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve been using the fantastic OpenCart e-commerce system (see link to the right) for some time now on a number of sites.</p>
<blockquote class="citeright"><p>
<b>Related Links&#8230;</b><br />
<a href="http://www.opencart.com/" rel="nofollow">OpenCart Open Source shopping</a>
</p></blockquote>
<p>OpenCart has almost every feature that you need to build an e-commerce solution quickly. More importantly, it can be extended easily and has a strong online community. We&#8217;ve recently made a simple enhancement to one of the stores we developed (the <a href="http://www.orchidstore.co.uk">Orchid Store</a>) to overlay product images with a watermark like this:</p>
<p><img src="http://www.webopius.com/content/wp-content/uploads/2010/02/orchid.jpg" alt="orchid" title="orchid" width="300" height="300" class="alignnone size-full wp-image-407" /></p>
<p>It&#8217;s a really simple change and I thought it would be useful to others if I posted how to do it. Please note, this solution is a &#8216;quick implementation&#8217; and can be enhanced further.</p>
<h2>Step 1. Create your watermark overlay image</h2>
<p>The first step is to create a small image that will become the watermark overlaying your product images. I would recommend using a transparent PNG format. In the example above, the background of the watermark is transparent and the text is set to 50% opacity.  </p>
<p>Save this image as &#8216;watermark.png&#8217;  in your site&#8217;s /images/userfiles directory.</p>
<h2>Step 2. Create some new OpenCart image functions</h2>
<p>The next step is to create a couple of new watermark functions within the OpenCart system area. Open the /system/library/image.php file.  You will see that there is already a watermark() function available. </p>
<p>In this example, we are going to create a new watermark function as it makes handling any upgrades of OpenCart slightly easier and you are adding to rather than changing any existing functionality. You may prefer to edit the existing watermark() function directly.</p>
<p>The new function looks like this:</p>
<pre>
<code>
public function addwatermark($position = &#039;bottomright&#039;) {
        $watermark = imagecreatefrompng(DIR_IMAGE . &#039;userfiles/watermark.png&#039;);
        $watermark_width = imagesx($watermark);
        $watermark_height = imagesy($watermark);

        switch($position) {
            case &#039;topleft&#039;:
                $watermark_pos_x = 0;
                $watermark_pos_y = 0;
                break;
            case &#039;topright&#039;:
                $watermark_pos_x = $this-&gt;info[&#039;width&#039;] - $watermark_width;
                $watermark_pos_y = 0;
                break;
            case &#039;bottomleft&#039;:
                $watermark_pos_x = 0;
                $watermark_pos_y = $this-&gt;info[&#039;height&#039;] - $watermark_height;
                break;
            case &#039;bottomright&#039;:
                $watermark_pos_x = $this-&gt;info[&#039;width&#039;] - $watermark_width;
                $watermark_pos_y = $this-&gt;info[&#039;height&#039;] - $watermark_height;
                break;
           case &#039;middle&#039;:
                $watermark_pos_x = ($this-&gt;info[&#039;width&#039;] - $watermark_width)/2;
                $watermark_pos_y = ($this-&gt;info[&#039;height&#039;] - $watermark_height)/2;
                break;
        }

        imagecopy($this-&gt;image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, $watermark_width, $watermark_height);
        imagedestroy($watermark);
     }
<!--formatted--></code></pre>
<p>What the function above is doing is creating an image object from your watermark (use imagecreatefromgif() if you have chosen a gif format) and then placing this watermark over the image (supplied in $this) in a position you specify (topleft, topright, bottomleft, bottomright or middle).</p>
<p>Now, edit /system/helper/image.php and add the following code as an additional function:</p>
<pre><code>
function image_watermark($filename) {
	if ( (!file_exists(DIR_IMAGE . $filename)) &amp;&amp; (!file_exists(DIR_IMAGE . &#039;cache/&#039; . $filename)) ) {
		return;
	} 

	if (file_exists(DIR_IMAGE . &#039;cache/&#039; . $filename)) {
		$old_image = DIR_IMAGE . &#039;cache/&#039; . $filename;
	} else {
		$old_image = DIR_IMAGE . $filename;
	}

	$new_image = &#039;cache/&#039; . substr($filename, 0, strrpos($filename, &#039;.&#039;)) . &#039;-w.jpg&#039;;

	if (!file_exists(DIR_IMAGE . $new_image) || (filemtime($old_image) &gt; filemtime(DIR_IMAGE . $new_image))) {
		$image = new Image($old_image);
		$image-&gt;addwatermark(&#039;middle&#039;);
		$image-&gt;save(DIR_IMAGE . $new_image);
	}

	if (isset($_SERVER[&#039;HTTPS&#039;]) &amp;&amp; (($_SERVER[&#039;HTTPS&#039;] == &#039;on&#039;) || ($_SERVER[&#039;HTTPS&#039;] == &#039;1&#039;))) {
		return HTTPS_IMAGE . $new_image;
	} else {
		return HTTP_IMAGE . $new_image;
	}
}
<!--formatted--></code></pre>
<p>What the above code is doing is being given a product image filename ($filename). It then checks in both the standard image directory and the cache directory for that product image. This is slightly non standard from the usual OpenCart which only looks in the main image directory. We added the second cache check because we will call this watermark function after the product image has been re-sized and placed in the cache directory.</p>
<p>What the code then does is create a new &#8216;watermarked&#8217; filename for the image by appending a &#8216;-w.jpg&#8217; to the existing filename. It then checks the cache to see if a watermark has already been created. If not, it calls the addwatermark() function we just created. The resulting watermarked image filename is then returned.</p>
<h3>Step 3. Modify your OpenCart product template to show the watermark</h3>
<p>The watermark function can of course be used in any templates and image fields you choose. In this example, we are using the &#8216;popup&#8217; product image field. Locate the product.php file in your sites /catalog/controller/product directory. Look for a line similar to this in the code:</p>
<pre><code>$this-&gt;data[&#039;popup&#039;]  = image_resize($image, $this-&gt;config-&gt;get(&#039;config_image_popup_width&#039;), $this-&gt;config-&gt;get(&#039;config_image_popup_height&#039;));	<!--formatted--></code></pre>
<p>&#8230;and change it to this:</p>
<pre><code>$productimage = image_resize($image, $this-&gt;config-&gt;get(&#039;config_image_popup_width&#039;),$this-&gt;config-&gt;get(&#039;config_image_popup_height&#039;));
$wmimage = image_watermark(basename($productimage));
$this-&gt;data[&#039;popup&#039;] = $wmimage;<!--formatted--></code></pre>
<p>This adds an additional step to the process. After resizing the image, the $productimage file will be placed in the cache. Image_watermark() is then called to overlay the product image with your watermark. </p>
<p>Hopefully, if you&#8217;ve followed these steps your site will now be showing product images with watermarks added. As I mentioned at the start, this solution can be extended and improved further and it would be good to hear feedback from others. </p>
<p>The OpenCart image library also contains a number of other hidden features including rotation and cropping. We have also extended the OpenCart image helpers further with dynamic image scaling. If anyone is interested in this, <a href="/enquiry.php">please get in touch</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.webopius.com/content/406/creating-image-watermarks-in-opencart/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.322 seconds -->

