Glossary Item Box

Squirrelcart v6.1.0

PHP Developer Tips

Overview

If you are a PHP developer, you might find some of the info below helpful when designing your own themes for Squirrelcart.

Referencing Theme Specific Variables in Your Template Files

Squirrelcart has a feature that allows you to associate merchant supplied data with a theme. You can use this to gather information that you might want to output somewhere in a template file, or to control some custom code entered in a template file. This feature allows you to enable up to 25 fields on a theme's record, which can be altered by a merchant and referenced by you in Squirrelcart's template files.

Adding Config Fields to a Theme's Record

  1. Open a theme's record
  2. Click the Advanced Config button, to display the Advanced Configuration field set:
  3. You can gather data using 5 different types of fields - text, HTML fields (via rich text editor), checkboxes, radio buttons, and select inputs.  For each type, you can gather up to 5 separate values for a total of 25 custom variables. Decide which type of field you want to use for the data you want to collect.
  4. For text variables, check Show Text Variables. For HTML, check Show HTML Variables. For all other field types, check Show Settings and then check the field below that corresponding to the type of field you wish to add to the theme's record. For this example, we will gather a greeting via a text field - so check the Show Text Variables field. A Text Variable Config fieldset will appear:
  5. Check the Text 1 Show field to tell Squirrelcart to show the 1st text config field on the theme's record. Enter a label for the field. For this example, enter Greeting:
  6. Click Save Changes 
  7. Open the theme's record again. You should see a section at the bottom labeled Variables. In the Text fieldset inside it you'll see a text field with the label Greeting:


    This field can now be used by a merchant after they've installed this theme.
  8. To set a default value for this variable, enter it in it's field. For this example, enter Hello there!
  9. Click Save Changes 

 

Now that you've added a custom variable to your theme, read the section below to find out how to access it's value.

Accessing Theme Variables in Template Files

All variables entered on a theme's record are available in the PHP array $SC['theme_config'].

Continuing with the example from the section above, we now have a theme that has a text field to gather a greeting to be used in this theme. The steps below explain how you can access the value of this variable for use in your template files. For this example, we will put the greeting on the storefront page right after the store logo.

If you haven't already done so, read the topic explaining how to properly modify template files before continuing.

 

  1. Open the store_main.tpl.php template file in an editor
  2. Locate the area you wish to add your code to. For this example, locate the store's logo code:
    <div id="sc_logo" class="tip" title="Click to go to our homepage">
         <a href="<?php print $Storefront_Home_URL ?>">
              <img src="<?php print $Logo_Image['dyn'] ?>" alt="<?php print $Logo_Image['alt'] ?>" width="<?php print $Logo_Image['width'] ?>" height="<?php print $Logo_Image['height'] ?>" />
         </a>
    </div>
  3. To print out the greeting the merchant has entered on their theme record, add a PHP print statement as follows:
    <div id="sc_logo" class="tip" title="Click to go to our homepage">
         <a href="<?php print $Storefront_Home_URL ?>">
              <img src="<?php print $Logo_Image['dyn'] ?>" alt="<?php print $Logo_Image['alt'] ?>" width="<?php print $Logo_Image['width'] ?>" height="<?php print $Logo_Image['height'] ?>" />
         </a>
         <h2><?php print $SC['theme_config']['Greeting'] ?></h2>
    </div>

Theme Setup File - pre_theme.php

How it Works

If you need a place to put your own custom PHP that is specifically related to a theme you are creating, the best place for it is the pre_theme.php file, which can be placed in squirrelcart/themes/YOUR_THEME/.

If your theme contains a pre_theme.php file, Squirrelcart will include that file before including any template files. You can put code in this file to setup special variables you might need access to in your templates.

 

Security for pre_theme.php

To ensure your pre_theme.php file is not loaded directly via a browser, we recommend adding this as the very first line after your opening PHP tag:

/* This line prevents direct access to template. Don't remove it. */ if (!defined('SC_INCLUDE_OK')) die;

Example

Here is an example of a pre_theme.php file that is used to set some theme_config variables to default values when they are not already set on the theme's record:

<?php
/*********************************************************************************************************************************

     pre_theme.php

     Purpose: this is not a template file, and should not be modified under normaly circumstances. This file
     sets up certain theme specific variables prior to including your template files.

*********************************************************************************************************************************/
/* This line prevents direct access to template. Don't remove it. */
if (!defined('SC_INCLUDE_OK')) die;

// setup variables for phone number and email
$SC['theme_config']['Phone_Number']  = $SC['settings']['Phone_Number'] ? sc_xml_safe($SC['settings']['Phone_Number']) : '888.555.1212';
$SC['theme_config']['Email_Address'] = $SC['settings']['Customer_Service_Email'] ? sc_xml_safe($SC['settings']['Customer_Service_Email']) : 'sales@example.com';
?>

 

 


© 2001-2017 Lighthouse Development. All Rights Reserved.