<h1>I am a title!</h1>{% raw %}
// Enqueue script
function logistics_calculator_scripts() {
wp_enqueue_script('logistics-calculator', get_template_directory_uri() . '/logistics-calculator.js', array('jquery'), null, true);
wp_localize_script('logistics-calculator', 'logisticsCalculatorAjax', array('ajaxurl' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'logistics_calculator_scripts');
// Shortcode to display the form
function logistics_calculator_form() {
ob_start();
?>
<form id="logistics-calculator-form">
<div>
<label for="direction">Sūtījuma virziens:</label>
<select id="direction" name="direction">
<option value="to_latvia">Uz Latviju</option>
</select>
</div>
<div>
<label for="lv-postcode">Pasta indekss:</label>
<input type="text" id="lv-postcode" name="lv-postcode" value="1010">
</div>
<div>
<label for="from-country">No:</label>
<select id="from-country" name="from-country">
<option value="BG">Bulgaria</option>
</select>
</div>
<div>
<label for="from-postcode">Pasta indekss:</label>
<input type="text" id="from-postcode" name="from-postcode" value="20010">
</div>
<div id="packages">
<div class="package">
<label for="weight">Sūtījuma svars (kg):</label>
<input type="number" id="weight" name="weight[]" value="50">
<label for="length">Garums (cm):</label>
<input type="number" id="length" name="length[]" value="60">
<label for="width">Platums (cm):</label>
<input type="number" id="width" name="width[]" value="50">
<label for="height">Augstums (cm):</label>
<input type="number" id="height" name="height[]" value="50">
</div>
</div>
<button type="button" id="add-package">Pievienot vēl vienu iepakojumu</button>
<button type="submit">Aprēķināt izmaksas</button>
</form>
<div id="logistics-cost-result"></div>
<?php
return ob_get_clean();
}
add_shortcode('logistics_calculator', 'logistics_calculator_form');
// Handle form submission via AJAX
function calculate_logistics_cost() {
$direction = sanitize_text_field($_POST['direction']);
$lv_postcode = sanitize_text_field($_POST['lv_postcode']);
$from_country = sanitize_text_field($_POST['from_country']);
$from_postcode = sanitize_text_field($_POST['from_postcode']);
$weights = $_POST['weight'];
$lengths = $_POST['length'];
$widths = $_POST['width'];
$heights = $_POST['height'];
$total_weight = array_sum($weights);
$total_volume = 0;
for ($i = 0; $i < count($weights); $i++) {
$total_volume += ($lengths[$i] * $widths[$i] * $heights[$i]) / 1000000; // Convert to m3
}
// Calculate cost (this is just a placeholder calculation)
$cost = ($total_weight * 0.5) + ($total_volume * 10) + 20;
echo json_encode(array('cost' => $cost));
wp_die();
}
add_action('wp_ajax_calculate_logistics_cost', 'calculate_logistics_cost');
add_action('wp_ajax_nopriv_calculate_logistics_cost', 'calculate_logistics_cost');
?>
{% endraw %}