Blog

Cubecart5

CubeCart 5 Plugin – Part One

CubeCart 5 is a great eCommerce shopping cart with great out of the box features. Having said this, in my experience whichever eCommerce package you choose the customers will often want a feature that is not available straight out of the box.

CubeCart5 solves this problem by providing a hook system enabling the easy integration of custom made plugins without the need to alter any of the source code.

Unfortunately the documentation for CubeCart 5 is not brilliant with regard to how to create these plugins from a developers point of view. A quick delve into the source code however reveals how the system works without too much trouble.

In this two part post I have included all of the the code to create a simple CubeCart 5 plugin which will allow you to set the default sort order for products on the category pages, allowing you to set the default sort order by price, date added or name. The files from this tutorial can be downloaded in zip format here

You can add also add further settings or adjustments but this will hopefully give you some ideas on how the plugins work.

First create a folder called Advanced_Settings in the modules/plugins folder

Place the config.xml file in your Advanced_Settings folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8" ?>
<installer version="1.0">
  <info>
    <uid>advanced_settings@tidy-designs.com</uid>
    <type>plugins</type>
    <folder>Advanced_settings</folder>
    <name><![CDATA[Advanced Settings]]></name>
    <description><![CDATA[Additional CubeCart Settings]]></description>
    <version>1.0.0</version>
    <minVersion>5.0.1</minVersion>
    <maxVersion>5.0.*</maxVersion>
    <creator><![CDATA[Ben Davies - tidy-designs.com]]></creator>
    <homepage><![CDATA[http://www.tidy-designs.com]]></homepage>
  </info>
  
  <hooks> 
    <hook trigger="controller.index" enabled="1" />
    <hook trigger="class.cubecart.construct" enabled="1" />
    <hook trigger="class.cubecart.display_category" enabled="1" />
  </hooks>
</installer>

Then create a folder admin in the Advanced_Settings folder and create a file called index.inc.php in there with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
$module = new Module(__FILE__, $_GET['module'], 'admin/index.tpl', false, false);

$currentSettings = array();
if ($GLOBALS['config']->has('Advanced_Settings', 'product_sort_field') && $productSortField = $GLOBALS['config']->get('Advanced_Settings', 'product_sort_field')) { 
    $currentSettings['product_sort_field'] = $productSortField;
}
if ($GLOBALS['config']->has('Advanced_Settings', 'product_sort_direction') && $productSortDirection = $GLOBALS['config']->get('Advanced_Settings', 'product_sort_direction')) { 
    $currentSettings['product_sort_direction'] = $productSortDirection;
}

$GLOBALS['smarty']->assign('currentSettings',$currentSettings);

$module->fetch();
$page_content = $module->display();
?>

Next create a file called index.tpl in folder skin/admin within the Advanced_Settings folder. Place the following code in the index.tpl file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<div id="config" class="tab_content">
<h3>Advanced Settings</h3>
<form action="{$VAL_SELF}" method="post" enctype="multipart/form-data">
  <fieldset>        
    <div><label for="status">Status</label><span>
      <select name="module[status]" id="status" class="textbox">
        <option value="0" {$SELECT_status_0}>{$LANG.common.disabled}</option>
        <option value="1" {$SELECT_status_1}>{$LANG.common.enabled}</option>
      </select></span>
    </div>
  </fieldset>
  <fieldset>
    <div><label for="product_sort_field">Product Sort Field</label><span>
      <select name="module[product_sort_field]" id="product_sort_field" class="textbox">
        <option value="name"{if $currentSettings.product_sort_field == 'name'} selected="selected"{/if}>{$LANG.common.name}</option>
        <option value="price"{if $currentSettings.product_sort_field == 'price'} selected="selected"{/if}>{$LANG.common.price}</option>
        <option value="date_added"{if $currentSettings.product_sort_field == 'date_added'} selected="selected"{/if}>{$LANG.category.sort_date}</option>
      </select></span>
    </div>
    <div><label for="product_sort_direction">Product Sort Direction</label><span>
      <select name="module[product_sort_direction]" id="product_sort_direction" class="textbox">
      {if $currentSettings.product_sort_field == 'date_added'}
        <option value="ASC"{if $currentSettings.product_sort_direction == 'ASC'} selected="selected"{/if}>{$LANG.category.sort_date_added_asc}</option>
        <option value="DESC"{if $currentSettings.product_sort_direction == 'DESC'} selected="selected"{/if}>{$LANG.category.sort_date_added_desc}</option>
      {elseif $currentSettings.product_sort_field == 'price'}
        <option value="ASC"{if $currentSettings.product_sort_direction == 'ASC'} selected="selected"{/if}>{$LANG.category.sort_low_high}</option>
        <option value="DESC"{if $currentSettings.product_sort_direction == 'DESC'} selected="selected"{/if}>{$LANG.category.sort_high_low}</option>      
      {else}
        <option value="ASC"{if $currentSettings.product_sort_direction == 'ASC'} selected="selected"{/if}>{$LANG.category.sort_name_asc}</option>
        <option value="DESC"{if $currentSettings.product_sort_direction == 'DESC'} selected="selected"{/if}>{$LANG.category.sort_name_desc}</option>      
      {/if}
      </select></span>
    </div>
    <script type="text/javascript">
        var select_list_field = document.getElementById('product_sort_field');
        select_list_field.onchange = function() {
            var select_list_selected_index = select_list_field.selectedIndex;
            var text = select_list_field.options[select_list_selected_index].text;
            var value = select_list_field.value;
            
            var select_list_field2 = document.getElementById('product_sort_direction');
            var select_list_selected_index2 = select_list_field2.selectedIndex;
            var text2 = select_list_field2.options[select_list_selected_index2].text;
            var value2 = select_list_field2.value;
            
            if (value == "name") {
                if (value2 == 'ASC') {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_name_asc}", "ASC", true, true);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_name_desc}", "DESC", true, false);
                } else {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_name_asc}", "ASC", true, false);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_name_desc}", "DESC", true, true);                
                }                
            }

            if (value == "price") {
                if (value2 == 'ASC') {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_low_high}", "ASC", true, true);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_high_low}", "DESC", true, false);
                } else {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_low_high}", "ASC", true, false);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_high_low}", "DESC", true, true);                
                }                
            }
            
            if (value == "date_added") {
                if (value2 == 'ASC') {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_date_added_asc}", "ASC", true, true);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_date_added_desc}", "DESC", true, false);
                } else {
                    select_list_field2.options[0]=new Option("{$LANG.category.sort_date_added_asc}", "ASC", true, false);
                    select_list_field2.options[1]=new Option("{$LANG.category.sort_date_added_desc}", "DESC", true, true);                
                }                
            }
        }
    </script>
  </fieldset>
  <div class="form_control">
    <input type="submit" value="{$LANG.common.save}" /> <br/><span style="font-size: 10px;">{$LANG.adv_banner.save_btn_desc} </span>
  </div>
  <input type="hidden" name="token" value="{$SESSION_TOKEN}" />
</form>  
</div>

Go to part two for the remaining code and to download a all the necessary files.

Related Posts

Discussion

3 Comment(s)

  1. jordan barton

    March 30, 2012 at 5:05 pm

    this is a good website to make websites i shall use this more offen

  2. Gussie Kourkoumellis

    April 4, 2012 at 5:46 pm

    Some genuinely quality blog posts on this internet site , bookmarked .

  3. Jude Fragosa

    April 6, 2012 at 2:50 pm

    I went over this site and I think you have a lot of wonderful information, saved to bookmarks (:.

Leave a Reply