我使用这个很棒的脚本在单个产品页面的附加信息选项卡中显示WooCommerce产品SKU,虽然我也想显示产品类别.你知道怎么把这个加到 playbook 里吗?

目前,SKU被放置在附加信息表的底部(最后一行).你知道如何将它添加到表格的顶部(第一行)吗?

function display_product_attributes( $product_attributes, $product ) {
    // Simple product
    if ( $product->is_type('simple' ) ) {
        // Get product SKU
        $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );

        // Add
        $product_attributes[ 'sku-field sku-field-single' ] = array(
            'label' => __('SKU', 'woocommerce'),
            'value' => $get_sku,
        );

    } 
    // Variable product
    elseif ( $product->is_type('variable' ) ) {
        // Get childIDs in an array
        $children_ids = $product->get_children();

        // Loop
        foreach ( $children_ids as $child_id ) {
            // Get product
            $product = wc_get_product( $child_id ); 

            // Get product SKU
            $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );

            // Add
            $product_attributes[ 'sku-field sku-field-variable sku-field-variable-' . $child_id ] = array(
                'label' => __('SKU', 'woocommerce'),
                'value' => $get_sku,
            );
        }
        ?>
        <script>
        jQuery(document).ready(function($) {
            // Hide all rows
            $( '.sku-field-variable' ).css( 'display', 'none' );

            // Change
            $( 'input.variation_id' ).change( function() {
                // Hide all rows
                $( '.sku-field-variable' ).css( 'display', 'none' );

                if( $( 'input.variation_id' ).val() != '' ) {
                    var var_id = $( 'input.variation_id' ).val();

                    // Display current
                    $( '.sku-field-variable-' + var_id ).css( 'display', 'table-row' );
                }
            });    
        });
        </script>
        <?php
    }

    return $product_attributes;
}
add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);

推荐答案

您可以使用以下修订后的代码,该代码还将显示产品类别:

add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);
function display_product_attributes( $product_attributes, $product ) {
    // Simple product
    if ( $product->is_type('simple') ) {
        $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__('N/A', 'woocommerce'); // Get product SKU

        // Add Row
        $product_attributes[ 'sku-field sku-field-single' ] = array(
            'label' => __('SKU', 'woocommerce'),
            'value' => $get_sku,
        );

    } 
    // Variable product
    elseif ( $product->is_type('variable') ) {
        // Loop through variation Ids
        foreach ( $product->get_children() as $variation_id ) {
            $variation = wc_get_product( $variation_id ); // Get the product variation object
            $get_sku   = ( $sku = $variation->get_sku() ) ? $sku : esc_html__('N/A', 'woocommerce'); // Get variation SKU

            // Add Row
            $product_attributes[ 'sku-field sku-field-variable sku-field-variable-' . $variation_id ] = array(
                'label' => __('SKU', 'woocommerce'),
                'value' => $get_sku,
            );
        }
        ?>
        <script>
        jQuery( function($) {
            $('.sku-field-variable').css('display', 'none'); // Hide all rows

            // On Change
            $('input.variation_id').change( function() {
                $('.sku-field-variable').css('display', 'none'); // Hide all rows

                const var_id = $('input.variation_id').val();
                if( var_id != '' ) {
                    // Display current
                    $( '.sku-field-variable-'+var_id ).css( 'display', 'table-row' );
                }
            });    
        });
        </script>
        <?php
    }
    // Categories
    $category_ids = $product->get_category_ids();

    if ( ! empty($category_ids) ) {
        $product_attributes[ 'category-field  category-field-single' ] = array(
            'label' => _n( 'Category', 'Categories', count( $category_ids ), 'woocommerce' ),
            'value' => wc_get_product_category_list( $product->get_id(), ', ', '', '' ),
        );
    }
    return $product_attributes;
}

代码放在子主题的functions.php文件中(或插件中).测试和作品.

Php相关问答推荐

Symfony Monolog:使用多个格式化程序

为什么注册SIGHUP的处理程序函数会阻止在PHP CLI中等待输入时点击X关闭Xterm窗口?""

根据产品类型显示或隐藏WooCommerce管理产品自定义字段

有没有办法为整个PHP应用程序启用严格类型

使用PHP curl的Amazon SP API批处理调用

Mod_重写子目录中相应的php文件

如何在Laravel Model中自定义多个日期属性的日期格式?

Laravel Carbon DiffForHumans选项(年,月,天)

限制联系人页面仅允许在WooCommerce中登录的用户

PHP Match如何准确判断条件?

WooCommerce产品页面上没有产品详细信息

在WooCommerce中以编程方式添加特定产品后,将价格设置为零

Laravel 在数据库上显示加密列

如果在 APIPlatform 3.1 中的同一资源上使用处理器,Messenger 将无法工作

如何处理 Null 上的 array_shift() ?

WooCommerce 按产品运输插件:仅收取较高的运输费用

在PHP WordPress插件中使用变量连接路径时的问题解决方法

php/html OnSubmit 禁用输入类型提交未给出预期结果

Symfony 自定义注销

使用 --exclude-dir 选项从 PHP 执行 grep 时出现问题