I need to add three checkout fields in WooCommerce checkout:
A checkbox labelled "I'm already a member". Checking this will display a field below the checkbox where the person must fill in their member number, which is a 6 digit number. When the member number is filled in correctly, a discount of SEK 200,00 should be applied to every product in the checkout and the checkout amount should automatically be recalculated. The member number field must be required/mandatory when this box is checked.
A checkbox labelled "I'm a member, but in another club". Checking this will display a field below the checkbox where the person must fill in their member number, which is a 6 digit number. The member number field must be required/mandatory when this box is checked.
It must be required/mandatory to choose one of those two options, it shouldn't be possible to place the order without having checked one of the boxes, having filled their member number. It should not be possible to choose both boxes, only one of them is allowed. If a person checks box1 and then box2, box1 automatically should be unchecked and the discount should be removed.
- A checkbox labelled "I have been a volunteer". Checking this will automatically apply a discount of SEK 100,00 to the total checkout amount. This option should be possible to be used with the other options.
For example, 3 products ordered, both box1 and box 3 are checked (and valid member number is filled in), the total discount will SEK 700,00. If 3 products are ordered, both box2 and box 3 are checked, the total discount will be SEK 100,00. If only box1 is checked, the total discount will be SEK 600,00.
I use the Astra theme and have access to the functions.php file.
I have added this to the functions.php. The SEK 100,00 is working, the other two don't work, no discount is applied.
add_action('woocommerce_review_order_before_submit', 'custom_checkout_fields');
function custom_checkout_fields() {
echo '<div class="woocommerce-additional-fields">';
// Kryssruta 1: Jag är medlem i klubben
woocommerce_form_field('is_member_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Jag är medlem i klubben'),
), WC()->session->get('is_member_checkbox'));
echo '<div class="is-member-fields" style="display:none;">';
woocommerce_form_field('member_number', array(
'type' => 'text',
'class' => array('input-text'),
'label' => __('Medlemsnummer'),
'placeholder' => __('Fyll i ditt medlemsnummer'),
), WC()->session->get('member_number'));
echo '</div>';
// Kryssruta 2: Jag har hjälpt till ideellt i klubben
woocommerce_form_field('volunteer_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Jag har hjälpt till ideellt i klubben'),
), WC()->session->get('volunteer_checkbox'));
// Kryssruta 3: Jag är medlem i annan klubb
woocommerce_form_field('other_club_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Jag är medlem i annan klubb'),
), WC()->session->get('other_club_checkbox'));
echo '<div class="other-club-fields" style="display:none;">';
woocommerce_form_field('other_club_number', array(
'type' => 'text',
'class' => array('input-text'),
'label' => __('Medlemsnummer i annan klubb'),
'placeholder' => __('Fyll i ditt medlemsnummer'),
), WC()->session->get('other_club_number'));
echo '</div>';
echo '</div>';
// JavaScript för att visa/dölja anpassade fält baserat på kryssrutor
?>
<script>
jQuery(document).ready(function($) {
$('#is_member_checkbox').change(function() {
if ($(this).is(':checked')) {
$('.is-member-fields').show();
} else {
$('.is-member-fields').hide();
}
recalculateCart();
});
$('#volunteer_checkbox').change(function() {
recalculateCart();
});
$('#other_club_checkbox').change(function() {
if ($(this).is(':checked')) {
$('.other-club-fields').show();
} else {
$('.other-club-fields').hide();
}
recalculateCart();
});
function recalculateCart() {
$('body').trigger('update_checkout');
}
});
</script>
<?php
}
// Applicera rabatt baserat på medlemsstatus
add_action('woocommerce_cart_calculate_fees', 'custom_apply_discounts');
function custom_apply_discounts($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if (isset($_POST['post_data'])) {
parse_str($_POST['post_data'], $post_data);
} else {
$post_data = $_POST;
}
if (isset($post_data['is_member_checkbox']) && $post_data['is_member_checkbox'] === '1' && !empty($post_data['member_number']) && preg_match('/^\d{6}$/', $post_data['member_number'])) {
$discount_amount = 200;
$cart->add_fee(__('Medlemsrabatt'), -$discount_amount, false);
WC()->session->set('is_member_checkbox', true);
WC()->session->set('member_number', $post_data['member_number']);
} else {
WC()->session->set('is_member_checkbox', false);
WC()->session->set('member_number', '');
}
if (isset($post_data['volunteer_checkbox']) && $post_data['volunteer_checkbox'] === '1') {
$volunteer_discount = 80; // Uppdatera rabattbeloppet här
$cart->add_fee(__('Ideell rabatt'), -$volunteer_discount, false);
WC()->session->set('volunteer_checkbox', true);
} else {
WC()->session->set('volunteer_checkbox', false);
}
if (isset($post_data['other_club_checkbox']) && $post_data['other_club_checkbox'] === '1' && !empty($post_data['other_club_number']) && preg_match('/^\d{6}$/', $post_data['other_club_number'])) {
foreach ($cart->get_fees() as $fee_key => $fee) {
if ($fee->get_name() === 'Medlemsrabatt') {
$cart->remove_fee($fee_key);
break;
}
}
WC()->session->set('other_club_checkbox', true);
WC()->session->set('other_club_number', $post_data['other_club_number']);
} else {
WC()->session->set('other_club_checkbox', false);
WC()->session->set('other_club_number', '');
}
}
// Återställ kryssrutor vid sidan uppdateras
add_action('woocommerce_before_checkout_form', 'custom_reset_session');
function custom_reset_session() {
if (is_checkout() && WC()->session->get('order_awaiting_payment') !== '1') {
WC()->session->set('is_member_checkbox', false);
WC()->session->set('volunteer_checkbox', false);
WC()->session->set('other_club_checkbox', false);
WC()->session->set('member_number', '');
WC()->session->set('other_club_number', '');
}
}
// Validera Nonce för att undvika nonce_failure
add_action('woocommerce_after_checkout_validation', 'custom_validate_nonce');
function custom_validate_nonce($posted_data) {
if (isset($posted_data['is_member_checkbox']) && $posted_data['is_member_checkbox'] === '1') {
if (!isset($posted_data['is_member_checkbox_nonce']) || !wp_verify_nonce($posted_data['is_member_checkbox_nonce'], 'is_member_checkbox_action')) {
wc_add_notice(__('Ogiltig förfrågan. Vänligen uppdatera sidan och försök igen.'), 'error');
}
}
if (isset($posted_data['other_club_checkbox']) && $posted_data['other_club_checkbox'] === '1') {
if (!isset($posted_data['other_club_checkbox_nonce']) || !wp_verify_nonce($posted_data['other_club_checkbox_nonce'], 'other_club_checkbox_action')) {
wc_add_notice(__('Ogiltig förfrågan. Vänligen uppdatera sidan och försök igen.'), 'error');
}
}
}