IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

WordPress PHP Discussion :

Problème de modification de valeur entre 2 méthode [Plugin]


Sujet :

WordPress PHP

  1. #1
    Membre éclairé Avatar de FCL31
    Profil pro
    Inscrit en
    Août 2007
    Messages
    887
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 887
    Par défaut Problème de modification de valeur entre 2 méthode [Plugin]
    Bonjour à tous.

    Mon plugin va avoir pour but de gérer de la location de matériel avec versement d'acompte (j'ai pas trouvé de plugin existant sans me ruiner).

    Dans la fiche produit, je saisi un prix de base ainsi que le prix de l'acompte a verser (mais pour le moment, pas important dans ce post)

    Avant tout voici du code ^^
    J'ai une première méthode :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
        public function add_rental_data_to_cart($cart_item_data, $product_id, $variation_id) {        if (get_post_meta($product_id, '_rental_enabled', true) !== 'yes') {
                return $cart_item_data;
            }
     
     
            if (!isset($_POST['rental_start_date']) || !isset($_POST['rental_start_time']) || !isset($_POST['rental_duration'])) {
                return $cart_item_data;
            }
     
     
            $start_date = sanitize_text_field($_POST['rental_start_date']);
            $start_time = sanitize_text_field($_POST['rental_start_time']);
            $duration = intval($_POST['rental_duration']);
            $duration_unit = get_post_meta($product_id, '_rental_duration_unit', true);
     
     
            // Combine date and time
            $start_datetime = date('Y-m-d H:i:s', strtotime("$start_date $start_time"));
     
     
            // Calculate end datetime
            $booking = new WC_ER_Booking();
            $end_datetime = $booking->calculate_end_date($start_datetime, $duration, $duration_unit);
     
     
            // Get the base price (regular price)
            $product = wc_get_product($product_id);
            $base_price = floatval($product->get_regular_price());
     
     
            // Calculate deposit
            $deposit_type = get_post_meta($product_id, '_rental_deposit_type', true);
            $deposit_amount = floatval(get_post_meta($product_id, '_rental_deposit', true));
     
     
            if ($deposit_type === 'percentage') {
                $deposit = $base_price * ($deposit_amount / 100);
            } else {
                $deposit = $deposit_amount;
            }
     
     
            $cart_item_data['rental_data'] = array(
                'start_datetime' => $start_datetime,
                'end_datetime' => $end_datetime,
                'duration' => $duration,
                'duration_unit' => $duration_unit,
                'base_price' => $base_price,
                'deposit_amount' => $deposit
            );
     
     
            return $cart_item_data;
        }
    La valeur de $cart_item_data['rental_data']['base_price'] correspond bien aux informations de la fiche produit.

    Puis, j'ai la méthode de calcul des prix :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
        public function calculate_rental_price($cart) {
            if (is_admin() && !defined('DOING_AJAX')) {
                return;
            }
     
     
            if (did_action('woocommerce_before_calculate_totals') >= 2) {
                return;
            }
     
     
            foreach ($cart->get_cart() as $cart_item) {
                if (isset($cart_item['rental_data'])) {
                    $rental_data = $cart_item['rental_data'];
                    $total_price = $rental_data['base_price'] * $rental_data['duration'];
                    $cart_item['data']->set_price($total_price);
                }
            }
        }
    Mon problème est que la valeur de $cart_item['rental_data']['base_price'] ne correspond plus alors qu'il n'y a aucun traitement de la valeur ailleurs.

    Voici le code complet du fichier complet dans lequel se trouve les fonctions en question :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
     
    <?php
    if (!defined('ABSPATH')) {
        exit;
    }
     
     
    class WC_ER_Cart {
        public function __construct() {
            add_filter('woocommerce_add_cart_item_data', array($this, 'add_rental_data_to_cart'), 10, 3);
            add_filter('woocommerce_get_item_data', array($this, 'display_rental_data_in_cart'), 10, 2);
            add_action('woocommerce_before_calculate_totals', array($this, 'calculate_rental_price'));
            add_filter('woocommerce_add_to_cart_validation', array($this, 'validate_rental_data'), 10, 3);
        }
     
     
        public function add_rental_data_to_cart($cart_item_data, $product_id, $variation_id) {
            if (get_post_meta($product_id, '_rental_enabled', true) !== 'yes') {
                return $cart_item_data;
            }
     
     
            if (!isset($_POST['rental_start_date']) || !isset($_POST['rental_start_time']) || !isset($_POST['rental_duration'])) {
                return $cart_item_data;
            }
     
     
            $start_date = sanitize_text_field($_POST['rental_start_date']);
            $start_time = sanitize_text_field($_POST['rental_start_time']);
            $duration = intval($_POST['rental_duration']);
            $duration_unit = get_post_meta($product_id, '_rental_duration_unit', true);
     
     
            // Combine date and time
            $start_datetime = date('Y-m-d H:i:s', strtotime("$start_date $start_time"));
     
     
            // Calculate end datetime
            $booking = new WC_ER_Booking();
            $end_datetime = $booking->calculate_end_date($start_datetime, $duration, $duration_unit);
     
     
            // Get the base price (regular price)
            $product = wc_get_product($product_id);
            $base_price = floatval($product->get_regular_price());
     
     
            // Calculate deposit
            $deposit_type = get_post_meta($product_id, '_rental_deposit_type', true);
            $deposit_amount = floatval(get_post_meta($product_id, '_rental_deposit', true));
     
     
            if ($deposit_type === 'percentage') {
                $deposit = $base_price * ($deposit_amount / 100);
            } else {
                $deposit = $deposit_amount;
            }
     
     
            $cart_item_data['rental_data'] = array(
                'start_datetime' => $start_datetime,
                'end_datetime' => $end_datetime,
                'duration' => $duration,
                'duration_unit' => $duration_unit,
                'base_price' => $base_price,
                'deposit_amount' => $deposit
            );
     
     
            return $cart_item_data;
        }
     
     
        public function display_rental_data_in_cart($item_data, $cart_item) {
            if (isset($cart_item['rental_data'])) {
                $rental_data = $cart_item['rental_data'];
     
     
                $item_data[] = array(
                    'key' => __('Base Price per Day', 'wc-equipment-rental'),
                    'value' => wc_price($rental_data['base_price'])
                );
     
     
                $item_data[] = array(
                    'key' => __('Duration', 'wc-equipment-rental'),
                    'value' => sprintf(
                        '%d %s',
                        $rental_data['duration'],
                        $rental_data['duration_unit']
                    )
                );
     
     
                $item_data[] = array(
                    'key' => __('Total', 'wc-equipment-rental'),
                    'value' => wc_price($rental_data['base_price'] * $rental_data['duration'])
                );
     
     
                $item_data[] = array(
                    'key' => __('Rental Start', 'wc-equipment-rental'),
                    'value' => date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($rental_data['start_datetime']))
                );
     
     
                $item_data[] = array(
                    'key' => __('Rental End', 'wc-equipment-rental'),
                    'value' => date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($rental_data['end_datetime']))
                );
     
     
                $item_data[] = array(
                    'key' => __('Deposit Required', 'wc-equipment-rental'),
                    'value' => wc_price($rental_data['deposit_amount'])
                );
            }
     
     
            return $item_data;
        }
     
     
        public function calculate_rental_price($cart) {
            if (is_admin() && !defined('DOING_AJAX')) {
                return;
            }
     
     
            if (did_action('woocommerce_before_calculate_totals') >= 2) {
                return;
            }
     
     
            foreach ($cart->get_cart() as $cart_item) {
                if (isset($cart_item['rental_data'])) {
                    $rental_data = $cart_item['rental_data'];
                    $total_price = $rental_data['base_price'] * $rental_data['duration'];
                    $cart_item['data']->set_price($total_price);
                }
            }
        }
     
     
        public function validate_rental_data($passed, $product_id, $quantity) {
            if (get_post_meta($product_id, '_rental_enabled', true) !== 'yes') {
                return $passed;
            }
     
     
            if (!isset($_POST['rental_start_date']) || !isset($_POST['rental_start_time']) || !isset($_POST['rental_duration'])) {
                wc_add_notice(__('Please select rental dates and duration.', 'wc-equipment-rental'), 'error');
                return false;
            }
     
     
            $start_date = sanitize_text_field($_POST['rental_start_date']);
            $start_time = sanitize_text_field($_POST['rental_start_time']);
            $duration = intval($_POST['rental_duration']);
     
     
            // Validate minimum and maximum duration
            $min_duration = get_post_meta($product_id, '_rental_min_duration', true);
            $max_duration = get_post_meta($product_id, '_rental_max_duration', true);
     
     
            if ($duration < $min_duration || $duration > $max_duration) {
                wc_add_notice(
                    sprintf(
                        __('Rental duration must be between %d and %d.', 'wc-equipment-rental'),
                        $min_duration,
                        $max_duration
                    ),
                    'error'
                );
                return false;
            }
     
     
            // Check availability
            $start_datetime = date('Y-m-d H:i:s', strtotime("$start_date $start_time"));
            $booking = new WC_ER_Booking();
            $end_datetime = $booking->calculate_end_date(
                $start_datetime,
                $duration,
                get_post_meta($product_id, '_rental_duration_unit', true)
            );
     
     
            if (!$booking->check_availability($product_id, $start_datetime, $end_datetime)) {
                wc_add_notice(__('The selected dates are not available for rental.', 'wc-equipment-rental'), 'error');
                return false;
            }
     
     
            return $passed;
        }
    }
    Merci d'avance pour votre aide

  2. #2
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 606
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 606
    Par défaut
    Citation Envoyé par FCL31 Voir le message
    la valeur de $cart_item['rental_data']['base_price'] ne correspond plus
    quelle est la valeur ? 0 ? un autre nombre ? ou alors la case n'existe pas dans le tableau ?

  3. #3
    Membre éclairé Avatar de FCL31
    Profil pro
    Inscrit en
    Août 2007
    Messages
    887
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France

    Informations forums :
    Inscription : Août 2007
    Messages : 887
    Par défaut
    Citation Envoyé par mathieu Voir le message
    quelle est la valeur ? 0 ? un autre nombre ? ou alors la case n'existe pas dans le tableau ?
    La valeur est le prix du produit saisi sur la fiche produit.

    Mais je sais pas pourquoi, depuis que j'ai posté ce message, j'ai travaillé sur autre chose et en revenant dessus, les valeurs sont bonnes.

    Me faut quand même vérifier.

    Merci quand même.

    Si je retrouve un problème, je sais où trouver de l'aide.

Discussions similaires

  1. Response Soap - modifer la valeur entre deux balises
    Par ref92 dans le forum XML/XSL et SOAP
    Réponses: 3
    Dernier message: 21/06/2011, 11h28
  2. [C#] Datagrid et modification de valeur
    Par Kaïn dans le forum Windows Forms
    Réponses: 5
    Dernier message: 20/01/2006, 11h48
  3. valeur entre 0000000a et 9999999z
    Par niglo dans le forum Général JavaScript
    Réponses: 8
    Dernier message: 30/06/2005, 09h37
  4. transfert de valeurs entre fonctions js et asp
    Par ericmart dans le forum ASP
    Réponses: 5
    Dernier message: 10/03/2005, 16h18
  5. Passage de valeurs entre fenêtres différentes
    Par Amnesiak dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 16/02/2005, 15h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo