Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Excellent question. As you noted, the data you wish to display is already part of the admin/backend of OpenCart, but it is not available on the frontend. I'll show you how to add it.</p> <p>Due to the <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="nofollow">MVC</a> architecture of OpenCart, you'll have to make changes in 3 places. The Model, the View and the Controller. First things first, you will have to get the data from the database. Because we're looking to make changes to the frontend, everything will be contained in the <em>catalog</em> directory. If you look at the code, you'll find <em>catalog/model/catalog/product.php</em>. This is where we're going to make our first change. Special price is available in the ModelCatalogProduct, but the special price end date is not. You can either modify the existing getProduct() method, or you can create your own method. I am going to show you the latter, while the former is left as an exercise for the user.</p> <p><strong>catalog/model/catalog/product.php</strong></p> <pre><code>class ModelCatalogProduct extends Model { ... // Return an array containing special (price, date_start, date_end). // or false if no special price exists. public function getSpecialPriceDates($product_id) { if ($this-&gt;customer-&gt;isLogged()) { $customer_group_id = $this-&gt;customer-&gt;getCustomerGroupId(); } else { $customer_group_id = $this-&gt;config-&gt;get('config_customer_group_id'); } $query = $this-&gt;db-&gt;query("SELECT price, date_start, date_end FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND ((date_start = '0000-00-00' OR date_start &lt; NOW()) AND (date_end = '0000-00-00' OR date_end &gt; NOW())) ORDER BY priority ASC, price ASC LIMIT 1"); if ($query-&gt;num_rows) { return array( 'special' =&gt; $query-&gt;row['price'], 'date_start' =&gt; $query-&gt;row['date_start'], 'date_end' =&gt; $query-&gt;row['date_end'], ); } else { return false; } } ... } </code></pre> <p>Great, now there is a function getSpecialPriceDates() you can call to find out when a product special will end. Let's make this data available to the View. In order to to that, we're going to have to add it to the Controller. Look in the ControllerProductProduct for where the 'special' variable is set.</p> <p><strong>catalog/controller/product/product.php</strong></p> <pre><code>... if ((float)$product_info['special']) { $this-&gt;data['special'] = $this-&gt;currency-&gt;format($this-&gt;tax-&gt;calculate($product_info['special'], $product_info['tax_class_id'], $this-&gt;config-&gt;get('config_tax'))); // +++ NEW CODE $special_info = $this-&gt;model_catalog_product-&gt;getSpecialPriceDates($product_id); if ($special_info) { $this-&gt;data['special_date_end'] = $special_info['date_end']; } else { $this-&gt;date['special_date_end'] = false; } // +++ END NEW CODE } else { $this-&gt;data['special'] = false; } ... </code></pre> <p>The last task is to implement your timer in the product view. This will be located somewhere like <em>catalog/view/theme/default/template/product/product.tpl</em> (if you have your own theme, replace <em>default</em> with <em>{your-theme}</em>). There are a lot of different countdown timer solutions for jQuery, pick your <a href="http://plugins.jquery.com/project/countdown2" rel="nofollow" title="jQuery Countdown">favorite</a>.</p> <p><strong>catalog/view/theme/default/template/product/product.tpl</strong></p> <pre><code> &lt;?php if (!$special) { ?&gt; &lt;?php echo $price; ?&gt; &lt;?php } else { ?&gt; &lt;span class="price-old"&gt;&lt;?php echo $price; ?&gt;&lt;/span&gt; &lt;span class="price-new"&gt;&lt;?php echo $special; ?&gt;&lt;/span&gt; &lt;?php if ($special_date_end): ?&gt; &lt;!-- TIMER CODE HERE --&gt; &lt;div class="timer"&gt;&lt;/div&gt; &lt;?php endif; ?&gt; &lt;?php } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload