Jump to content



Photo
- - - - -

How to build a content module


  • Please log in to reply
11 replies to this topic

#1   PupStar

PupStar
  • Members
  • 902 posts
  • Real Name:Mark
  • Gender:Male

Posted 18 January 2015 - 15:16

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....-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()) . '&nbsp;&nbsp;' . 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>&nbsp;&nbsp;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


Bootstrap 3 ROCKS!

(or it will if I ever get my head around it lol)

(and I think I have now (w00t) )


#2   Tsimi

Tsimi
  • Members
  • 1,752 posts
  • Real Name:Lambros
  • Gender:Male
  • Location:Japan

Posted 19 January 2015 - 07:03

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:



#3   burt

burt

    I drink and I know things

  • Community Team
  • 12,438 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 19 January 2015 - 09:09

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')

This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#4   Dan Cole

Dan Cole
  • Community Sponsor
  • 1,640 posts
  • Real Name:Dan Cole
  • Gender:Male
  • Location:Ontario, Canada

Posted 19 January 2015 - 12:40

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



#5   Gyakutsuki

Gyakutsuki
  • Members
  • 595 posts
  • Real Name:Loic Richard
  • Gender:Male
  • Location:Montreal

Posted 19 January 2015 - 16:33

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

 

also for your script : $call_to_checkout .=

is 

$call_to_checkout =

 

+


Regards

 

-----------------------------------------

Loïc

Contact me by skype for business

Contact me @gyakutsuki for an answer on the forum


#6   burt

burt

    I drink and I know things

  • Community Team
  • 12,438 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 19 January 2015 - 17:20

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')


This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#7   burt

burt

    I drink and I know things

  • Community Team
  • 12,438 posts
  • Real Name:G Burton
  • Gender:Male
  • Location:UK/DEV/on

Posted 19 January 2015 - 17:43

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.


This is a signature that appears on all my posts.  It is not specifically aimed at you.

 

IF YOU MAKE A POST REQUESTING HELP...please state the exact version of osCommerce that you are using. THANKS
 
If you are still on the old style osCommerce, it is time to move to Responsive.

 


#8   Gyakutsuki

Gyakutsuki
  • Members
  • 595 posts
  • Real Name:Loic Richard
  • Gender:Male
  • Location:Montreal

Posted 19 January 2015 - 17:56

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 ?


Regards

 

-----------------------------------------

Loïc

Contact me by skype for business

Contact me @gyakutsuki for an answer on the forum


#9   PupStar

PupStar
  • Members
  • 902 posts
  • Real Name:Mark
  • Gender:Male

Posted 19 January 2015 - 19:13

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, 19 January 2015 - 19:14.

Bootstrap 3 ROCKS!

(or it will if I ever get my head around it lol)

(and I think I have now (w00t) )


#10   vampirehunter

vampirehunter
  • Members
  • 700 posts
  • Real Name:vampire

Posted 23 January 2015 - 19:18

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>';


#11   vampirehunter

vampirehunter
  • Members
  • 700 posts
  • Real Name:vampire

Posted 24 January 2015 - 11:56

 

@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, 24 January 2015 - 12:02.


#12   vampirehunter

vampirehunter
  • Members
  • 700 posts
  • Real Name:vampire

Posted 24 January 2015 - 12:38

Nevermind, i got it fixed. it should be footer_scripts not footer_script.