如果您是插件开发人员,并且想要在WooCommerce的产品选项卡中添加新的部分,请在本教程中密切关注,因为我将分享WooCommerce黑客在产品选项卡中添加一个部分。
如何在WooCommerce中将产品添加到产品选项卡
让我们在“滑块”的产品选项卡中创建一个部分。
在产品选项卡中创建新的部分时,我们需要做的第一件事是使用WooCommerce过滤器添加新的部分woocommerce_get_sections_products
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
/**
* Create the section beneath the products tab
**/
add_filter( ‘woocommerce_get_sections_products’, ‘wcslider_add_section’ );
function wcslider_add_section( $sections ) {
$sections[‘wcslider’] = __( ‘WC Slider’, ‘text-domain’ );
return $sections;
}
|
现在打开产品选项卡,您将看到另一个部分为“WC滑块”。
让我们为本节创建一个表单。复制下面的代码并将其添加到您的functions.php文件中,以防您不想创建您的插件,否则将下面的代码放在您的插件文件中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php
/**
* Add settings to the specific section we created before
*/
add_filter( ‘woocommerce_get_settings_products’, ‘wcslider_all_settings’, 10, 2 );
function wcslider_all_settings( $settings, $current_section ) {
/**
* Check the current section is what we want
**/
if ( $current_section == ‘wcslider’ ) {
$settings_slider = array();
// Add Title to the Settings
$settings_slider[] = array( ‘name’ => __( ‘WC Slider Settings’, ‘text-domain’ ), ‘type’ => ‘title’, ‘desc’ => __( ‘The following options are used to configure WC Slider’, ‘text-domain’ ), ‘id’ => ‘wcslider’ );
// Add first checkbox option
$settings_slider[] = array(
‘name’ => __( ‘Auto-insert into single product page’, ‘text-domain’ ),
‘desc_tip’ => __( ‘This will automatically insert your slider into the single product page’, ‘text-domain’ ),
‘id’ => ‘wcslider_auto_insert’,
‘type’ => ‘checkbox’,
‘css’ => ‘min-width:300px;’,
‘desc’ => __( ‘Enable Auto-Insert’, ‘text-domain’ ),
);
// Add second text field option
$settings_slider[] = array(
‘name’ => __( ‘Slider Title’, ‘text-domain’ ),
‘desc_tip’ => __( ‘This will add a title to your slider’, ‘text-domain’ ),
‘id’ => ‘wcslider_title’,
‘type’ => ‘text’,
‘desc’ => __( ‘Any title you want can be added to your slider with this option!’, ‘text-domain’ ),
);
$settings_slider[] = array( ‘type’ => ‘sectionend’, ‘id’ => ‘wcslider’ );
return $settings_slider;
/**
* If not, return the standard settings
**/
} else {
return $settings;
}
}
|
刷新管理页面,你会看到新的部分添加为“WC滑块”与基本选项。您可以根据您的要求更改或自定义上述代码。