Woocommerce在标签内添加部分

时间:2014-10-14 08:06:20

标签: php wordpress plugins wordpress-plugin woocommerce

我正在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设置选项卡中添加部分吗?任何帮助和建议都会非常明显。

2 个答案:

答案 0 :(得分:6)

我已经尝试过2x回答这个问题了。我的部分工作在我的插件中,但是你发现它是一个可以解决的野兽。最好的例子是如何添加运输/支付网关......如果您可以访问其中任何一个。 这取决于你是否正在制作一些你希望以那些方式扩展的东西。

无论如何,因为我几乎找不到我正在使用的代码,所以我试着把它压缩成一个控制所有部分的文件。

首先加载设置文件。

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正在做的事情,也不是我在自己的代码中如何做到这一点。但是,我认为这是最简单的方法。如果您需要可以扩展的东西,这将是不合适的,但这也更难写。

答案 1 :(得分:0)

你也应该在钩子上触发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文件中,该文件应放在主题文件夹中。

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