【问题标题】:Woocommerce add sections inside the tabsWoocommerce 在选项卡内添加部分
【发布时间】:2014-12-08 22:41:03
【问题描述】:

我在woocommerce 做一个小插件。在该插件中,我想在管理区域中显示部分,就像 woocommerce 设置仪表板的运输选项卡中的 shipping Options, Flat Rate, Free Shipping etc 一样。首先,我从插件创建了一个选项卡,在该选项卡内我想要这些部分。为了创建标签,我从这个link 中获取了代码所以我的插件代码是这样的

<?php
/**
 * Plugin Name: WooCommerce Settings Tab 
 * Plugin URI: http://www.wordpress.org
 * Description: Woocmmerce settings tab.
 * Author: Author Name
 * Author URI: http://www.wordpress.org
 * Version: 1.0
 *
 */

class WC_Settings_Tab {
    public static function init() {
        add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 );
        add_action( 'woocommerce_settings_tabs_settings_tab_demo', __CLASS__ . '::settings_tab' );
        add_action( 'woocommerce_update_options_settings_tab_demo', __CLASS__ . '::update_settings' );
        add_action( 'woocommerce_sections',__CLASS__ . '::get_sections');
    }

    public static function add_settings_tab( $settings_tabs ) {
        $settings_tabs['settings_tab_demo'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
        return $settings_tabs;
    }

    public static function settings_tab() {
        woocommerce_admin_fields( self::get_settings() );
    }

    public static function update_settings() {
        woocommerce_update_options( self::get_settings() );
    }

      public function get_sections() {
        $sections = array(
          '' => __( 'Test Link 1', 'woocommerce' ),
          'testlink2' => __( 'Test Link 2', 'woocommerce' ),
        );

        return apply_filters( 'woocommerce_sections', $sections );
      }    

    public static function get_settings() {

        $settings = array(
            'section_title' => array(
                'name'     => __( 'Section Title', 'woocommerce-settings-tab-demo' ),
                'type'     => 'title',
                'desc'     => '',
                'id'       => 'wc_settings_tab_demo_section_title'
            ),
            'title' => array(
                'name' => __( 'Title', 'woocommerce-settings-tab-demo' ),
                'type' => 'text',
                'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
                'id'   => 'wc_settings_tab_demo_title'
            ),
            'description' => array(
                'name' => __( 'Description', 'woocommerce-settings-tab-demo' ),
                'type' => 'textarea',
                'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
                'id'   => 'wc_settings_tab_demo_description'
            ),
            'section_end' => array(
                 'type' => 'sectionend',
                 'id' => 'wc_settings_tab_demo_section_end'
            )
        );

        return apply_filters( 'wc_settings_tab_demo_settings', $settings );
    }

}

WC_Settings_Tab::init();

但它没有显示这些部分。那么有人可以告诉我如何在 woocommerce 设置选项卡中添加部分吗?任何帮助和建议都会非常重要。

【问题讨论】:

    标签: php plugins wordpress woocommerce


    【解决方案1】:

    我已经尝试过两次回答这个问题。我的插件中有部分工作,但正如您发现的那样,这是一个很难解决的问题。最好的例子是如何添加运输/支付网关......如果您可以访问其中任何一个。 而且这取决于你是否正在制作一些你想要以这种方式进行扩展的东西。

    无论如何,因为我几乎无法弄清楚我正在使用的代码,所以我试图将其压缩为一个控制所有部分的文件。

    首先加载设置文件。

    add_filter( 'woocommerce_get_settings_pages', 'so_26355697_add_settings_page' );
    function so_26355697_add_settings_page( $settings ) {
        $settings[] = include( 'wc-plugin-settings.php' );  
        return $settings;
    }
    

    然后在wc-plugin-settings.php文件中(必须返回一个类对象)...

    class WC_Settings_My_Plugin extends WC_Settings_Page {
    
        /**
         * Constructor
         */
        public function __construct() {
    
            $this->id    = 'demo_plugin';
    
            add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 50 );
            add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
            add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
            add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
    
        }
    
        /**
         * Add plugin options tab
         *
         * @return array
         */
        public function add_settings_tab( $settings_tabs ) {
            $settings_tabs[$this->id] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' );
            return $settings_tabs;
        }
    
        /**
         * Get sections
         *
         * @return array
         */
        public function get_sections() {
    
            $sections = array(
                'section-0'         => __( 'Plugin Options', 'woocommerce-settings-tab-demo' ),
                'section-1'         => __( 'Section 1', 'woocommerce-settings-tab-demo' ),
                'section 2'         => __( 'Section 2', 'woocommerce-settings-tab-demo' ),
    
            );
    
            return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
        }
    
    
        /**
         * Get sections
         *
         * @return array
         */
        public function get_settings( $section = null ) {
    
            switch( $section ){
    
                case 'section-0' :
                    $settings = array(
                        'section_title' => array(
                            'name'     => __( 'Main Section Title', 'woocommerce-settings-tab-demo' ),
                            'type'     => 'title',
                            'desc'     => '',
                            'id'       => 'wc_settings_tab_demo_title_section-1'
                        ),
                        'title' => array(
                            'name' => __( 'Main Title', 'woocommerce-settings-tab-demo' ),
                            'type' => 'text',
                            'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_title_section-1'
                        ),
                        'description' => array(
                            'name' => __( 'Main Description', 'woocommerce-settings-tab-demo' ),
                            'type' => 'textarea',
                            'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_description_section-1'
                        ),
                        'section_end' => array(
                             'type' => 'sectionend',
                             'id' => 'wc_settings_tab_demo_end-section-1'
                        )
                    );
    
                break;
                case 'section-1':
                    $settings = array(
                        'section_title' => array(
                            'name'     => __( 'Section One Title', 'woocommerce-settings-tab-demo' ),
                            'type'     => 'title',
                            'desc'     => '',
                            'id'       => 'wc_settings_tab_demo_section_title_section-2'
                        ),
                        'title' => array(
                            'name' => __( 'Section One Title', 'woocommerce-settings-tab-demo' ),
                            'type' => 'text',
                            'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_title_section-2'
                        ),
                        'description' => array(
                            'name' => __( 'Section One Description', 'woocommerce-settings-tab-demo' ),
                            'type' => 'textarea',
                            'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_description_section-2'
                        ),
                        'section_end' => array(
                             'type' => 'sectionend',
                             'id' => 'wc_settings_tab_demo_section_end_section-2'
                        )
                    );
                break;
                case 'section-2':
                    $settings = array(
                        'section_title' => array(
                            'name'     => __( 'Section Two Title', 'woocommerce-settings-tab-demo' ),
                            'type'     => 'title',
                            'desc'     => '',
                            'id'       => 'wc_settings_tab_demo_section_title'
                        ),
                        'title' => array(
                            'name' => __( 'Section Two Title', 'woocommerce-settings-tab-demo' ),
                            'type' => 'text',
                            'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_title'
                        ),
                        'description' => array(
                            'name' => __( 'Section Two Description', 'woocommerce-settings-tab-demo' ),
                            'type' => 'textarea',
                            'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ),
                            'id'   => 'wc_settings_tab_demo_description'
                        ),
                        'section_end' => array(
                             'type' => 'sectionend',
                             'id' => 'wc_settings_tab_demo_section_end'
                        )
                    );
    
    
                break;
    
            }
    
            return apply_filters( 'wc_settings_tab_demo_settings', $settings, $section );
    
        }
    
        /**
         * Output the settings
         */
        public function output() {
            global $current_section;
            $settings = $this->get_settings( $current_section );
            WC_Admin_Settings::output_fields( $settings );
        }
    
    
        /**
         * Save settings
         */
        public function save() {
            global $current_section;
            $settings = $this->get_settings( $current_section );
            WC_Admin_Settings::save_fields( $settings );
        }
    
    }
    
    return new WC_Settings_My_Plugin();
    

    正如我所说,这并不是 Woo 的做法,甚至不是我在自己的代码中的做法。但是,我认为这是最简单的方法。如果您需要可以扩展的东西,这将不合适,但编写起来也困难得多。

    【讨论】:

    • 所以add_filter( 'woocommerce_get_settings_pages', 'so_26355697_add_settings_page' ); function so_26355697_add_settings_page( $settings ) { $settings[] = include( 'wc-plugin-settings.php' ); return $settings; } 应该在WC_Settings_Page 类中?
    • 没有。它可以在任何地方......大概是您正在构建的任何插件的主文件。
    • 大家好,我如何阅读上面的类创建的 WC 选项?将这些变量存储在数据库中后,如何获取它们?谢谢
    • 数据可以通过get_option()使用设置ID访问。所以我认为get_option( "wc_settings_tab_demo_title_section-1" )
    【解决方案2】:

    您还应该在一个钩子上触发 init 函数,以便与 WordPress 配合得很好,这样它就会在正确的时间发生。此触发器是“admin_init”触发器。

    add_action( 'admin_init',  '<your func name>');
    

    所以对于你的用例,替换:

    WC_Settings_Tab::init();
    

    add_action( 'admin_init',  'WC_Settings_Tab::init');
    

    使用 admin_init 触发器的另一种方法是将您提供的函数添加到您应该放在主题文件夹中的 woocommerce-admin-init.php 文件中。

    不同之处在于,第一个解决方案可以在您想要的任何地方使用,因此您可以在插件和主题中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      相关资源
      最近更新 更多