PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to echo custom woocommerce product variation field?

 May 09, 2022     php, product, variations, woocommerce, wordpress     No comments   

Issue

I've added a custom field for each woocommerce product variation.

I've added a datetime input field. Then I will need to auto delete the variation when the datetime value is already past. So auto expire the woocommerce product variation.

This is my code for adding the custom input field.

// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );

// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

//Create New fields for Variations
function variation_settings_fields( $loop, $variation_data, $variation ) {
    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field_date_expire[' . $variation->ID . ']',
            'class'       => '_text_field_date_expire',
            'type'        => 'datetime-local',  
            'label'       => __( 'Date of Expiration', 'woocommerce' ), 
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Expiration of Each Product Variation', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field_date_expire', true )
        )
    );
}


//Save New Fields for Variation
function save_variation_settings_fields( $post_id ) {
    // Text Field
    $text_field = $_POST['_text_field_date_expire'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field_date_expire', esc_attr( $text_field ) );
    }
}

How could I get the input field value per each variations and echo it, so I could use the value for another function and delete the variations automatically?


Solution

you can get custom woocommerce product variation field by using get_post_meta() function

echo  get_post_meta( $post->ID, '_text_field_date_expire', true );


Answered By - Shital Marakana
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing