Woocommerce集成设置API


Woocommerce Integration Settings API

我不确定这是否需要在WooCommerce, Wordpress或PHP问题,我也不知道该怎么称呼它,因为我不知道出了什么问题,但希望你们中的一个braniacs可以帮助我在这里。

我正在做一个WooCommerce集成。下面是我的代码(为了便于阅读):

if ( ! class_exists( 'WC_My_Integration' ) ) :
    class WC_My_Integration extends WC_Integration {
    public function __construct() {
        global $woocommerce;
        $this->id = 'my_id';
        $this->method_title       = __( 'My Title', 'my-text-domain' );
        $this->test_select = $this->get_option('test_select');
        $this->init_form_fields();
        add_action( 'woocommerce_update_options_integration', array( &$this, 'process_admin_options' ) );
        // If I don't do this the changes aren't showing immediately
        $this->process_admin_options();
        var_dump($this->test_select);  // Returns correct result on save, but then disappears after page reload 
    }
    public function init_form_fields() {
        $this->form_fields = array(
            'test_select'   => array(
                'title'       => __( 'Test Select', 'my-text-domain' ),
                'type'        => 'select',
                'options'     => array(
                    'yes' => 'Yes',
                    'no'  => 'No'
                )
            ),
        );
    }
    public function test_function() {
        if('yes' === $this->test_select) {
            echo "Yes";
        } else {
            echo "No";
        }
    }
    public function process_admin_options() {    
        parent::process_admin_options();
        $this->test_select = get_option('test_select');
    }
    public function admin_options() {
        parent::admin_options();
    }
}
endif;

问题是当我将选择选项更改为是/否时,更改仅反映在我重新加载页面后。

所以,换句话说,假设选择选项被设置为"yes"。我将其更改为"no",点击保存按钮,页面重新加载。表单显示现在选择了"no",并且var_dump()的值为"string 'no' (length=2)"。text_function()的预期输出现在应该是"no"。但是,它仍然显示"是"。

然后,当我刷新页面时,test_function()的结果是正确的,它回显"no",但只有在我刷新页面之后。有人能说明一下这件事吗?

我在我的插件文件中正确初始化插件。不知道我是否需要把这个也包括进去?

更新:

我已经更新了上面的代码,以反映我在Magnavode回答后的更改。现在的问题是,保存后的值是正确的,但然后消失后页面重新加载

您所拥有的错误是因为在您的构造器被称为process_admin_options()的时候还没有被调用,所以发布的表单还没有保存到数据库中。导致get_option获取旧值。

您可以通过在处理post数据后再次设置类变量来修复它。像这样覆盖process_admin_options:

public function process_admin_options() {
    parent::process_admin_options();
    $this->test_select = $this->get_option('test_select');
}

或者干脆放弃类变量,每次只调用get_option

你还应该覆盖init_form_fields()并从那里初始化你的表单字段。

你的admin_options()也隐藏了父的admin_options()。用这个代替:

public function admin_options() {
    parent::admin_options();
    var_dump($this->test_select);
    var_dump($this->get_option('test_select'));
}

我的目标是根据另一个值显示/隐藏一些设置。由于默认情况下init_form_fields()process_admin_options()之前被调用,显示/隐藏在保存设置后不工作,但在设置页面重新加载后。我已经通过重写process_admin_options()实现了目标:

public function process_admin_options() {
    $saved = parent::process_admin_options();
    $this->init_form_fields();
    return $saved;
}

和增加init_settings()呼叫init_form_fields():

public function init_form_fields() {
    $this->init_settings();
    $this->form_fields = [];
    if ( ! empty( $this->settings['one'] ) ) {
        // build a depending settings
    }
}

我确信这不是这样做的方式,但是我没有选择,直到有人能进一步阐明这一点。

最后,我在admin_options()函数中添加了以下代码:
if($this->test_select !== $this->get_option('test_select')) {
    header("Refresh:0");
}

如果两个(完全相同的!!)变量不匹配,则强制页面重新加载。显然,它需要检查所有选项,但这是基本的想法。