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

我试图在单个产品附加信息选项卡的表行中显示产品的 SKU。


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


当使用具有不同 SKU 的可变产品时,选择(下拉选择)变体时该字段不会更新,只会显示空白。有没有正确的方法来做到这一点?


这是我当前的代码:


// 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;

}


牛魔王的故事
浏览 99回答 1
1回答

宝慕林4294392

这应该足够了,注释并添加到我的代码中对于 和single产品variable,SKU 表行添加到附加信息选项卡。SKU 表格行会根据variable产品的下拉选择菜单进行相应更新function display_product_attributes( $product_attributes, $product ) {&nbsp; &nbsp; // Simple product&nbsp; &nbsp; if ( $product->is_type('simple' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get product SKU&nbsp; &nbsp; &nbsp; &nbsp; $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );&nbsp; &nbsp; &nbsp; &nbsp; // Add&nbsp; &nbsp; &nbsp; &nbsp; $product_attributes[ 'sku-field sku-field-single' ] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __('SKU', 'woocommerce'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $get_sku,&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; // Variable product&nbsp; &nbsp; elseif ( $product->is_type('variable' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get childIDs in an array&nbsp; &nbsp; &nbsp; &nbsp; $children_ids = $product->get_children();&nbsp; &nbsp; &nbsp; &nbsp; // Loop&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $children_ids as $child_id ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get product&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product = wc_get_product( $child_id );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get product SKU&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $get_sku = ( $sku = $product->get_sku() ) ? $sku : esc_html__( 'N/A', 'woocommerce' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_attributes[ 'sku-field sku-field-variable sku-field-variable-' . $child_id ] = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label' => __('SKU', 'woocommerce'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value' => $get_sku,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; jQuery(document).ready(function($) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide all rows&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.sku-field-variable' ).css( 'display', 'none' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Change&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( 'input.variation_id' ).change( function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide all rows&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.sku-field-variable' ).css( 'display', 'none' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( $( 'input.variation_id' ).val() != '' ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var var_id = $( 'input.variation_id' ).val();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display current&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.sku-field-variable-' + var_id ).css( 'display', 'table-row' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; }&nbsp; &nbsp; return $product_attributes;}add_filter('woocommerce_display_product_attributes', 'display_product_attributes', 10, 2);
打开App,查看更多内容
随时随地看视频慕课网APP