我正在 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 次了。我的插件中有一些部分在工作,但正如您所发现的那样,它很难解决。最好的例子是如何添加运输/支付网关……如果您可以访问其中任何一个。 而且这取决于您是否正在制作您希望以这些方式扩展的东西。
无论如何,因为我几乎无法理解我正在使用的代码,所以我试图将它压缩成一个控制所有部分的文件。
首先加载设置文件。
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 在选项卡内添加部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26355697/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资
我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca
我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>