即使选项已保存,WordPress get_option有时会返回空字符串

时间:2017-02-01 10:04:59

标签: php wordpress woocommerce

我正在开发一个WooCommerce支付网关,并且我使用get_option来检索用户定义的选项。用户可以指定的选项之一是结帐页面上显示的付款方式的名称。在WooCommerce中,有一个名为getMethodTitle()的方法,它应该返回它。

我的样子如下:

public function getMethodTitle()
{
    $title = $this->get_option($this->getMethodId() . '_title');

    if (is_null($title) || $title === '')
    {
        return __('My Default Value', My_Plugin_Class::PLUGIN_ID);
    }

    return __($title, My_Plugin_Class::PLUGIN_ID);
}

现在,$this->get_option($this->getMethodId() . '_title');由于某种原因总是返回'',即使这个值肯定保存在数据库中。

在另一个WooCommerce方法process_payment中,它可以正常工作。如果我使用var_dump($this->get_option($this->getMethodId() . '_title'));,我会得到正确的值。

有谁知道为什么?谢谢!

编辑:根据要求,提供有关该课程及其初始化方式的一些额外信息。我的插件不是添加一个,而是(目前)4个支付网关到WooCommerce,所以我的所有网关类都从另一个具有全局功能的类扩展,而后者又从另一个类扩展。基本上是:

My_Payment_Gateway extends My_Payment_Gateway_With_Webhook extends My_Abstract_Payment_Gateway extends WC_Payment_Gateway

构造函数是这样的:

class My_Payment_Gateway extends My_Payment_Gateway_With_Webhook
{
    public function __construct()
    {
        $this->supports = array(
            'products',
            'refunds'
        );

        $this->has_fields = true;
        $this->icon = false;
        $this->title = $this->getMethodTitle();

        parent::__construct();
    }

class My_Payment_Gateway_With_Webhook extends My_Abstract_Payment_Gateway
{
    public function __construct()
    {
        parent::__construct();

        add_action('woocommerce_api_'.strtolower('My_Payment_Gateway_With_Webhook'), array($this, 'webhook'));
    }

最后:

abstract class My_Abstract_Payment_Gateway extends WC_Payment_Gateway
{
    const PLUGIN_FOLDER_NAME = "xxxxxxxxxxx";

    const STATUS_PENDING    = 'pending';
    const STATUS_PROCESSING = 'processing';
    const STATUS_ON_HOLD    = 'on-hold';
    const STATUS_COMPLETED  = 'completed';
    const STATUS_CANCELLED  = 'cancelled';
    const STATUS_REFUNDED   = 'refunded';
    const STATUS_FAILED     = 'failed';

    public function __construct()
    {
        $this->plugin_id = '';
        $this->id = strtolower(get_class($this));
        $this->method_title = $this->getMethodTitle();
        $this->method_description = $this->getMethodDescription();
        $this->init_form_fields();
        $this->init_settings();

        $this->apiClient = new Client(My_Base_Plugin_class::getApiKey(), My_Base_Plugin_class::isSandboxEnabled());

        $this->enabled = $this->get_option($this->getMethodId().'_'.'enabled');
        if (!$this->is_available())
        {
            $this->enabled = false;
        }

        add_action('woocommerce_api_' . $this->id, array($this, 'webhookAction'));

        $this->accountFieldInit();

        if (is_admin())
        {
            add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
        }
    }

网关初始化如下:My_Main_Plugin_Class有一个名为$GATEWAYS的静态类数组和一个过滤器:

// Add payment gateways
add_filter('woocommerce_payment_gateways', array(__CLASS__, 'addGateways'));

最后是addGateways方法:

public function addGateways(array $gateways)
{
    return array_merge($gateways, self::$GATEWAYS);
}

0 个答案:

没有答案
相关问题