在“附加信息区域"上显示单一和可变产品的 SKU

Display SKU on the #39;additional information area#39; for single and variable products(在“附加信息区域上显示单一和可变产品的 SKU)
本文介绍了在“附加信息区域"上显示单一和可变产品的 SKU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在单个产品附加信息选项卡的表格行中显示产品的 SKU.

I'm trying to display the product's SKU inside the table row of single products Additional Information tab.

我尝试使用 woocommerce_display_product_attributes 过滤器并显示它(下面是我的代码示例),但它仅适用于简单产品.

I tried using the woocommerce_display_product_attributes filter and have it displayed (sample of my code below) but it only works with simple product.

When using variable products with different SKU, the field are not updated when (dropdown select) variation is selected and only show blank.有没有合适的方法来做到这一点?

When using variable products with different SKU, the field are not updated when (dropdown select) variation is selected and only show blank. Is there a proper way to do this?

这是我当前的代码:

// Displays SKU/Part# to Single product Additional information table rows
add_filter('woocommerce_display_product_attributes', 'wc_display_sku_additional_info_table', 10, 2);
function wc_display_sku_additional_info_table( $product_attributes, $product ){
    // Get product SKU
    $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );

    $product_attributes[] = [
        'label' => __('SKU', 'woocommerce'),
        'value' => $get_sku,
    ];
    return $product_attributes;
}

推荐答案

这应该足够了,注释和解释添加到我的代码中

This should suffice, comment with explanation added to my code

  • 对于 singlevariable 产品,SKU 表行会添加到附加信息选项卡中.
  • SKU 表行根据variable 产品的下拉选择菜单进行相应更新
  • For both single and variable products, a SKU table row is added to the additional information tab.
  • SKU table row is updated accordingly with the dropdown select menu for variable products
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);

这篇关于在“附加信息区域"上显示单一和可变产品的 SKU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)