0

On the WP homepage I have a Form like this:

<form action="'.esc_url( admin_url('admin-post.php')).'" method="post">
   //etc.
   //etc.

and in the file functions.php I have this snipped of code.

    function redirect_form() {
        if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {


            if ($_POST["var1"] == 'product1'){
                wp_redirect(home_url('/product/product1/')); exit;
            // etc.
            // etc.

Everything is working as expected.

But the question is how can I get those variables on the WooCommerce single product page? I want echo those variables on the page of the product.

UPDATED:

I am trying the same thing with global variables. What I am missing?

function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {

    global $abc;
    $abc = $_POST["var2"];

    if ($_POST["var1"] == 'product1'){
        wp_redirect(home_url('/products/product1/')); exit;

    // etc.
    // etc.

then

add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
    global $product, $abc;
    echo $abc;
}

It is not working. What I am missing?

UPDATED 2

then I want adjust price of the product based on those variables. Something like this.

function return_custom_price($price, $product) {
    global $post, $blog_id, $abc;
    $price = get_post_meta($post->ID, '_regular_price');
    $post_id = $post->ID;
    $price = ($price[0] + $abc);
    return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
  • Are these variables you need for the single product page a one-time use based on user's input? Or are these variables going to stick around if a user navigates away from the product page and then comes back? – ChristopherJones Jun 11 at 18:58
  • @ChristopherJones . Good question. I don't know how to answer. I want adjust price of the product based on those variables. – Jozef P. Jun 11 at 19:08
0

I think you can maybe use $_SESSIONS or a $_GET var.

Option 1: (I think this will work best for you situation)

function redirect_form() {
  if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {

    $_SESSION['var2'] = htmlentities(stripslashes(trim($_POST["var2"])), ENT_QUOTES);

    if ($_POST["var1"] == 'product1'){
        wp_redirect(home_url('/products/product1/')); exit;

///// And In your price adjustment call

function return_custom_price($price, $product) {
    global $post, $blog_id;
    $price = get_post_meta($post->ID, '_regular_price');
    $post_id = $post->ID;
    $price = ($price[0] + $_SESSION['var2']);
    return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

Note: And at the top of your functions.php file, you'll need to start $_SESSIONS in order to use them.

function register_my_session(){
  if( !session_id()){
    session_start();
  }
}

add_action('init', 'register_my_session');

Option 2: (If you run into $_SESSION issues)

function redirect_form() {
  if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {

    $url_parm = htmlentities(stripslashes(trim($_POST["var2"])), ENT_QUOTES);

    if ($_POST["var1"] == 'product1'){
        wp_redirect(home_url('/products/product1/?var2='.$url_parm)); exit;

///// And In your price adjustment call

function return_custom_price($price, $product) {
    global $post, $blog_id;
    $price = get_post_meta($post->ID, '_regular_price');
    $post_id = $post->ID;
    $price = ($price[0] + $_GET['var2']);
    return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);

If you take Option 2, I would maybe think about protecting doing some checks and balances in your return_custom_price() call to make sure you have clean data;

Hope that helps and let me know if you running into any issues!!

  • Thanks. I am not sure if I need SESSION. If the user comes second time . He needs fill the form again. So the price wont be the same. You know, the price is based on the input. – Jozef P. Jun 11 at 19:40
  • You may not need it for a returned visitor. But when you hit wp_redirect() you are going to lose anything that you saved to $abc. So I think you'll need to store that $abc in either a cookie or a $_SESSION. Or you could pass that $abc value through the URL on wp_redirect and pull it down with $_GET on your single product. I can edit my answer to work through that if you'd like? – ChristopherJones Jun 11 at 20:15
  • Thanks. It would be great. So because of the redirect I am unable to get those global variables? Thats the reason? – Jozef P. Jun 11 at 21:53
  • 1
    I think I am going for this approach. I didn't found anything else. – Jozef P. Jun 12 at 19:00
-2

If you're looking to customize what's displayed on product pages, there are a couple of ways you can do this:

1) Overriding the product page templates: The components that make up a product page can be found in wp-content/plugins/woocommerce/templates, if you want to add variables to one or more parts of a product page, you can override that part of the page within your currently set theme.

For example if you wanted to add variables to the short description, you could copy the short description template:

wp-content/plugins/woocommerce/templates/single-product/short-description.php

to your currently set theme:

yourtheme/woocommerce/single-product/short-description.php`

and add variables/change the structure on your copy. You can do the same with the tabs in the product pages (e.g. additional information & description), check out the files in plugins/woocommerce/templates/single-product/tabs/.

This is useful if you want to make major changes to a section, a downside is that you may need to re-copy the file for compatability if WooCommerce developers make changes to that template.

2) Add variables via filters: Some of the elements have filters, you could manipulate the data that would be used in each section using these.

Using the short description as an example again, you could add a hook to manipulate the data like so:

// define the woocommerce_short_description callback 
function filter_woocommerce_short_description( $post_post_excerpt ) { 
    // make filter magic happen here... 
    return $post_post_excerpt; 
}; 

// add the filter 
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 ); 

Taken from hookr.io

You could do this in your functions.php file or inside a custom plugin. The benefit of this is that it's a bit more robust against WooCommerce developer changes to the template, but you're more limited in terms of the changes you can make.

Update based on clarification

I think I misunderstood your question, if you're asking about variable scope, this is more of a PHP question than a WordPress question. You could declare your variable as a global, which you can then access anywhere.

function redirect_form() {#
    /* This is now globally scoped, you can access it anywhere */
    global $my_var;
    $my_var = $_POST["var1"];
}

You'll then need to re-define you're global wherever you want to use it, so wherever your manipulating your product page code you can access it like this:

global $my_var;
echo $my_var;

Just be careful what you name your variable as you don't want to overwrite something important. I'd always try to avoid using globals if possible, depending on what you're trying to do, there may be a better/safer way of sharing this variable, but I wouldn't be able to suggest a better approach without fully knowing what it is you're trying to achieve.

  • No. I can't get $_POST variable on the product page. I know how to do that. But the variable is not available on the product page. – Jozef P. Jun 9 at 4:45
  • @JozefP. Ahh I think I misunderstood you, I added an extra section to the question which hopefully addresses what you're trying to do. – Jamqes Barndonius Jun 11 at 17:06
  • Thanks. On the homepage I have a form with 3 inputs. 1. input - I want redirect users on the product page (woocommerce). 2 input - I want adjust price of the product based on the second input. 3 input - I just want store some additional data. My issue is that those variables are not availabe. – Jozef P. Jun 11 at 18:54
  • thanks. I added an extra section updated with global variables. What I am missing? – Jozef P. Jun 11 at 19:05
  • global $my_var; will not pull over to the destination page after being set on the source page. $my_var will need to get set again on the destination page. Once a Basic PHP var is set, a page reload will reset it. – ChristopherJones Jun 12 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.