1

正如标题所说,我正在使用插件中的设置模型从组件访问设置,但得到空白结果而不是当前存储在数据库中。这就是我正在做的事情。首先这是我的fields.yaml文件定义:

fields:
    sandbox_enabled:
      label: Activar modo Sandbox?
      type: checkbox
      default: false
    ppal_fee:
      label: Cargo de PayPal
      type: text
    ppal_discount:
      label: Descuento por transferencias
      type: text
    ppal_app_id_sandbox:
      label: PayPal Sandbox App ID (*)
      type: text
    ppal_developer_account:
      label: PayPal Developer Account Email Address (*)
      type: text
    ppal_api_username_sandbox:
      label: PayPal Username API Credentials (Sandbox) (*)
      type: text
    ppal_api_password_sandbox:
      label: PayPal Password API Credentials (Sandbox) (*)
      type: text
    ppal_api_signature_sandbox:
      label: PayPal Signature API Credentials (Sandbox) (*)
      type: text
    ppal_rest_client_id_sandbox:
      label: PayPal REST API Username Credentials (Sandbox) (*)
      type: text
    ppal_rest_client_secret_sandbox:
      label: PayPal REST API Secret Credentials (Sandbox) (*)
      type: text

二、alomicuba.recharge.settings在DB记录值:

{
     "sandbox_enabled":"1",
     "ppal_fee":"0.029",
     "ppal_discount":"0.30",
     "ppal_app_id_sandbox":"",
     "ppal_developer_account":"",
     "ppal_api_username_sandbox":"",
     "ppal_api_password_sandbox":"",
     "ppal_api_signature_sandbox":"",
     "ppal_rest_client_id_sandbox":"",
     "ppal_rest_client_secret_sandbox":""
  }

最后,我如何获取组件中的设置值:

public function onAmountChange()
{
    // The user amount
    $amount = post('amount');

    if (empty($amount))
    {
        throw new \Exception(sprintf('Por favor introduzca un valor.'));
    }

    $toReload = $amount - ($amount * Settings::get('ppal_fee') - Settings::get('ppal_discount'));
    $this->page["confirmation_text"] = $toReload;
}

但它不起作用,因为这两个值Settings::get('ppal_fee')Settings::get('ppal_discount')空的,为什么?我做错了什么?

4

1 回答 1

1

您是否在顶部指定了以下代码行?

use Alomicuba\Recharge\Models\Settings;

如果您已指定,请尝试使用以下代码行:

$settings = Settings::instance();

$settings->ppal_fee;

整个功能代码看起来像这样

public function onAmountChange()
{
    // The user amount
    $amount = post('amount');

    if (empty($amount))
    {
        throw new \Exception(sprintf('Por favor introduzca un valor.'));
    }
    $settings = Settings::instance();

    $toReload = $amount - ($amount * $settings->ppal_fee - $settings->ppal_discount);
    $this->page["confirmation_text"] = $toReload;
}

参考https://octobercms.com/docs/plugin/settings#reading-settings

于 2014-10-15T06:45:54.740 回答