skip to Main Content

I’m trying to display the meta-field “_ts_gtin” of a variable product within the tab “additional_information” of a single product.

I just figured out that I can get the value of a specific variation this way:

get_post_meta( $variations[ 'variation_id' ], '_ts_gtin', true )

Can anyone help me?

2

Answers


  1. You can add this to your functions.php or plugin:

    add_action( 'woocommerce_product_additional_information', 'print_variation_html' );
    
    function print_variation_html($product){
        $variations_id = $product->get_variation_id();   
    
        if(!$variations_id) return;
    
        $variation_meta = get_post_meta( $variations_id , '_ts_gtin', true );
        print_r( $variation_meta );
    }
    
    Login or Signup to reply.
  2. Add the follows code snippet to do the above –

    add_action( 'woocommerce_product_additional_information', 'add_info_in_product_additional_information', 99 );
    
    function add_info_in_product_additional_information( $product ) {
        if( $product ) {
            $children_ids = $product->get_children();
            if( $children_ids ) {
                echo '<ul id="variation-gtin">';
                foreach ( $children_ids as $v_id ) {
                    echo '<li class="item-'.$v_id.'">#' . $v_id . ': ' . get_post_meta( $v_id , '_ts_gtin', true ) . '</li>';
                }
                echo '</ul>';
            }
        }
    }
    
    // scripts
    add_action( 'wp_footer', 'add_scripts_on_found_variation' );
    
    function add_scripts_on_found_variation() {
        ?>
        <script type="text/javascript">
            ( function( $ ) {
                $( 'form.variations_form').on( 'found_variation', function( event, variation ) {
                    $( '#variation-gtin .item-'+variation.variation_id ).show().siblings().hide();
                } );
            } )( jQuery );
        </script>
        <?php
    }
    

    Codes goes to your active theme’s functions.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search