英文:
Product metabox fields enabling additional tabs on WooCommerce single product pages
问题
以下是翻译好的部分:
问题代码部分:
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Key Features
echo '<h4>' . __( 'Key Features', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_key_features', true );
wp_editor( $value, '_key_features', array( 'editor_height' => 100 ) );
// What News?
echo '<br><hr><h4>' . __( 'What News?', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_what_news', true );
wp_editor( $value, '_what_news', array( 'editor_height' => 100 ) );
// FAQ
echo '<br><hr><h4>' . __( 'FAQ', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_faq', true );
wp_editor( $value, '_faq', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_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;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_key_features' ]) )
update_post_meta( $post_id, '_key_features', wp_kses_post($_POST[ '_key_features' ]) );
if( isset($_POST[ '_what_news' ]) )
update_post_meta( $post_id, '_what_news', wp_kses_post($_POST[ '_what_news' ]) );
if( isset($_POST[ '_faq' ]) )
update_post_meta( $post_id, '_faq', wp_kses_post($_POST[ '_faq' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
$values = get_field('key_features_tab');
if ( ! empty($values) ) {
$tabs['key_features_tab'] = array(
'title' => __( 'Key Features', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_key_features_tab_content'
);
}
$what_news_values = get_field('what_news_tab');
if ( ! empty($what_news_values) ) {
$tabs['what_news_tab'] = array(
'title' => __( 'What News?', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_what_news_tab_content'
);
}
$faq_values = get_field('faq_tab');
if ( ! empty($faq_values) ) {
$tabs['faq_tab'] = array(
'title' => __( 'FAQ', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_faq_tab_content'
);
}
$tabs['reviews']['priority'] = 40;
return $tabs;
}
function woo_key_features_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_key_features' ) . '</p></div>';
}
function woo_what_news_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_what_news' ) . '</p></div>';
}
function woo_faq_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_faq' ) . '</p></div>';
}
请注意,这只是代码的翻译部分。如果您有其他问题或需要进一步的帮助,请随时提出。
英文:
In continuation of this question: Custom metabox content displayed in single product additional tabs on Woocommerce
When I use the following code from this answer:
And by this method, when I want to not show empty tabs, I get an error. I use the following code:
// Add a custom metabox
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
function additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Additional product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'normal',
'high'
);
}
// Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
// Key Features
echo '<h4>' . __( 'Key Features', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_key_features', true );
wp_editor( $value, '_key_features', array( 'editor_height' => 100 ) );
// What News?
echo '<br><hr><h4>' . __( 'What News?', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_what_news', true );
wp_editor( $value, '_what_news', array( 'editor_height' => 100 ) );
// FAQ
echo '<br><hr><h4>' . __( 'FAQ', 'woocommerce' ) . '</h4>';
$value = get_post_meta( $post->ID, '_faq', true );
wp_editor( $value, '_faq', array( 'editor_height' => 100 ) );
// Nonce field (for security)
echo '<input type="hidden" name="additional_product_tabs_nonce" value="' . wp_create_nonce() . '">';
}
// Save product data
add_action( 'save_post_product', 'save_additional_product_tabs', 10, 1 );
function save_additional_product_tabs( $post_id ) {
// Security check
if ( ! isset( $_POST[ 'additional_product_tabs_nonce' ] ) ) {
return $post_id;
}
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'additional_product_tabs_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;
}
if ( ! current_user_can( 'edit_product', $post_id ) ) {
return $post_id;
}
// Sanitize user input and save the post meta fields values.
if( isset($_POST[ '_key_features' ]) )
update_post_meta( $post_id, '_key_features', wp_kses_post($_POST[ '_key_features' ]) );
if( isset($_POST[ '_what_news' ]) )
update_post_meta( $post_id, '_what_news', wp_kses_post($_POST[ '_what_news' ]) );
if( isset($_POST[ '_faq' ]) )
update_post_meta( $post_id, '_faq', wp_kses_post($_POST[ '_faq' ]) );
}
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
$values = get_field('key_features_tab');
if ( ! empty($values) ) {
$tabs['key_features_tab'] = array(
'title' => __( 'Key Features', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_key_features_tab_content'
);
}
$what_news_values = get_field('what_news_tab');
if ( ! empty($what_news_values) ) {
$tabs['what_news_tab'] = array(
'title' => __( 'What News?', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_what_news_tab_content'
);
}
$faq_values = get_field('faq_tab');
if ( ! empty($faq_values) ) {
$tabs['faq_tab'] = array(
'title' => __( 'FAQ', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_faq_tab_content'
);
}
$tabs['reviews']['priority'] = 40;
return $tabs;
}
function woo_key_features_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_key_features' ) . '</p></div>';
}
function woo_what_news_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_what_news' ) . '</p></div>';
}
function woo_faq_tab_content() {
global $product;
echo'<div><p>'. $product->get_meta( '_faq' ) . '</p></div>';
}
Where is the problem in this code?
Another question is that I know that additional info is displayed when the product has weight, height, etc. Is it possible to delete the additional info tab forever because my products are virtual and I don't have a physical product.
Also, can I create a tab called additional info that has nothing to do with WooCommerce's additional info tab?
答案1
得分: 3
问题在于您在woo_custom_product_tabs()
挂钩函数中使用了ACF的get_field()
函数,而不是WooCommerce的get_meta()
方法。另外,您使用了错误的元数据键。
所以请更改为:
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $product;
$key_features = $product->get_meta('_key_features');
if ( ! empty($key_features) ) {
$tabs['key_features_tab'] = array(
'title' => __( 'Key Features', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_key_features_tab_content'
);
}
$what_news = $product->get_meta('_what_news');
if ( ! empty($what_news) ) {
$tabs['what_news_tab'] = array(
'title' => __( 'What News?', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_what_news_tab_content'
);
}
$faq_values = $product->get_meta('_faq');
if ( ! empty($faq_values) ) {
$tabs['faq_tab'] = array(
'title' => __( 'FAQ', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_faq_tab_content'
);
}
$tabs['reviews']['priority'] = 40;
return $tabs;
}
现在它应该按预期工作。
英文:
The problem is that you are using ACF get_field()
function instead of the WooCommerce method get_meta()
inside woo_custom_product_tabs()
hooked function. Also, you are using the wrong meta keys.
So change it for:
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $product;
$key_features = $product->get_meta('_key_features');
if ( ! empty($key_features) ) {
$tabs['key_features_tab'] = array(
'title' => __( 'Key Features', 'woocommerce' ),
'priority' => 10,
'callback' => 'woo_key_features_tab_content'
);
}
$what_news = $product->get_meta('_what_news');;
if ( ! empty($what_news) ) {
$tabs['what_news_tab'] = array(
'title' => __( 'What News?', 'woocommerce' ),
'priority' => 20,
'callback' => 'woo_what_news_tab_content'
);
}
$faq_values = $product->get_meta('_faq');
if ( ! empty($faq_values) ) {
$tabs['faq_tab'] = array(
'title' => __( 'FAQ', 'woocommerce' ),
'priority' => 30,
'callback' => 'woo_faq_tab_content'
);
}
$tabs['reviews']['priority'] = 40;
return $tabs;
}
Now it should work as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论