问题
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!
回答1:
When a product variant has the "trialversion" enabled, a new checkbox will be added at the checkout page
woocommerce_save_product_variation
should not contain a foreach loop, the 2nd parameter of the function already contains a counter$i
Normally the problem with the checkboxes should also be solved
function add_to_variation_option( $loop, $variation_data, $variation){
$is_trial = get_post_meta( $variation->ID, '_trialversion', true);
if ( $is_trial == 'yes' ) {
$is_trial = 'checked';
} else {
$is_trial = '';
}
?>
<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( $loop ); ?>]"<?php echo $is_trial;?>/>
</label>
<?php
}
add_action('woocommerce_variation_options', 'add_to_variation_option', 10, 3);
function save_trialversion_option_fields( $variation_id, $i ) {
if ( !empty($_POST['_trialversion']) && !empty( $_POST['_trialversion'][$i] ) ) {
update_post_meta( $variation_id, '_trialversion', 'yes' );
} else {
update_post_meta( $variation_id, '_trialversion', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields', 10, 2 );
/**
* Add checkbox field to the checkout
**/
function my_custom_checkout_field( $checkout ) {
// Get $product object from Cart object
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ) {
// The WC_Product object
$product = wc_get_product( $cart_item['product_id'] );
// Checks the product type, 'variable', returns boolean
if ( $product->is_type( 'variable' ) ) {
// Get variation id
$variation_id = $cart_item['data']->get_id();
// Get post meta
$trialversion = get_post_meta( $variation_id, '_trialversion', true);
// Found
if ( $trialversion == 'yes' ) {
$trialversion = 'found';
// Break loop
break;
}
}
}
// Found
if ( isset($trialversion) && $trialversion == 'found' ) {
echo '<div id="my-new-field">';
woocommerce_form_field( 'my_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I agree'),
'required' => true,
), $checkout->get_value( 'my_checkbox' ));
echo '</div>';
}
}
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1 );
来源:https://stackoverflow.com/questions/61006598/add-checkbox-to-woocommerce-checkout-page-based-on-custom-woocommerce-variation