Perlshop 4 - Plugin Documentation

Contents


Overview

In its basic configuration, Perlshop expects that each catalog page will be represented by an individual static html file. Perlshop Plugins extend the capabilities of Perlshop 4 by allowing Perlshop to call optional resources that generate catalog page content on demand.


An Example of a Custom Plugin

A primary use case for a Perlshop Plugin is Serpentine Music, an on-line music store with dozens of titles. The entire inventory of the store is maintained by the owner as a MacIntosh FoxPro database, and not as individual web pages. When the inventory is updated, the owner exports the database as a flat ASCII file, and transfers this file to the web server.

This is where a custom Perlshop Plugin comes into play.

The home page of the store provides a search interface that allows customers to look for items that interest them. This interface calls a Plugin that searches through the database file, finds all items that meet the search criteria, and generates Perlshop catalog page html as a response. This html is read in by Perlshop 4, which then treats it as if it had come from a static html file on disk. The customer simply sees a catalog page that looks like any other.

By mixing static html pages with pages generated by Perlshop Plugins, an ecommerce web site can be given dynamic content without sacrificing any existing capabilities.



Plugin Types

There are four types of Perlshop plugins:


External Plugins

External plugins are stand-alone programs that Perlshop uses to generate web page content on demand. The Serpentine Music plugin described in the example above is an external plugin, as is the Calendar Plugin which is included in the Perlshop 4 standard release package. External plugins may be activated in response to an internal Perlshop event, or by a call from a catalog web page.


Internal Plugins

Internal plugins are custom built Perl language modules that are automatically connected into Perlshop as they are needed. The Address Verification plugin, available as a free download, is an internal plugin. Internal plugins are activated in reponse to internal Pelshop events.


File Plugins

File plugins are not composed of executable software, and are not expected to generate an entire web page. Instead, this type of plugin is used as a way to insert customized pieces of html code into the web pages generated by Perlshop, such as the Order Form page and the Shipping Rates page. The order_form_as (Order Form, After Shipping) file plugin is included as a standard part of Perlshop 4.2.08 or later. File plugins are activated in reponse to internal Pelshop events.


Text Plugins

Text plugins are just like file plugins, save that the text involved comes from data in the plugins table, and not from an external file. Text plugins are activated in reponse to internal Pelshop events.



The Plugins Registration Table

All Perlshop Plugins must be registered with Perlshop before they can be used. Registration is a simple matter of adding the plugin to the %plugins table found in the ps.cfg file.

Each entry in this table is defines one plugin. Each entry has a unique name, and one or more information attributes.

The following example defined an external plugin whose name is calendar, and whose executable file name is ps_plugin_gencal.pl.

%plugins =
(
	# Calendar example plugin
	'calendar'  => 
	{
		'program'	=> 'ps_plugin_gencal.pl'
	}
);

The information fields required for each plugin type are explained in detail later on in this document.

There is no limit on the number of plugins that may be registered with Perlshop 4.



External Plugins


Defining an External Plugin

External plugins may be defined in the Plugin Registration table using the following data attributes:

Attribute Required Default Value Description
program Yes None This is the name of the executable file for the plugin. This file is expected to be located in the same directory as the perlshop.cgi file.
background No 'no' This optional field is available on Unix systems only, and is used to indicate that the plugin is intended for use in background mode. Legal values for this attribute are 'yes' and 'no'.

This field is applied to event driven plugin calls only.

display No 'yes' This optional field is used to determine whether or not the output of the plugin is to be added to the html of the web page when the plugin is executed. Legal values for this attribute are 'yes' and 'no'.

This field is applied to event driven plugin calls only.

event No One or more event names This optional field specifies the event(s) that will activate this plugin.
condition No None This optional field specifies the logical conditions that are required to activate this plugin. The condition must be expressed as a Perl language statement. This statement will be executed via Perl eval() function call at the time that the plugin is called. If the statement evaluates to true, the plugin is allowed to execute. If the statement evaluates to false, no further actions are taken.

This field is applied to event driven plugin calls only.

An Example:
This example defines an external plugin named calendar. This external program file for this plugin is ps_plugin_gencal.pl. This program will not run in background mode. The output from this program will be included in the web page display. No events are associated with this plugin.

%plugins =
(
	# Calendar example plugin
	'calendar'  => 
	{
            'program'    => 'ps_plugin_gencal.pl',
            'background' => 'no',
            'display'    => 'yes'
	}
);


Calling External Plugins from Catalog Web Pages

External plugins have a characteristic that makes them different from the other plugin types. In addition to being called in response to an internal Perlshop event, external plugins can also be called directly from a Perlshop catalog web page.

When used this way, an external plugin can be thought of as a Perlshop built-in command, like the View Cart command or the Check Out command. The customer clicks on a hyperlink or a form button, and Perlshop is called to generate and present a new page. Instead of retrieving the content of the new page from a disk file, Perlshop calls upon the plugin program to dynamically generate the HTML for the new web page.

The plugin program then generates an HTML text stream, which Perlshop receives and processes in exactly the same way as it would the HTML from a static web page file:


External Plugin Parameters

The following parameters are used when calling Perlshop 4 Plugins:


Calling an External Plugin Using a Hyperlink

<a href="!MYURL!?action=plugin&plugin=searchplug&plugin_param1=keyword&order_id=!ORDERID!>
Click Me</a>


Calling an External Plugin Using a Form

<form name="searchForm" method="post" action="!MYURL!">

<input type=hidden name="order_id" value="!ORDERID!>
<input type=hidden name="action" value="plugin">
<input type=hidden name="plugin" value="searchplug">
Enter a thing to search for : 
<input type=text   name="plugin_param1">
<input type=submit value="Search Me">

</form>


Creating External Plugins to be Called from Catalog Pages

External plugins can be written in any programming language. The only rules an External Plugin needs to follow are these:


Creating Event Driven External Plugins

External plugins can be written in any programming language. The only rules an External Plugin needs to follow are these:

Event driven external plugins must accept the following three command line parameters:

  1. The name of the event that triggered the plugin, passed as a string value.
  2. The order ID number associated with the shopping cart, passed as a string value.
  3. The name of the catalog page file being loaded, passed as a string.
    This parameter is only used with the before_page_load and after_page_load events.


The Calendar Sample Plugin

Perlshop 4 is distributed with the external Calendar Plugin included. This plugin is a simple Perl program that generates a simple calendar using the Unix cal program. The Calendar plugin takes the output of the cal program and produces the html for a simple web page.

An Example:



Internal Plugins


Defining an Internal Plugin

Internal plugins may be defined in the Plugin Registration table using the following data attributes:

Attribute Required Default Value Description
module Yes None This is the name of the Perl module used for the plugin. The module file is expected to be located in the same directory as the perlshop.cgi file.
display No 'yes' This optional field is used to determine whether or not the output of the plugin is to be added to the html of the web page when the plugin is executed. Legal values for this attribute are 'yes' and 'no'.
event No One or more event names This optional field specifies the event(s) that will activate this plugin.
condition No None This optional field specifies the logical conditions that are required to activate this plugin. The condition must be expressed as a Perl language statement. This statement will be executed via Perl eval() function call at the time that the plugin is called. If the statement evaluates to true, the plugin is allowed to execute. If the statement evaluates to false, no further actions are taken.

This field is applied to event driven plugin calls only.

An Example:
This example defines an internal plugin named addver. The external Perl module file used for this plugin is called PS_AddVer.pm. The output from this plugin will be included in the web page display. This plugin will be triggered by the before_checkout_cart_contents event.

%plugins =
(
	# Address Verification Display plugin
	'addver' =>
	{
		'module'  => 'PS_AddVer.pm',
		'display' => 'yes',
		'event'   => 'before_checkout_cart_contents'
	}
}


Creating your own Internal Plugins

Internal plugins must be created as Perl language modules. The module must contain a standard interface method named Dispatch, which which must accept the following three parameters:
  1. The name of the event that triggered the plugin, passed as a string value.
  2. The order ID number associated with the shopping cart, passed as a string value.
  3. The name of the catalog page file being loaded, passed as a string.
    This parameter is only used with the before_page_load and after_page_load events.

Internal plugins must also follow these rules:

See the source code of the free Address Verification plugin for coding standards and examples.



File Plugins


Defining a File Plugin

File plugins may be defined in the Plugin Registration table using the following data attributes:

Attribute Required Default Value Description
file Yes None This is the name of the file that contains the text to be used. The file is expected to be located in the plugins/files subdirectory.
display No 'yes' This optional field is used to determine whether or not the output of the plugin is to be added to the html of the web page when the plugin is executed. Legal values for this attribute are 'yes' and 'no'.
event No One or more event names This optional field specifies the event(s) that will activate this plugin.
condition No None This optional field specifies the logical conditions that are required to activate this plugin. The condition must be expressed as a Perl language statement. This statement will be executed via Perl eval() function call at the time that the plugin is called. If the statement evaluates to true, the plugin is allowed to execute. If the statement evaluates to false, no further actions are taken.

This field is applied to event driven plugin calls only.

An Example:
This example defines a file plugin named order_form_as. The plugin uses text from file plugins/files/aftershipping.inc, and will be triggered by the order_form_after_shipping_address event..

This has the effect of inserting the contents of the given file into the Order Form page, just after the Shipping Address section.

%plugins =
(
	'order_form_as'	=>
	{
		'file'    => 'plugins/files/aftershipping.inc',
		'display' => 'yes',
		'event'   => 'order_form_after_shipping_address'
	}
);



Text Plugins


Defining a Text Plugin

Text plugins may be defined in the Plugin Registration table using the following data attributes:

Attribute Required Default Value Description
text Yes None This attribute contains the text to be inserted into the web page.
display No 'yes' This optional field is used to determine whether or not the output of the plugin is to be added to the html of the web page when the plugin is executed. Legal values for this attribute are 'yes' and 'no'.
event No One or more event names This optional field specifies the event(s) that will activate this plugin.
condition No None This optional field specifies the logical conditions that are required to activate this plugin. The condition must be expressed as a Perl language statement. This statement will be executed via Perl eval() function call at the time that the plugin is called. If the statement evaluates to true, the plugin is allowed to execute. If the statement evaluates to false, no further actions are taken.

This field is applied to event driven plugin calls only.

An Example:
This example defines a text plugin named shipping_footer. The text <b>This the footer of the shipping page!</b><br> will be included in the web page display. This plugin will be triggered by the before_page_footer event, and will execute only if the condition

q($action eq 'SHIPPING RATES')
is true.

This has the effect of inserting the text of this plugin above the page footer of the Shipping Rates page.

%plugins =
(
	'shipping_footer'	=>
	{
		'text'      => '<b>This the footer of the shipping page!</b><br>',
		'display'   => 'yes',
		'event'     => 'before_page_footer',
		'condition' => q($action eq 'SHIPPING RATES')
	}
);



The Perlshop 4 Event Driven Plugin Model

Perlshop 4 can be configured to trigger plugins in response to various internal events. These events can be used to insert HTML page content into a web page, to process data, to execute some logical function, or all of the above.

The following tables list the internal Perlshop events that may be used to trigger plugins.

Events described as Data Processing are useful for internal and external plugins only, and are not intended for page content insertion.


General Content Insertion Events:

Event Name Category Description
above_page_footer Content Insertion This event is invoked for every page generated by Perlshop, and is used to insert content just above the page footer.
below_page_footer Content Insertion This event is invoked for every page generated by Perlshop, and is used to insert content just below the page footer.

Plugins that want to insert page content in response to this event should leave their display attributes set to 'no', as Perlshop will handle their inclusion automatically.

header_above_banner Content Insertion This event is invoked for every page generated by Perlshop, and is used to insert content just above the Perlshop page banner.

Plugins that want to insert page content in response to this event should leave their display attributes set to 'no', as Perlshop will handle their inclusion automatically.

header_below_banner Content Insertion This event is invoked for every page generated by Perlshop, and is used to insert content just below the Perlshop page banner.

Plugins that want to insert page content in response to this event should leave their display attributes set to 'no', as Perlshop will handle their inclusion automatically.



Events Associated with Catalog Page Loading:

Event Name Category Description
after_page_load Data Processing This event is invoked when any catalog page is loaded by Perlshop, and after any internal Perlshop logic has executed.
before_page_load Data Processing This event is invoked when any catalog page is loaded by Perlshop, and before any internal Perlshop logic has executed.



Events Associated with the Perlshop Add Action:

These events triggered when new items are added to a shopping cart. Any one of the following synonymous Perlshop actions will have this effect: Add, Buy, Order, Purchase, Put, or Register.

Event Name Category Description
addaction Data Processing This event is invoked when the Add action is executed by Perlshop. If any plugins are defined to use this event, those plugins will be called in place of the standard internal Perlshop logic for processing the Add action.
after_add_item Data Processing This event is invoked when the Add action is executed by Perlshop, and after any internal Perlshop logic has executed.
before_add_item Data Processing This event is invoked when the Add action is executed by Perlshop, and before any internal Perlshop logic has executed.
cart_remove_item Data Processing This event is invoked once for each item removed from the shopping cart.

Available in version 4.5.02 and later.



Events Associated with the Perlshop Check Out Action:

Event Name Category Description
before_check_out Data Processing This event is invoked when the Check Out action is executed by Perlshop.

Available in version 4.3.03 and later.

before_display_order_form
order_form_before_billing_address
order_form_after_billing_address
order_form_before_shipping_address
order_form_after_shipping_address
order_form_before_payment_data
order_form_after_payment_data
order_form_before_shipping_data
order_form_after_shipping_data
order_form_before_comments
order_form_after_comments
after_display_order_form
Content Insertion These events are associated with the Order Form page, and are intended for use for content insertion. The names of the events indicate the locations on the order form where the content insertions will take place.



Events Associated with the Perlshop Enter Action:

These events triggered when a customer enters the store for the first time. Any one of the following synonymous Perlshop actions will have this effect: Enter, QuickBuy, or Go To.

Event Name Category Description
after_enter_shop Data Processing This event is invoked when the Enter action is executed by Perlshop, and after any internal Perlshop logic has executed.
orderid Data Processing This event is invoked when the Enter action is executed by Perlshop, and is used to call a plugin that will override the standard Perlshop 4 Order ID generation algorithm.

Available in version 4.4.02 and later.



Events Associated with the Perlshop Place Order Action:

Event Name Category Description
after_compute_invoice Data Processing This event is invoked in response to the Place Order command, after all shipping and handling fees have been calculated.
after_place_order Data Processing This event is invoked when the Place Order action is executed by Perlshop, and after all internal Perlshop logic has executed.
before_place_order Data Processing This event is invoked when the Place Order action is executed by Perlshop, and before any internal Perlshop logic has executed.
invoice_page_above_print Content Insertion This event is invoked when the Place Order action is executed by Perlshop, and is processed while the invoice text is being generated. The text produced by this plugin will be inserted above the Print This Invoice button.

Available in version 4.3.02 and later.

invoice_page_above_continue Content Insertion This event is invoked when the Place Order action is executed by Perlshop, and is processed while the invoice text is being generated. The text produced by this plugin will be inserted above the Return to Home Page button.

Available in version 4.3.02 and later.

invoice_page_footer Content Insertion This event is invoked when the Place Order action is executed by Perlshop, and is processed while the invoice text is being generated. The text produced by this plugin will be inserted at the top of the invoice display page.

Available in version 4.3.02 and later.

invoice_page_header Content Insertion This event is invoked when the Place Order action is executed by Perlshop, and is processed while the invoice text is being generated. The text produced by this plugin will be inserted at the bottom of the invoice display page.

Available in version 4.3.02 and later.

item_note Content Insertion This event is invoked once for each item displayed in the shopping cart contents table. It is used to insert customized content into the description column for a given item in the cart. This event is intended for use by web sites that implement their own custom content plugins.

Available in version 4.5.00 and later.



Events Associated with the Perlshop Restart Action:

Event Name Category Description
restart Data Processing This event is invoked in response to the Restart command.

Available in version 4.5.02 and later.



Events Associated with the Perlshop Shipping Rates Action:

Event Name Category Description
after_shipping_table
before_shipping_table
Content Insertion These events are associated with the Shipping Rates Display page, and are intended for use for content insertion. The names of the events indicate the locations on the order form where the content insertions will take place.

Note: These events are supported as of Perlshop v4.3.01.



Events Associated with the Perlshop Submit Action:

These events are associated with the action of submitting the customer information form.

Event Name Category Description
after_submit_customer Data Processing This event is invoked when the Submit action is executed by Perlshop, and after any internal Perlshop logic has executed.
before_submit_customer Data Processing This event is invoked when the Submit action is executed by Perlshop, and before any internal Perlshop logic has executed.



Events Associated with the Perlshop View Cart Action:

Event Name Category Description
after_compute_cart Data Processing This event is invoked in response to the View Cart command, after all shipping and handling fees have been calculated.
before_checkout_cart_contents Data Processing/Content Insertion This event is invoked when the final checkout View Cart action is executed by Perlshop, just above the table used to display the shopping cart contents.

Plugins that want to insert page content in response to this event must have their display attributes set to 'yes'.

before_simple_cart_contents Data Processing/Content Insertion This event is invoked when the normal View Cart action is executed by Perlshop, just above the table used to display the shopping cart contents.

Plugins that want to insert page content in response to this event must have their display attributes set to 'yes'.

before_view_checkout_cart Data Processing This event is invoked when the final checkout View Cart action is executed by Perlshop, and before any internal Perlshop logic has executed.
before_view_simple_cart Data Processing This event is invoked when the normal View Cart action is executed by Perlshop, and before any internal Perlshop logic has executed.
item_note Content Insertion This event is invoked once for each item displayed in the shopping cart contents table. It is used to insert customized content into the description column for a given item in the cart. This event is intended for use by web sites that implement their own custom content plugins.

Available in version 4.5.00 and later.



Events Associated with Invoice Email:

Event Name Category Description
add_to_company_email Content Insertion This event is used to insert information into the company data section of the store copy of the invoice email.

Available in version 4.4.00 and later.

invoice_email_header_html Content Insertion This event is used to insert content at the top of the text generated for invoice email.

This event is used with html mode email only.

Available in version 4.3.02 and later.

invoice_email_footer_html Content Insertion This event is used to insert content at the bottom of the text generated for invoice email.

This event is used with html mode email only.

Available in version 4.3.02 and later.

invoice_email_header_plain Content Insertion This event is used to insert content at the top of the text generated for invoice email.

This event is used with plain-text mode email only.

Available in version 4.3.02 and later.

invoice_email_footer_plain Content Insertion This event is used to insert content at the bottom of the text generated for invoice email.

This event is used with plain-text mode email only.

Available in version 4.3.02 and later.



General Data Processing Events:

Event Name Category Description
before_execute_action Data Processing This event is invoked in response to any Perlshop action at all except for Enter, and before any action specific internal Perlshop logic has executed.
compute_discount Data Processing This event is invoked whenever Perlshop needs to perform the discount calculation on the contents of the shopping cart. This event is intended for use by web sites that implement their own discount computation plugins.

This plugin interface is simple to use:

  1. Set your $discount_type setting to 'plugin'.
  2. Add your custom discount plugin to the plugins definition table. Set it to trigger on the 'compute_discount' event.
The discount value for the shopping cart will be set to the value returned by the plugin.

Available in version 4.5.00 and later.




Using the Provided Plugin Files

Using the "After Shipping" Order Page Plugin

This example will work with Perlshop 4.3.00 and later.

The order_form_after_shipping_address event is used to add custom content into the Order Form page. This event will insert custom content just below the shipping address fields on the order form.

The order_form_as file plugin uses this event to insert the contents of the aftershipping.inc file into the order form page. In order to use this plugin, the following text would be added to the plugin registration table in the ps.cfg file:

%plugins = ( 
	'order_form_as'   =>
	{
		'file'      =>	'plugins/files/aftershipping.inc',
		'event'     =>	'order_form_after_shipping_address',
		'display'   =>	'yes'
	},
);

Using the Shipping Weight Display Plugin

This example will work with Perlshop 4.3.01 and later.

The before_shipping_table event is used to add custom content into the Shipping Rates page. This event will insert the custom content just above the table that displays the various shipping rates.

This example shows how the before_shipping_table content insertion event can be used to display the total weight of the current shopping cart on the Shipping Rates page. The following text would be added to the plugin registration table in the ps.cfg file:

%plugins = ( 
	'before_shipping'	=>
	{
		'file'      => 'plugins/files/shippingweight.inc',
		'event'     => 'before_shipping_table',
		'display'   => 'yes',
		'eval'      => 1,
		'condition' => 'LoadOrders(1)'
	}
);

The file plugin used in this example is included in the Perlshop 4 zip file.




Defining Custom Plugin Events

The ability to define your own plugin events was added to Perlshop version 4.3.01.
This new feature allows you to add plugin driven content anywhere on any of your catalog web pages.
The concept behind this idea is simple:


Step 1: Naming Your Custom Event

The first thing that you must do is create a name for your new event. The name must be composed of alphabetic characters, and may not be a duplicate of any of the event names listed in the tables earlier in this document. This name will be used in the <--#plugin event --> tags that you will place in your catalog page files.


Step 2: Adding Your Plugin

The second thing that you must do is select a name for your plugin. This name will be used to register your plugin with Perlshop. See The Plugins Registration Table section of this document for details. You then use this name to add your plugin to the plugins table in the ps.cfg file.


Step 3: Using Your Plugin

You use the new <--#plugin event --> tag inside your catalog pages. You can use it as many times as you like in a given page. All you need to do is replace the word event with the name of the new event you've defined.

For example, if you new event is named fred, your tag would look like this:
<--#plugin fred -->


A Simple Example: Inserting Text

This example demonstrates a simple little plugin that inserts the word "Howdy!" whenever it is called. Could you do the same thing with an SSI #include command? Sure you could.

For this example, we've defined a new event named display_howdy, and have registered a plugin named howdy to work with it. The display setting for this plugin is turned off because the <--#plugin event --> tag performs the content insertion automatically.

The plugin will be actived anywhere on any catalog web page where the following tag is found:
<!--#plugin display_howdy -->

%plugins = 
( 
	'howdy'	=>
	{
		'text'      => 'Howdy!',
		'event'     => 'display_howdy',
		'display'   => 'no'
	}
);


A Complex Example: Displaying a Shopping Cart Total

This example demonstrates a complex plugin that inserts information describing the current shopping cart contents into a catalog page. The result will be text in this form:

Items in Cart : 3
Subtotal : $73.45

For this example, we've defined a new event named display_cart_contents, and have registered a plugin named cart_contents to work with it.

Please note the following:

The plugin will be actived anywhere on any catalog web page where the following tag is found:
<!--#plugin display_cart_contents -->

%plugins = 
( 
	'cart_contents'	=>
	{
		'text'      => '',
		'event'     => 'display_cart_contents',
		'display'   => 'no',
		'condition' =>	
q(
LoadOrders(1);
$plugins{'cart_contents'}->{'text'} = 
    "Items in Cart : $total_quantity<br>\nSubtotal : \$" . 
    sprintf('%1.2f', $total_price) . "<br>\n";
)
	}
);