User Tools

Site Tools


how_to_create_a_customer_module

How to Create a Customer Module

Get Address

Prefill some or all fields in the checkout based on a updated field trigger.

~/includes/modules/customer/cm_get_address.inc.php
<?php
 
  class cm_get_address {
    public $id = __CLASS__;
    public $name = 'Get Address Module';
    public $description = '';
    public $author = 'ACME Corp.';
    public $version = '1.0';
    public $website = 'http://www.acme.com';
    public $priority = 0;
 
    public function get_address($data) {
 
      if (empty($this->settings['status'])) return;
 
      if (!isset($data['trigger']) || $data['trigger'] != 'tax_id')) return;
 
      // Do something so we can return some data
      // ...
 
      return array(
        'firstname' => '',
        'lastname' => '',
        'company' => '',
        'address1' => '',
        'address2' => '',
        'postcode' => '',
        'city' => '',
        'country_code' => '',
        'zone_code' => '',
      );
    }
 
    public function validate($data) {
 
      if (empty($this->settings['status'])) return;
 
      // Validate some data and return an error
      // ...
      //return array('error' => 'Your data is not valid');
 
      return true;
    }
 
    public function update($data) {
 
      if (empty($this->settings['status'])) return;
 
      // The customer profile was saved. Do something with the saved data.
      // ...
    }
 
    function settings() {
 
      return array(
        array(
          'key' => 'status',
          'default_value' => '1',
          'title' => language::translate(__CLASS__.':title_status', 'Status'),
          'description' => language::translate(__CLASS__.':description_status', 'Enables or disables the module.'),
          'function' => 'toggle("e/d")',
        ),
        array(
          'key' => 'priority',
          'default_value' => '0',
          'title' => language::translate(__CLASS__.':title_priority', 'Priority'),
          'description' => language::translate(__CLASS__.':description_priority', 'Process this module in the given priority order.'),
          'function' => 'int()',
        ),
      );
    }
 
    public function install() {}
 
    public function uninstall() {}
  }
 
?>

Newsletter Service Module

Run some operations after a customer account is created or updated. In this example adding/removing a customer from an external newsletter service.

~/includes/modules/customer/cm_newsletter_service.inc.php
<?php
 
  class cm_newsletter_service {
    public $id = __CLASS__;
    public $name = 'Newsletter Service Module';
    public $description = '';
    public $author = 'ACME Corp.';
    public $version = '1.0';
    public $website = 'http://www.acme.com';
    public $priority = 0;
 
    public function update($data) {
 
      if (empty($this->settings['status'])) return;
 
      if (!empty($data['newsletter'])) {
        // Subscribe user to newsletter
        // ...
      } else {
        // Unsubscribe user to newsletter
        // ...
      }
    }
 
    function settings() {
 
      return array(
        array(
          'key' => 'status',
          'default_value' => '1',
          'title' => language::translate(__CLASS__.':title_status', 'Status'),
          'description' => language::translate(__CLASS__.':description_status', 'Enables or disables the module.'),
          'function' => 'toggle("e/d")',
        ),
        array(
          'key' => 'priority',
          'default_value' => '0',
          'title' => language::translate(__CLASS__.':title_priority', 'Priority'),
          'description' => language::translate(__CLASS__.':description_priority', 'Process this module in the given priority order.'),
          'function' => 'int()',
        ),
      );
    }
 
    public function install() {}
 
    public function uninstall() {}
  }
 
?>
how_to_create_a_customer_module.txt · Last modified: 2016/02/09 03:22 by admin