【问题标题】:Unable to reorder product tabs in single product page?无法在单个产品页面中重新排序产品选项卡?
【发布时间】:2022-05-20 01:26:55
【问题描述】:

我正在尝试重新排序单个产品页面中的产品选项卡,从描述、评论到评论、描述。我遇到了 woocommerce 文档,该文档解释了如何操作并尝试了示例代码。但是当我在我的functions.php文件中添加代码时没有任何反应。我可能做错了什么?这是我的示例代码

<?php
/**
 * Kidz-Child functions and definitions
 *
 * @package kidz-child
 */

/** Loading language **/
add_action( 'after_setup_theme', 'kidz_child_theme_setup' );
function kidz_child_theme_setup() {
    load_child_theme_textdomain( 'kidz', get_stylesheet_directory() . '/languages' );
    load_textdomain( 'ideapark-wishlist', get_stylesheet_directory() . '/languages/' . 'ideapark-wishlist' . '-' . apply_filters( 'plugin_locale', get_locale(), 'ideapark-wishlist' ) . '.mo' );
    load_textdomain( 'ideapark-theme-functionality', get_stylesheet_directory() . '/languages/' . 'ideapark-theme-functionality' . '-' . apply_filters( 'plugin_locale', get_locale(), 'ideapark-theme-functionality' ) . '.mo' );
}

/** Enqueue the child theme stylesheet **/
add_action( 'wp_enqueue_scripts', 'kidz_child_enqueue_scripts', 100 );
function kidz_child_enqueue_scripts() {
    wp_enqueue_style( 'kidz-child-style', get_stylesheet_directory_uri() . '/style.css' );
}

function wc_billing_field_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Billing Details' :
$translated_text = __( 'Your Details', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'wc_billing_field_strings', 20, 3 );

add_action( 'woocommerce_before_cart_table', 'woo_add_continue_shopping_button_to_cart' );
function woo_add_continue_shopping_button_to_cart() {
 $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );

 echo '<div class="woocommerce-message">';
 echo ' <a href="'.$shop_page_url.'" class="button">Continue Shopping →</a> Would you like some more goods?';
 echo '</div>';
}



remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );

add_action('init', 'add_new_star_rating');
    function add_new_star_rating()
    {
      add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 6 );
    }

function wc_direct_link_to_product_tabs() {
    if( is_product() ) {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {

            if( window.location.hash ) {

                // Vars
                var tab         = window.location.hash.replace('#', '');
                var tab_content = 'tab-' + tab;

                // Tabs
                $( 'li.description_tab' ).removeClass( 'active' );
                $( 'li.' + tab + '_tab' ).addClass( 'active' );

                // Tabs content
                $( '#tab-description' ).hide();
                $( '#' + tab_content ).show();
            }

            // when the tab is selected update the url with the hash
            $(".tabs a").click( function() { 
                window.location.hash = $(this).parent('li').attr("class").replace(' active', '').replace('_tab', '');
            });

        });
        </script>

    <?php
    }
}


add_action( 'wp_footer', 'wc_direct_link_to_product_tabs', 30 );
add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {

    $tabs['reviews']['priority'] = 5;           // Reviews first
    $tabs['description']['priority'] = 10;          // Description second


    return $tabs;
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以在主题的functions.php 或自定义模块中使用此代码 sn-p 文件。

    add_filter( 'woocommerce_product_tabs', function ( $tabs ) {
    $tab_list = [
        'description'            => 10,
        'additional_information' => 15,
        'reviews'                => 5,
    ];
    foreach ( $tab_list as $tab => $priority ) {
        if ( isset( $tabs[ $tab ] ) ) {
            $tabs[ $tab ]['priority'] = $priority;
        }
    }
    return $tabs;
    }, 98 );
    

    【讨论】:

      【解决方案2】:

      这适用于 function.php - 子主题:

      /*** Reorder product data tabs*/
      add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
      function woo_reorder_tabs( $tabs ) {
      $tabs['additional_information']['priority'] = 5; // Additional information first    
      $tabs['description']['priority'] = 10; // Description second
      return $tabs;
      }

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多