gpt4 book ai didi

php - 在 Woocommerce 管理产品页面中编辑自定义产品选项卡内容

转载 作者:可可西里 更新时间:2023-11-01 00:39:48 25 4
gpt4 key购买 nike

在 WooCommerce 中,我想添加自定义全局产品选项卡,就像创建新产品时的附加信息一样。

我可以创建新标签,但无法在创建新产品页面上更新任何内容。

我可以在显示页面上看到它,但是如何通过产品编辑页面添加信息。我知道我可以使用自定义字段,但我希望将其放在产品页面上,以允许商店经理或其他人填写这个额外的运输标签。

我的代码是

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {

// Adds the new tab
$tabs['test_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);

return $tabs;
}

function woo_new_product_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<p>'.get_post_meta($prod_id,'Shipping',true).'</p>';
}

http://prntscr.com/h37kqv

最佳答案

您可以在管理产品页面中添加自定义 Metabox,这将允许商店经理为您的自定义产品选项卡添加内容。你会得到这个:

enter image description here

代码如下:

// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_shipping_field',
__( 'Custom Shipping Tab information', 'woocommerce' ),
'add_custom_content_meta_box',
'product',
'normal',
'high'
);
}
}

// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_content_meta_box' ) )
{
function add_custom_content_meta_box( $post )
{
$value = get_post_meta( $post->ID, '_shipping_tab', true ) ? get_post_meta( $post->ID, '_shipping_tab', true ) : '';
wp_editor( $value, 'custom_shipping_tab', array( 'editor_height' => 100 ) );
echo '<input type="hidden" name="custom_product_field_nonce" value="' . wp_create_nonce() . '">';

}
}


//Save the data of the Meta field
add_action( 'save_post', 'save_custom_content_meta_box', 10, 1 );
if ( ! function_exists( 'save_custom_content_meta_box' ) )
{

function save_custom_content_meta_box( $post_id ) {

// We need to verify this with the proper authorization (security stuff).

// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_product_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'custom_product_field_nonce' ];

//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}

// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}

// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {

if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {

if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// --- Its safe for us to save the data ! --- //

// Sanitize user input and update the meta field in the database.
update_post_meta( $post_id, '_shipping_tab', wp_kses_post($_POST[ 'custom_shipping_tab' ]) );
}
}

那么你自己的代码将是:

// Add product custom "Shipping" tab
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {

$tabs['test_tab'] = array(
'title' => __( 'Shipping', 'woocommerce' ),
'priority' => 50,
'callback' => 'shipping_product_tab_content'
);

return $tabs;
}

// The Shipping tab content
function shipping_product_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo'<div><p>'.get_post_meta( get_the_ID(), '_shipping_tab' ,true ).'</p></div>';
}

代码进入您活跃的子主题(或主题)的 function.php 文件或任何插件文件。

经过测试并有效。

关于php - 在 Woocommerce 管理产品页面中编辑自定义产品选项卡内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com