Latest News: (loading..)
PupStar

How to build a content module

12 posts in this topic

This brief guide is for anyone who wants to make their own content module.

 

The code below shows how to make a 'Call to Action' module as inspired by the template made by Gary

 

http://www.template.me.uk/responsive-saul/index.php

 

This particular call to action module displays a checkout message in the header when there are products in the shopping cart prompting the user to finish their order.

 

Step 1.

Create a file in catalog/includes/modules/content/header called cm_header_call_to_checkout.php and add the following code to it:

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2014 osCommerce

  Released under the GNU General Public License
*/

  class cm_header_call_to_checkout {
    var $code;
    var $group;
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;

    function cm_header_call_to_checkout() {
      $this->code = get_class($this);
      $this->group = basename(dirname(__FILE__));

      $this->title = MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_TITLE;
      $this->description = MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_DESCRIPTION;

      if ( defined('MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_STATUS') ) {
        $this->sort_order = MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_SORT_ORDER;
        $this->enabled = (MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_STATUS == 'True');
      }
    }

    function execute() {

      global $PHP_SELF, $cart, $currencies, $HTTP_GET_VARS, $request_type, $currency, $oscTemplate;

      $content_width = MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_CONTENT_WIDTH;


      if ($cart->count_contents() > 0) {

      $call_to_checkout .= '<div class="alert alert-danger text-center call_to_checkout" role="alert">' . sprintf(TEXT_CALL_TO_CHECKOUT, $cart->count_contents()) . '  ' . tep_draw_button(IMAGE_BUTTON_CALL_TO_CHECKOUT, 'fa fa-thumbs-o-up fa-lg', tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'), 'primary', NULL, 'btn-success') . '</div>';
      }else{
      }

      ob_start();
      include(DIR_WS_MODULES . 'content/' . $this->group . '/templates/call_to_checkout.php');
      $template = ob_get_clean();

      $oscTemplate->addContent($template, $this->group);
    }

    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_STATUS');
    }

    function install() {
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Call to checkout Module', 'MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_STATUS', 'True', 'Do you want to enable the call to checkout Box content module?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Width', 'MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_CONTENT_WIDTH', '4', 'What width container should the content be shown in?', '6', '1', 'tep_cfg_select_option(array(\'12\', \'11\', \'10\', \'9\', \'8\', \'7\', \'6\', \'5\', \'4\', \'3\', \'2\', \'1\'), ', now())");
      tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
      tep_db_query("delete from configuration where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_STATUS', 'MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_CONTENT_WIDTH', 'MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_SORT_ORDER');
    }
  }

Step 2.

Create a file in catalog/includes/modules/content/header/templates called call_to_checkout.php and add the following code to it:

<div class="header col-sm-<?php echo $content_width; ?>">
  <?php echo $call_to_checkout; ?>
</div>

Step 3.

Create a file in catalog/includes/languages/english/modules/content/header called cm_header_call_to_checkout.php and add the following code to it:

<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2014 osCommerce

  Released under the GNU General Public License
*/

  define('MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_TITLE', 'Call to Checkout');
  define('MODULE_CONTENT_HEADER_CALL_TO_CHECKOUT_DESCRIPTION', 'Adds a call to checkout action into the Header Area of your site.');

Step 4.

Add the following to catalog/includes/languages/english.php

define('TEXT_CALL_TO_CHECKOUT', '<i class="fa fa-flag fa-lg"></i>  You have %s item(s) in your shopping basket, click to proceed through the checkout');

Then all you need to do is go to your admin and install the module, select the width and the sort order

 

Hopefully this will give people ideas on how to create further content modules.

 

Mark

Share this post


Link to post
Share on other sites

Very nice Mark!

 

I would like to see a tutorial with a bit more complicate function. Something that uses a database query and javascript/jquery and php.

I have a hard time to get my head around that... :wacko:

PupStar likes this

Share this post


Link to post
Share on other sites

Excellent @@PupStar awesome to see new code coming out in modules :)

 

1 small tip (not just for modules but for everywhere in osc):

 

 

tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL')

 

to

 

 

tep_href_link('checkout_shipping.php', '', 'SSL')
PupStar likes this

Share this post


Link to post
Share on other sites
1 small tip (not just for modules but for everywhere in osc):

 

@@burt is this because filenames.php is on the way out or for another reason?

 

Dan

Share this post


Link to post
Share on other sites

Dan, the next release, I think,  delete the FILENAME constant

 

also for your script : $call_to_checkout .=

is 

$call_to_checkout =

 

+

PupStar likes this

Share this post


Link to post
Share on other sites

We're deprecating two files:

 

/includes/database_tables.php

/includes/filenames.php

 

This change makes things more flexible in certain situations, less flexible in others.

Overall, for ease of installing addons.

 

It's time (for everyone) to get used to writing their links as so:  tep_href_link('conditions.php')

Share this post


Link to post
Share on other sites

In Step 4, you could add that text to the same file as Step 3...give it a go ?

 

I'm also wondering how this would look/react as a hook file rather than a CM file.  

Less flexible I think, less code also.  I'll see if I can recreate it.

PupStar likes this

Share this post


Link to post
Share on other sites

I think the hook cannot be desactivated and cannot use also to display until checkout_shipping for example.

I think also the hook can be call in module to activate and deactivate ? no ?

Share this post


Link to post
Share on other sites

In Step 4, you could add that text to the same file as Step 3...give it a go ?

 

@@burt

 

done and done :thumbsup:

 

Also I noticed I missed a line out of Step 3

define('IMAGE_BUTTON_CALL_TO_CHECKOUT', 'Checkout now');
Edited by PupStar

Share this post


Link to post
Share on other sites

Hi, ive managed to follow the steps above, but im not sure how to load a small javascript piece of code if making a module?

 

Im coding the Bootstrap carousel into a module, and need to code the following somewhere so that it can show up on the index page footer.

 

and editing that so that it will use the field which can be controlled from admin.

<script>' . '$(\'.carousel\').carousel({
  interval: ' . ( int ) MODULE_FRONT_PAGE_BANNER_ROTATOR_FADE_TIME . '
})' . "\n" .
'</script>';

Share this post


Link to post
Share on other sites

 

@@burt

 

done and done :thumbsup:

 

Also I noticed I missed a line out of Step 3

define('IMAGE_BUTTON_CALL_TO_CHECKOUT', 'Checkout now');

 

Hi, is there any advice on how to add javascript to be outputted?

 

Im using this code as part of a module, and it doesn't seem to output anything. I understand in template bottom, there is code which will output anything sent to $footer_script,

but can't get this code below to work.

 

Not sure if im messing something up!

if ($PHP_SELF == 'index.php' && $cPath == '') {
        // Set the Javascript to go in the header
        $footer_script = '<script>' . '$(\'.carousel\').carousel({
  interval: ' . ( int ) MODULE_FRONT_PAGE_BANNER_ROTATOR_FADE_TIME . '
})' . "\n" .
'</script>';

        $oscTemplate->addBlock($footer_script, 'footer_script');
Edited by vampirehunter

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now