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).

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

    报告相同问题?

    悬赏问题

    • ¥15 Hadoop中eclipse运行问题
    • ¥15 端口转发器解析失败不知道电脑设置了啥
    • ¥15 Latex算法流程图行号自定义
    • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
    • ¥40 基于51单片机实现球赛计分器功能
    • ¥15 cs2游戏画面卡住,应用程序sid与指挥者sid不匹配
    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.
    • ¥35 电脑放图书馆,这是被黑了吗
    • ¥15 等高线中数据取消科学计数法