Prova cos'
<?php
/*
Plugin Name: Calcolatore
Plugin URI:
https://pianoweb.e
Description: This plugin adds a custom widget.
Version: 1.0
Author: Dumi PianoWeb
Author URI:
https://pianoweb.e
License: GPL2
*/
//the widget class
class My_Custom_Widget extends WP_WIDGET{
//Main constructor
public function __construct(){
/* .. */
parent::__construct(
'my_custom_widget',
__('Calcolatore', 'text_domain'),
array(
'customize_selective_refresh' =>true,
)
);
}
//Widget form (per backend)
public function form($instance){
/*Set widget defaults*/
$defaults = array(
'number' => '',
'select' => ''
);
//pares current settings with defaults
extract( wp_parse_args( ( array ) $instance, $defaults ) ); ?>
<?php //Widget title ?>
<section id="site_header">
<h1>Calcola il tuo preventivo gratuito</h1>
</section>
<!-- Number field -->
<section class="persons">
<h4>Persone</h4>
<label for=""> Quante persone vuoi seguire?</label>
<input type="number" value="1">
</section>
<!-- Dropdown -->
<section>
<label for="<?php echo $this->get_field_id( 'select' ); ?>"><?php _e( 'Scegli uno o più servizi', 'text_domain' ); ?></label>
<select name="<?php echo $this->get_field_name( 'select' ); ?>" id="<?php echo $this->get_field_id( 'select' ); ?>" class="widefat" multiple>
<?php
// Your options array
$servizi = array (
'Stalking' => '50',
'Infedeltà coniugale' => '100',
'Affidamento minori' => '150',
'Sicurezza informatica' => '50',
'Antitaccheggio' => '20',
);
// Loop through options and add each one to the select dropdown
foreach ( $servizi as $option => $price ) {
echo '<option value="' . esc_attr( $price ) . '" id="' . esc_attr( $servizi ) . '" '. selected( $select, $option, false ) . '>'. $option . '</option>';
} ?>
</select>
</section>
<section>
<div class="button_box mt-2">
<button onclick="calcolaPrezzo()">Calcola</button>
</div>
<div id="prezzo_finale"></div>
</section>
<?php }
//update widget settings
public function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['number'] = isset($new_instance['number']) ? wp_strip_all_tags($new_instance['number']) : '';
$instance['select'] = isset($new_instance['select']) ? wp_strip_all_tags( $new_instance['select']) : '';
return $instance;
}
//Display the widget
public function widget($args, $instance){
extract($args);
//check the widget options
$number = isset( $instance['number'] ) ? $instance['number'] : '';
$select = isset( $instance['select'] ) ? $instance['select'] : '';
//WordPress core before_widget hook (includi sempre)
echo $before_widget;
//mostra widget
echo '<div class="widget-text wp_widget_plugin_box">
<?php //Widget title ?>
<section id="site_header">
<h1>Calcola il tuo preventivo gratuito</h1>
</section>
<!-- Number field -->
<section class="persons">
<h4>Persone</h4>
<label for=""> Quante persone vuoi seguire?</label>
<input type="number" value="1">
</section>
<section>
<select id="mySelectBox" multiple>'
?>
// Your options array
$servizi = array (
'Stalking' => '50',
'Infedeltà coniugale' => '100',
'Affidamento minori' => '150',
'Sicurezza informatica' => '50',
'Antitaccheggio' => '20',
);
// Loop through options and add each one to the select dropdown
foreach ( $servizi as $option => $price ) {
echo '<option value="' . esc_attr( $price ) . '" id="' . esc_attr( $servizi ) . '" '. selected( $select, $option, false ) . '>'. $option . '</option>';
}
</select>
</section>
</div>';
<?php
echo '<p>' . $select . '</p>';
}
}
//Register the widget
function my_register_custom_widget(){
register_widget('My_Custom_Widget');
}
add_action('widgets_init', 'my_register_custom_widget');
?>
<?php
//funzione che calcola il prezzo
/* <script>
function calcolaPrezzo() {
var opzioniSelect = document.getElementById("servizi");
//array vuoto che conterrrà i prezzi dei servizi selezionati
let arrServizi = [];
for(let i=0; i<opzioniSelect.length; i++){
//console.log(opzioniSelect
);
if(opzioniSelect.selected === true){
//se l'elemento è selezionato allora pushalo trasformandolo in un integer
arrServizi.push(parseInt(opzioniSelect.value));
//console.log(arrServizi);
}
}
let sommaArrServizi = 0;
for (let i = 0; i < arrServizi.length; i++) {
sommaArrServizi += arrServizi;
}
//console.log(sommaArrServizi);
//inizializzo il prezzo dei servizi
var prezzoBase = 0;
//Prendo il valore del form
var persone = document.getElementById('persone').value;
var totale = prezzoBase += sommaArrServizi;
var total2 = totale * persone;
return total2;
//console.log(total2);
document.getElementById("prezzo_finale").innerHTML = total2 + " €";
}
</script>
*/
?>