• Resolved webrevnz

    (@webrevnz)


    Hi there,

    I have one flower website which is selling the different size of Bouquet for example: Grand, Lavish, Sumptuous & Divine. I don’t have photo for all these options. I am showing them on a product detail page with Radio option.

    In the backend of each variation for a product I have added the option for free text input field. Where on setting up the variation I am typing a text “Shown in image”.

    My question is is there any hook available that we can use to alter the output of the radio option to append the text for each variation if in the backend the above text input field is not empty?

    The structure for radio option is:

    <div class="woovr-variation-info">
    <div class="woovr-variation-name">Lavish</div>
    <div class="woovr-variation-price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>139.00</bdi></span></div>
    <div class="woovr-variation-availability"></div>
    </div>

    and I want to add it inside above option like below:

    <div class="woovr-variation-info">
    <div class="woovr-variation-name">Lavish</div>
    <div class="woovr-variation-price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">$</span>139.00</bdi></span></div>
    <div class="woovr-variation-availability"></div>
    <div class="shown-in-image">Shown in image</div>
    </div>

    Any help would really be appreciated.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author WPClever

    (@wpclever)

    Hi @webrevnz

    Please update our plugin to the latest version 2.4.3 then add the below snippet (How to add custom code?):

    add_filter( 'woovr_variation_info', 'woovr_variation_extra_info', 99, 2 );
    function woovr_variation_extra_info( $info, $product ) {
    	return $info . '<div class="woovr-variation-extra">Your custom text for variation ID ' . $product->get_id() . ' here!</div>';
    }

    And the result https://www.screencast.com/t/8CCvku69wlq or https://www.screencast.com/t/IJwOdiJs

    Thread Starter webrevnz

    (@webrevnz)

    Thanks @wpclever.

    It works great ??

    Hi,

    I have this code that creates a custom field in each variation,

    // 1. Add custom field input @ Product Data > Variations > Single Variation
     
    add_action( 'woocommerce_variation_options_pricing', 'fierbourg_add_custom_field_to_variations', 10, 3 );
     
    function fierbourg_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
       woocommerce_wp_text_input( array(
    'id' => 'custom_field[' . $loop . ']',
    'class' => 'short',
    'label' => __( 'Custom Field', 'woocommerce' ),
    'value' => get_post_meta( $variation->ID, 'custom_field', true )
       ) );
    }
      
    // 2. Save custom field on product variation save
     
    add_action( 'woocommerce_save_product_variation', 'fierbourg_save_custom_field_variations', 10, 2 );
     
    function fierbourg_save_custom_field_variations( $variation_id, $i ) {
       $custom_field = $_POST['custom_field'][$i];
       if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
    }
     
    // 3. Store custom field value into variation data
    
    add_filter( 'woocommerce_available_variation', 'fierbourg_add_custom_field_variation_data' );
     
    function fierbourg_add_custom_field_variation_data( $variations ) {
       $variations['custom_field'] = '<div class="woocommerce_custom_field">Dates: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
       return $variations;
    }

    but I can’t adjust your snippet with it. What am I missing

    // 4. Display custom field to product page variations
    add_filter( 'woovr_variation_info', 'woovr_variation_extra_info', 99, 2 );
    
    function woovr_variation_extra_info( $info, $product ) {
    	return $info . '<div class="woovr-variation-extra">Your custom text for variation ID ' . $product->get_id() . ' here!</div>';
    }

    Thanks for your support

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add value from variation custom field radio option’ is closed to new replies.