php – Woocommerce在标签内添加部分
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – Woocommerce在标签内添加部分,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7695字,纯文字阅读大概需要11分钟。
内容图文
![php – Woocommerce在标签内添加部分](/upload/InfoBanner/zyjiaocheng/818/7d39e7c50091493a99874838fc0fe5bc.jpg)
我在woocommerce做了一个小插件.在那个插件中,我想在管理区域显示部分,就像在woocommerce设置仪表板的运输选项卡中发送选项,统一费率,免费送货等.首先,我从插件创建了一个选项卡,在该选项卡中我想要这些部分.为了创建选项卡,我已经从这个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设置选项卡中添加部分吗?任何帮助和建议都会非常明显.
解决方法:
我试图已经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如何做到的,甚至我在自己的代码中是如何做到的.但是,我认为这是最简单的方法.如果您需要可以扩展的东西,这将是不合适的,但这也更难写.
内容总结
以上是互联网集市为您收集整理的php – Woocommerce在标签内添加部分全部内容,希望文章能够帮你解决php – Woocommerce在标签内添加部分所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。