I wish to make a customized checkboxes in WooCommerce product variation. Find out how to save checked checkboxes in web site to make use of it later? After this i wish to show prodcuts is dependent upon this checkboxes. Checkboxes is an inventory of CPT (wordpress cpt) posts – cities. Variations will probably be displayed through cookies relying on the town chosen and for this i would like to save lots of this checkboxes to submit meta.
perform action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
$is_checked = get_post_meta( $variation->ID, 'metropolis', true );
if ( $is_checked == 'sure' ) {
$is_checked = 'checked';
} else {
$is_checked = '';
}
?>
<p><span>Metropolis:</span>
<?php
$args = array(
'post_type' => 'metropolis',
'posts_per_page' => -1
);
$question = new WP_Query($args); ?>
<?php if ($query->have_posts() ) :
whereas ( $query->have_posts() ) : $query->the_post(); ?>
<label class="" data-tip="<?php esc_attr_e( 'City', 'woocommerce' ); ?>">
<?php esc_html_e( the_title(), 'woocommerce' ); ?>
<enter kind="checkbox" class="checkbox variable_checkbox checbkox-cities" identify="city[<?php echo get_post_field( 'post_name', get_post() ); ?>]"<?php echo $is_checked; ?>/>
</label>
<?php endwhile; wp_reset_postdata(); endif; ?>
</p>
<?php }
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3);
I attempted with beneath code however it works provided that there is just one customized checkbox not a number of, i need assistance to save lots of all checked checkboxes values.
perform action_woocommerce_save_product_variation( $variation_id, $i ) {
if ( ! empty( $_POST['city'] ) && ! empty( $_POST['city'][$i] ) ) {
update_post_meta( $variation_id, 'metropolis', 'sure' );
} else {
update_post_meta( $variation_id, 'metropolis', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'action_woocommerce_save_product_variation', 10, 2 );