Woocommerce集成设置API

维库斯

我不太确定是否需要在WooCommerce,Wordpress或PHP问题中使用它,我也不知道该怎么称呼,因为我不知道出了什么问题,但是希望你们其中的小伙子们可以在这里为我提供帮助。

我正在进行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;

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

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

然后,当我刷新页面时,test_function()的结果是正确的,并且回显“ no”,但仅在刷新页面之后。请问有人可以对此事发表一些看法吗?

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

更新:

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

维库斯

我敢肯定这不是做到这一点的方法,但是我别无选择,直到有人可以对此进一步阐明。

我最终在admin_options()函数中添加了以下内容:

if($this->test_select !== $this->get_option('test_select')) {
    header("Refresh:0");
}

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章