dsyq40772 2018-08-06 10:07
浏览 186
已采纳

WooCommerce:在可变产品页面中显示当前的SKU和GTIN

Is there a way to display the current product SKU and GTIN in a variable product page? Right now I'm using the following code, but it only works for simple products without variations:

add_action('woocommerce_single_product_summary', 'show_sku_and_gtin');
function show_sku_and_gtin() {
  global $product, $post; 
  echo '<p>SKU: '.$product->sku.'</p>';
  // In my case, GTIN is a custom field called "barcode"
  echo '<p>GTIN: '.get_post_meta($post->ID, 'barcode', 1).'</p>';                    
}

I'd like it to update the SKU and GTIN dynamically whenever I choose a different product variation in the product page.

  • 写回答

1条回答 默认 最新

  • dougu2240 2018-08-06 11:28
    关注

    Update (for your existing code at the end)

    To make that possible for variation products, we will use the dynamic variation description to insert the SKU and the GTIN and display them dynamically:

    // Display product variations SKU and GTIN info
    add_filter( 'woocommerce_available_variation', 'display_variation_sku_and_gtin', 20, 3 );
    function display_variation_sku_and_gtin( $variation_data, $product, $variation ) {
        $html = ''; // Initializing
    
        // Inserting SKU
        if( ! empty( $variation_data['sku'] ) ){
            $html .= '</div><div class="woocommerce-variation-sku">' . __('SKU:') . ' ' . $variation_data['sku'];
        }
    
        // Inserting GTIN
        if( get_post_meta( $variation->get_id(), 'barcode', true ) ){
            $gtin = get_post_meta( $variation->get_id(), 'barcode', true );
            $html .= '</div><div class="woocommerce-variation-gtin">' . __('GTIN:') . ' ' . $gtin;
        }
    
        // Using the variation description to add dynamically the SKU and the GTIN
        $variation_data['variation_description'] .= $html;
    
        return $variation_data;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.


    Now your answer code is a bit outdated since Woocommerce 3 and with some errors.

    Also you should exclude varible products from it as with my answer code, you get now the SKU and GTIN for product variations of the variable products.

    Here is the revisited code:

    add_action('woocommerce_single_product_summary', 'display_product_sku_and_gtin', );
    function display_product_sku_and_gtin() {
        global $product; 
    
        echo '<p>' . __("SKU:") . ' ' . $product->get_sku() . '</p>';
    
        if( get_post_meta( $product->get_id(), 'barcode', true) )
            echo '<p>' . __("GTIN:") . ' ' . get_post_meta( $product->get_id(), 'barcode', true) . '</p>';                  
    }
    

    Code goes in function.php file of your active child theme (or active theme).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联