Support

Account

Home Forums Feature Requests shortcode for acf variation field with current variation ID

Unread

shortcode for acf variation field with current variation ID

  • Hi everyone,
    I try to create a UPC field for my variable product and put it somewhere on the single product page. I am able to find the script code for the current variation id based on the attribute selected, but I don’t know how to connect the script to the get_field().

    
    <?php 
    
    function product_id_shortcode($loop, $variation_data, $variation) {
    
    if ( is_product() ){
        global $product;
        if( $product->is_type('variable') ){
    ?> <script>
          jQuery(document).ready(function($) {
    
             $('input.variation_id').change( function(){
                if( '' != $('input.variation_id').val() ) {
                    
                   var variation_id = $('input.variation_id').val();                     document.getElementById("varid").innerHTML = variation_id;
                          }
    });
              
          });
    
                    </script>
    
      <?php 
               
            echo get_field( '_upc', ?><code id="varid"></code><?php );    
              
        
        }
        else{
            echo get_field( '_upc', $product->get_id());
        }
        
        
    }
    }
        
    
    add_shortcode('product_id_shortcode', 'product_id_shortcode');
    
    ?>
    

    I understand the get_field($selector, [$post_id], [$format_value]);.
    I want this [$post_id] change based on the attribute selected value.
    I found the script code below the I can get the current variation ID base on the attribute selected.

    
    <script>
          jQuery(document).ready(function($) {
    
             $('input.variation_id').change( function(){
                if( '' != $('input.variation_id').val() ) {
                    
                   var variation_id = $('input.variation_id').val();                     document.getElementById("varid").innerHTML = variation_id;
                          }
    });
              
          });
    
                    </script>
    

    However, I don’t how to connect the script to [$post_id]. I had tried to change [$post_id] to ?><code id="varid"></code><?php, but it was not correct way.

    
     echo get_field( '_upc', ?><code id="varid"></code><?php ); 
    

    Please, can someone help me to fix it or another way to use acf variation field based on the current attribute selected.
    Thank you and have a good day.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.