Page 1 of 1

Creating Switch in Customer Details - admin only

Posted: Sat Apr 06, 2024 5:22 am
by Portman
Hi,

I am trying to create a switch that I can set on a customers details that will allow me to activate a 30 day account payment option - at this stage all I am trying to do is create an active switch on the customers detail page (admin controlled only)

I copied and modified the newsletter switch, and it all looks good - shows up only in the admin customer page - but it does not save to the database... I have created a record in the database for it - copying the setup of the newsletter record... Here is my code, can anyone see what I have done wrong?

cd_30d_account.php

Code: Select all

<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/

  class cd_30d_account extends abstract_customer_data_module {

    const CONFIG_KEY_BASE = 'MODULE_CUSTOMER_DATA_30D_ACCOUNT_';

    const PROVIDES = [ '30 Day Account' ];
    const REQUIRES = [  ];

    protected function get_parameters() {
      return [
        static::CONFIG_KEY_BASE . 'STATUS' => [
          'title' => 'Enable 30 Day Account Switch Module',
          'value' => 'True',
          'desc' => 'Do you want to add the module to your shop?',
          'set_func' => "Config::select_one(['True', 'False'], ",
        ],
        static::CONFIG_KEY_BASE . 'GROUP' => [
          'title' => 'Customer data group',
          'value' => '3',
          'desc' => 'In what group should this appear?',
          'use_func' => 'customer_data_group::fetch_name',
          'set_func' => 'Config::select_customer_data_group(',
        ],
        static::CONFIG_KEY_BASE . 'PAGES' => [
          'title' => 'Pages',
          'value' => 'customers',
          'desc' => 'Only used on Admin Customers Page',
          'set_func' => 'Customers::select_pages(',
          'use_func' => 'abstract_module::list_exploded',
        ],
        static::CONFIG_KEY_BASE . 'SORT_ORDER' => [
          'title' => 'Sort Order',
          'value' => '3770',
          'desc' => 'Sort order of display. Lowest is displayed first.',
        ],
      ];
    }

    public function get($field, &$customer_details) {
      switch ($field) {
        case '30d_account':
          if (!isset($customer_details[$field])) {
            $customer_details[$field] = $customer_details['30d_account']
                                     ?? $customer_details['customers_30d_account'] ?? null;
          }

          return $customer_details[$field];
      }
    }

    public function display_input(&$customer_details = null) {
      $input = new Tickable('30d_account', ['value' => '1'], 'checkbox');

      if ($customer_details && is_array($customer_details)) {
        $input->tick(1 == $this->get('30d_account', $customer_details));
      }

      if ($this->is_required()) {
        $input->require();
      }

      include Guarantor::ensure_global('Template')->map(__FILE__);
    }

    public function process(&$customer_details) {
      $customer_details['30d_account'] = isset($_POST['30d_account']) ? Text::input($_POST['30d_account']) : false;

      if ( ( ('1' !== $customer_details['30d_account']) )
        && (!empty($customer_details['30d_account']) || $this->is_required())
         )
      {
        $GLOBALS['messageStack']->add_classed($GLOBALS['message_stack_area'] ?? 'customer_data', ENTRY_30D_ACCOUNT_ERROR);

        return false;
      }

      return true;
    }

    public function build_db_values(&$db_tables, $customer_details, $table = 'both') {
      Guarantor::guarantee_subarray($db_tables, 'customers');
      $db_tables['customers']['customers_30d_account'] = $customer_details['30d_account'];
    }

    public function build_db_aliases(&$db_tables, $table = 'both') {
      Guarantor::guarantee_subarray($db_tables, 'customers');
      $db_tables['customers']['customers_30d_account'] = '30d_account';
    }

    public function get_template() {
      return substr(__FILE__, strlen(DIR_FS_CATALOG));
    }

  }


tpl_cd_30d_account.php

Code: Select all

<?php
/*
  $Id$

  CE Phoenix, E-Commerce made Easy
  https://phoenixcart.org

  Copyright (c) 2021 Phoenix Cart

  Released under the GNU General Public License
*/
?>
  <div class="form-group row align-items-center">
    <div class="col-form-label col-sm-3 text-left text-sm-right"><?= ENTRY_30D_ACCOUNT ?></div>
    <div class="col-sm-9 pl-5 custom-control custom-switch">
      <?= $input->append_css('custom-control-input')->set('id', 'input30d_account') ?>
      <label for="input30d_account" class="custom-control-label text-muted"><small><?= ENTRY_30D_ACCOUNT_TEXT ?>&nbsp;</small></label>
    </div>
  </div>
Thanks for any help