A
A
Alexander Kuznetsov2021-08-19 19:25:35
WordPress
Alexander Kuznetsov, 2021-08-19 19:25:35

How to get $_POST request in functions.php in Wordpress?

Good day!

There is a form:

<form method="POST" action="<?php echo $link_address;?>"> 
<input type="hidden" name="custom_price" id="custom_price" value="<?php echo round( $pricing->hours ); ?>" min="1" />
<input type="hidden" name="priceform" id="pricef" value="<?php echo $pricing->base; ?>" />
<button type="submit" class="btn btn-success">Бронь</button>
</form>


In functions.php
I get the required variable like this:
$pricequery = $_POST['custom_price'];

And then I call it in the price change code:
add_filter( 'woocommerce_get_discounted_price', 'calculate_discounted_price', 10, 2 );
add_filter( 'woocommerce_cart_item_subtotal', 'display_discounted_price', 10, 2 );

function calculate_discounted_price( $price, $values ) {
    $price += $pricequery;
    return $price;
}

function display_discounted_price( $values, $item ) {
    return wc_price( $item[ 'line_total' ] );
}


If you just write:
$price += 10;
Then the price changes accordingly, +10 is added to it
But the code above does not work

The variable from the form is correctly processed on any page - if you write it manually, but for some reason it is not called through functions.php

What am I doing wrong, and how to do so?
Thanks for the hints - pieces of code, answers!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Immortal_pony, 2021-08-19
@Immortal_pony

I don't see in the code that you get it. There is definitely no specified variable inside the calculate_discounted_price function. Well add:

function calculate_discounted_price( $price, $values ) {
    $pricequery = (float)$_POST['custom_price'];
    $price += $pricequery;
    return $price;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question