Customise Currency hook - 'Price' + 15%

I’d like to display an additional 15% on the ad price (excluding Tiers / Extras), so the default value will be shown on each ad on the primary / secondary page.

After customizing the currency hook as follows:

public function get_display_value() {
		if ( ! is_null( $this->value ) && hp\is_plugin_active( 'woocommerce' ) ) {
			// Checks if field name is "price".
			if ( $this->get_name() === 'price' ) {
				// Increases the value by 15% if the field name is "price".
				$this->value *= 1.15;
			}

			return hivepress()->woocommerce->format_price( $this->value );
		}
	}

I thought it only worked with the get_name() === ‘price’ except that what I hadn’t understood was that price tiers / extras are also calculated from the ‘price’ field. So when I add 15% to the price field, whatever the context etc., it will also add 15% to the extras / tiers.

I’ve tried various things such as adding 15% only for the first occurance of the price field, but it didn’t work. I’ll need some help if possible.

public function get_display_value() {
        // Variable statique pour suivre si le champ "price" a déjà été traité
        static $firstPriceProcessed = false;

        // Ajoutez une déclaration de débogage pour voir le nom du champ.
        error_log('Field name: ' . $this->get_name());

        // Vérifiez si le plugin WooCommerce est actif et si la valeur n'est pas nulle.
        if ( ! is_null( $this->value ) && hp\is_plugin_active( 'woocommerce' ) ) {
            // Vérifiez si le nom du champ est "price".
            if ( $this->get_name() === 'price' ) {
                // Si c'est la première occurrence du champ "price", appliquer les 15%
                if (!$firstPriceProcessed) {
                    $firstPriceProcessed = true;
                    error_log('Applying 15% increase to the first price field.');

                    // Obtenez le contexte du champ.
                    $context = $this->get_context();

                    // Vous pouvez également vérifier le contexte pour des conditions supplémentaires.
                    // Par exemple, vérifiez si le modèle est "listing".
                    if (isset($context['model']) && $context['model'] === 'listing') {
                        // Augmente la valeur de 15 % si le nom du champ est "price" et que le contexte est correct.
                        $this->value *= 1.15;
                    }
                } else {
                    error_log('Skipping 15% increase for subsequent price fields.');
                }

                // Formatez la valeur en utilisant les fonctions de WooCommerce.
                return hivepress()->woocommerce->format_price( $this->value );
            }
        }
    }

Thank you very much,
Sincerely
Nolan

Ref : Display price including commissions

If you want to change only the initial price display value, please try this PHP code snippet.

add_filter(
	'hivepress/v1/fields/currency/display_value',
	function($value, $field){
		if(hivepress()->helper->is_rest() || 'price' !== $field->get_name()){
			return $value;
		}
		
		return hivepress()->woocommerce->format_price( $field->get_value() * 1.15 );
	},
	1000,
	2
);
1 Like