产品元框字段,可在WooCommerce单个产品页面上启用额外的选项卡。

huangapple go评论77阅读模式
英文:

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( &#39;add_meta_boxes&#39;, &#39;additional_product_tabs_metabox&#39; );
function additional_product_tabs_metabox()
{
    add_meta_box(
        &#39;add_product_metabox_additional_tabs&#39;,
        __( &#39;Additional product Tabs&#39;, &#39;woocommerce&#39; ),
        &#39;additional_product_tabs_metabox_content&#39;,
        &#39;product&#39;,
        &#39;normal&#39;,
        &#39;high&#39;
    );
}

//  Add custom metabox content
function additional_product_tabs_metabox_content( $post )
{
    // Key Features
    echo &#39;&lt;h4&gt;&#39; . __( &#39;Key Features&#39;, &#39;woocommerce&#39; ) . &#39;&lt;/h4&gt;&#39;;
    $value = get_post_meta( $post-&gt;ID, &#39;_key_features&#39;, true );
    wp_editor( $value, &#39;_key_features&#39;, array( &#39;editor_height&#39; =&gt; 100 ) );


    // What News?
    echo &#39;&lt;br&gt;&lt;hr&gt;&lt;h4&gt;&#39; . __( &#39;What News?&#39;, &#39;woocommerce&#39; ) . &#39;&lt;/h4&gt;&#39;;
    $value = get_post_meta( $post-&gt;ID, &#39;_what_news&#39;, true );
    wp_editor( $value, &#39;_what_news&#39;, array( &#39;editor_height&#39; =&gt; 100 ) );


    // FAQ
    echo &#39;&lt;br&gt;&lt;hr&gt;&lt;h4&gt;&#39; . __( &#39;FAQ&#39;, &#39;woocommerce&#39; ) . &#39;&lt;/h4&gt;&#39;;
    $value = get_post_meta( $post-&gt;ID, &#39;_faq&#39;, true );
    wp_editor( $value, &#39;_faq&#39;, array( &#39;editor_height&#39; =&gt; 100 ) );


    // Nonce field (for security)
    echo &#39;&lt;input type=&quot;hidden&quot; name=&quot;additional_product_tabs_nonce&quot; value=&quot;&#39; . wp_create_nonce() . &#39;&quot;&gt;&#39;;
}


// Save product data
add_action( &#39;save_post_product&#39;, &#39;save_additional_product_tabs&#39;, 10, 1 );
function save_additional_product_tabs( $post_id ) {

    // Security check
    if ( ! isset( $_POST[ &#39;additional_product_tabs_nonce&#39; ] ) ) {
        return $post_id;
    }

    //Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[ &#39;additional_product_tabs_nonce&#39; ] ) ) {
        return $post_id;
    }

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

    if ( ! current_user_can( &#39;edit_product&#39;, $post_id ) ) {
        return $post_id;
    }

    // Sanitize user input and save the post meta fields values.

    if( isset($_POST[ &#39;_key_features&#39; ]) )
        update_post_meta( $post_id, &#39;_key_features&#39;, wp_kses_post($_POST[ &#39;_key_features&#39; ]) );

    if( isset($_POST[ &#39;_what_news&#39; ]) )
        update_post_meta( $post_id, &#39;_what_news&#39;, wp_kses_post($_POST[ &#39;_what_news&#39; ]) );
	
    if( isset($_POST[ &#39;_faq&#39; ]) )
        update_post_meta( $post_id, &#39;_faq&#39;, wp_kses_post($_POST[ &#39;_faq&#39; ]) );

}

add_filter( &#39;woocommerce_product_tabs&#39;, &#39;woo_custom_product_tabs&#39; );
function woo_custom_product_tabs( $tabs ) {
	
    $values = get_field(&#39;key_features_tab&#39;);
	if ( ! empty($values) ) { 
		$tabs[&#39;key_features_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;Key Features&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 10,
            &#39;callback&#39;  =&gt; &#39;woo_key_features_tab_content&#39;
        );
	}
	
	$what_news_values = get_field(&#39;what_news_tab&#39;);
    if ( ! empty($what_news_values) ) {
        $tabs[&#39;what_news_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;What News?&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 20,
            &#39;callback&#39;  =&gt; &#39;woo_what_news_tab_content&#39;
        );
    }
	
	$faq_values = get_field(&#39;faq_tab&#39;);
    if ( ! empty($faq_values) ) {
        $tabs[&#39;faq_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;FAQ&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 30,
            &#39;callback&#39;  =&gt; &#39;woo_faq_tab_content&#39;
        );
    }
	
    $tabs[&#39;reviews&#39;][&#39;priority&#39;] = 40;

    return $tabs;
}


function woo_key_features_tab_content()  {
    global $product;

    echo&#39;&lt;div&gt;&lt;p&gt;&#39;. $product-&gt;get_meta( &#39;_key_features&#39; ) . &#39;&lt;/p&gt;&lt;/div&gt;&#39;;
}

function woo_what_news_tab_content()  {
    global $product;

    echo&#39;&lt;div&gt;&lt;p&gt;&#39;. $product-&gt;get_meta( &#39;_what_news&#39; ) . &#39;&lt;/p&gt;&lt;/div&gt;&#39;;
}

function woo_faq_tab_content()  {
    global $product;

    echo&#39;&lt;div&gt;&lt;p&gt;&#39;. $product-&gt;get_meta( &#39;_faq&#39; ) . &#39;&lt;/p&gt;&lt;/div&gt;&#39;;
}

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( &#39;woocommerce_product_tabs&#39;, &#39;woo_custom_product_tabs&#39; );
function woo_custom_product_tabs( $tabs ) {
    global $product;

    $key_features = $product-&gt;get_meta(&#39;_key_features&#39;);
    if ( ! empty($key_features) ) { 
        $tabs[&#39;key_features_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;Key Features&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 10,
            &#39;callback&#39;  =&gt; &#39;woo_key_features_tab_content&#39;
        );
    }
    
    $what_news = $product-&gt;get_meta(&#39;_what_news&#39;);;
    if ( ! empty($what_news) ) {
        $tabs[&#39;what_news_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;What News?&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 20,
            &#39;callback&#39;  =&gt; &#39;woo_what_news_tab_content&#39;
        );
    }
    
    $faq_values = $product-&gt;get_meta(&#39;_faq&#39;);
    if ( ! empty($faq_values) ) {
        $tabs[&#39;faq_tab&#39;] = array(
            &#39;title&#39;     =&gt; __( &#39;FAQ&#39;, &#39;woocommerce&#39; ),
            &#39;priority&#39;  =&gt; 30,
            &#39;callback&#39;  =&gt; &#39;woo_faq_tab_content&#39;
        );
    }
    
    $tabs[&#39;reviews&#39;][&#39;priority&#39;] = 40;

    return $tabs;
}

Now it should work as expected.

huangapple
  • 本文由 发表于 2023年6月22日 14:55:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定