dpkrh2444 2019-02-27 10:38
浏览 366

为WooCommerce产品添加动态自定义标签

I'm looking for a solution to add a custom tab for my product page in WooCommerce. I found already some solution like this code snippet below. But all that is for static contents. Is it possible to create a custom tab for product pages and where I can set the content individually in the admin product page? I know that there are plugins for that, but I'm trying to find a solution with using a plugin.

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

    // 1) Removing tabs

    unset( $tabs['description'] );              // Remove the description tab
    // unset( $tabs['reviews'] );               // Remove the reviews tab
    unset( $tabs['additional_information'] );   // Remove the additional information tab


    // 2 Adding new tabs and set the right order

    //Attribute Description tab
    $tabs['attrib_desc_tab'] = array(
        'title'     => __( 'Desc', 'woocommerce' ),
        'priority'  => 100,
        'callback'  => 'woo_attrib_desc_tab_content'
    );

    // Adds the qty pricing  tab
    $tabs['qty_pricing_tab'] = array(
        'title'     => __( 'Quantity Pricing', 'woocommerce' ),
        'priority'  => 110,
        'callback'  => 'woo_qty_pricing_tab_content'
    );

    // Adds the other products tab
    $tabs['other_products_tab'] = array(
        'title'     => __( 'Other Products', 'woocommerce' ),
        'priority'  => 120,
        'callback'  => 'woo_other_products_tab_content'
    );

    return $tabs;

}

// New Tab contents

function woo_attrib_desc_tab_content() {
    // The attribute description tab content
    echo '<h2>Description</h2>';
    echo '<p>Custom description tab.</p>';
}
function woo_qty_pricing_tab_content() {
    // The qty pricing tab content
    echo '<h2>Quantity Pricing</h2>';
    echo '<p>Here\'s your quantity pricing tab.</p>';
}
function woo_other_products_tab_content() {
    // The other products tab content
    echo '<h2>Other Products</h2>';
    echo '<p>Here\'s your other products tab.</p>';
}
  • 写回答

1条回答 默认 最新

  • douhu4091 2019-05-08 14:12
    关注

    Adding a custom tab is possible through woocommerce_product_tabs filter.
    For creating custom data field for your products I recommend to use ACF.

    1. Add a new field

    • You need to add a new field for the products and add a name parameter for example custom_detail. (We use this in the code inside the get_field function). You can change its type for example textarea or WYSIWYG. In the roles section set Post type | is equal to | product.
    • Now on the Edit product screen you can find your new field.

    2. Display the custom tab

    add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
    function woo_custom_product_tabs( $tabs ) {
      $tabs = array(
        'title' => 'Custom tab',
        'priority' => 20,
        'callback' => 'get_custom_product_tab_content',
      );
      return $tabs;
    }
    
    function get_custom_product_tab_content () {
      the_field('custom_detail');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器