// Custom montagekosten
// Voeg de checkbox toe aan de afrekenpagina
function wbmz_add_montagekosten_checkbox() {
$checked = WC()->session->get('wbmz_montagekosten') ? 'checked' : '';
$label_text = WC()->session->get('wbmz_montagekosten') ? 'Gratis montage verwijderen' : 'Gratis montage toevoegen';
?>
<div class="wbmz-montagekosten">
<label for="wbmz_montagekosten">
<input type="checkbox" name="wbmz_montagekosten" id="wbmz_montagekosten" value="1" <?php echo $checked; ?> />
<span id="wbmz_montagekosten_label"><?php echo $label_text; ?></span>
</label>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
function calculateFee() {
var subtotal = parseFloat('<?php echo WC()->cart->get_subtotal(); ?>');
var country = '<?php echo WC()->customer->get_billing_country(); ?>';
var state = $('select#billing_state').val() || $('select#shipping_state').val();
var fee = 30;
if ((country === 'NL' && subtotal >= 450) ||
(country === 'BE' && subtotal >= 850) ||
(country === 'DE' && subtotal >= 999)) {
fee = 0;
}
return fee;
}
function updateCart() {
var fee = calculateFee();
var data = {
action: 'wbmz_toggle_montagekosten',
wbmz_montagekosten: $('#wbmz_montagekosten').is(':checked') ? 1 : 0,
fee: fee
};
$.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
if (response.success) {
$('body').trigger('update_checkout');
}
});
}
function updateLabel() {
var isChecked = $('#wbmz_montagekosten').is(':checked');
var fee = calculateFee();
var labelText = isChecked ? 'Montage' : 'Montage';
if (fee === 0) {
labelText = isChecked ? 'Gratis montage' : 'Gratis montage';
}
$('#wbmz_montagekosten_label').text(labelText);
}
$('#wbmz_montagekosten').change(function() {
updateCart();
updateLabel();
});
$('body').on('change', 'select#billing_state, select#shipping_state', function() {
updateCart();
updateLabel();
});
$('body').on('updated_cart_totals', function() {
updateCart();
updateLabel();
});
// Initial label update and cart update when page loads
updateLabel();
updateCart();
});
</script>
<?php
}
add_action('woocommerce_after_order_notes', 'wbmz_add_montagekosten_checkbox');
// Verwerk de AJAX-aanroep en pas de fee toe
function wbmz_toggle_montagekosten() {
if (isset($_POST['wbmz_montagekosten']) && $_POST['wbmz_montagekosten'] == 1) {
WC()->session->set('wbmz_montagekosten', true);
$fee = isset($_POST['fee']) ? floatval($_POST['fee']) : 0;
WC()->session->set('wbmz_montagekosten_fee', $fee);
} else {
WC()->session->set('wbmz_montagekosten', false);
WC()->session->set('wbmz_montagekosten_fee', 0);
}
wp_send_json_success();
}
add_action('wp_ajax_wbmz_toggle_montagekosten', 'wbmz_toggle_montagekosten');
add_action('wp_ajax_nopriv_wbmz_toggle_montagekosten', 'wbmz_toggle_montagekosten');
// Bereken de fee op basis van de sessiestatus
function wbmz_custom_montagekosten_fee() {
if (WC()->session->get('wbmz_montagekosten')) {
$fee = WC()->session->get('wbmz_montagekosten_fee');
if ($fee !== false) {
WC()->cart->add_fee('Montagekosten', $fee, false, '');
}
}
}
add_action('woocommerce_cart_calculate_fees', 'wbmz_custom_montagekosten_fee');
#AJAX #checkout #fee #functions #jQuery #woocommerce