I've added an additional variation option to my products with the following code:
add_action('woocommerce_variation_options', 'he_add_to_variation_option', 10, 3);
function he_add_to_variation_option( $loop, $variation_data, $variation){
$is_trial = (get_post_meta($variation->ID, '_trialversion', true)) ? ' checked' : '';
?>
<label class="tips" data-tip="<?php esc_attr_e( 'Enable this option to make as a trial version', 'woocommerce' ); ?>">
<?php esc_html_e( 'Trial Version?', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_is_trial_version" name="_trialversion[<?php echo esc_attr( $variation->ID ); ?>]"<?php echo $is_trial;?>/>
</label>
<?php
}
add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields' );
function save_trialversion_option_fields( $post_id ) {
if ( isset( $_POST['_trialversion'] ) ){
foreach ( $_POST['_trialversion'] as $productid=>$checked ){
update_post_meta( $productid, '_trialversion', 'yes' );
}
}
}
This works great, it's displayed within the variants and it's saved correctly in the database.
So far, so good.
Now, I would like to add an additional checkbox at checkout, if a product is flagged as "trial version". I'm using the "Germanized" plugin as well, which has options for custom checkboxes, but I can't get it to recognize the changes I've made with the above code.
How would I accomplish the custom checkbox for my trial version variants? With or without Germanized, at this point I just want to get it to work. Maybe there's a free plugin, but if I can just do it by adding some code, that would probably be easier.
The checkbox would have to be a required one to complete the purchase of the trial version.
Hopefully someone has an idea on how to do this. Looking forward to your replies!