Ask your WordPress questions! Pay money and get answers fast! (more info)

Set Temp variable to use at Checkout WordPress

How do I set a Temporary variable with a click?

----Background-----
Upon login I'm opening a Page with 2 links (using form action)

<form action="http://mydomain.com/index.php/shop/" method="post" name="ordertype"><input name="ordertype" type="submit" value="local_pickup" /></form>

There is another link with value set to local_pickup

This allows me to set ordertype variable. But obviously unfortunately its lost on page refresh.

Now, I can set this to a temp variable using $ordertype=$_POST["ordertype"];

I've placed it in a Filter which displays the logged in used but I think that filer is called every page load !

//Display Loggedin User
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items){
if(is_user_logged_in())
{
if ((empty($ordertype)) { // only assign is $ordertype is empty!
$ordertype=$_POST["ordertype"];
}
$user=wp_get_current_user();
...

<strong>Where can I do this so its only once so $ordertype remains set?</strong>

[For some reason even with checking for "empty" $ordertype is being reset
if ((empty($ordertype)) { // only assign is $ordertype is empty!
$ordertype=$_POST["ordertype"];
} ]

Then before checkout filter, I want to <strong>set the Chosen Shipping method</strong>

add_action( 'woocommerce_before_checkout_shipping_form', 'before_shipping');
function before_shipping( $checkout ) {
WC()->session->set( 'chosen_shipping_methods', array($ordertype) );
WC()->cart->calculate_totals();
}

Answers (1)

2015-10-22

Rempty answers:

Use a session variable, now you dont need to reasign each time.

$_SESSION['ordertype']=$_POST['ordertype'];


bas mistry comments:

Hi

Actually, I already tried that first.
It kept clearing every time I changed the page or pressed next (as in next shop page)

I read some posts about $_SESSION and Caching plugins (originally site was with WPEngine but I had to work with their setup so I move the site to another host)

So I tried $_POST and assign that to $ordertype as per my original post

---------------------------
Ok, so now I have:

//Display Loggedin User
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items){
if(is_user_logged_in())
{
$_SESSION['ordertype']=$_POST['ordertype'];
if ((empty($ordertype)) {
$ordertype=$_POST['ordertype'];
}
$user=wp_get_current_user();
...

I'm assuming this is where I need to put the code

(I kept the $_POST stuff too)

I can display both but as soon as I change the page - its gone !

Maybe the filter is causing the problem because its being called more than one?


Rempty comments:

you must not use $ordertype, now is $_SESSION['ordertype].


Rempty comments:

Try this

if(is_user_logged_in())
{
if(isset($_POST['ordertype']){ //Asign only when you send your form
$_SESSION['ordertype']=$_POST['ordertype']
}
$user=wp_get_current_user();
....


bas mistry comments:

No, still the same. (corrected a couple of typos)

I can add a product to cart ... fine since its using Ajax
goto bottom of page & select the next page & its cleared again !


This is what I have now..

if(is_user_logged_in())

{

if(isset($_POST['ordertype'])) { //Asign only when you send your form

$_SESSION['ordertype']=$_POST['ordertype'];

}

$user=wp_get_current_user();
...


Rempty comments:

OK i installed a wp + wc and made some tests.
First:

<form action="/test/shop/" method="post" name="ordertype"><input name="ordertype" type="submit" value="local_pickup" /></form>

And in the functions.php not in the filter

if(isset($_POST['ordertype'])){
$_SESSION['ordertype']=$_POST['ordertype'];
}
print_r($_SESSION['ordertype']);


bas mistry comments:

Ok tried that ..

the code was inside a filter which displays the logged in user name

I moved it out of there so the code above is on its own (not inside a filter)

Dosen't work - the print_r($_SESSION['ordertype']); causes error 500 (white screen)

change it to


//Set Session
if(isset($_POST['ordertype'])){
$_SESSION['ordertype']=$_POST['ordertype'];
}
echo "<span class='test'><p>$ordertype</p></span>";
//print_r([$_SESSION['ordertype']);


I can see the Session variable is set but when I goto checkout the Delivery is set to the Default ie. not updated from session.

I tied adding $ordertype to the above and display next to username in menu with
$items .= '<li><a href=""><strong><font color="white">Order Type:'.$ordertype.'</font></strong></a></li>';
$items .= '<li><a href=""><strong><font color="white">Chosen:'.$_SESSION['ordertype'].'</font></strong></a></li>';

both are set initially after from submits but BOTH are empty when page changes


Rempty comments:

Is a iis server? Can you test on apache?


Rempty comments:

Maybe adding session_start() can help

if(isset($_POST['ordertype'])){
session_start();
$_SESSION['ordertype']=$_POST['ordertype'];
}

I test this code and is working (localhost apache).

add_action( 'woocommerce_before_checkout_shipping_form', 'before_shipping');
function before_shipping( $checkout ) {
WC()->session->set( 'chosen_shipping_methods', array($_SESSION['ordertype']) );
WC()->cart->calculate_totals();
}


bas mistry comments:

It is Apache

Tried adding start session - no luck

I'll try this on another host in a bit


bas mistry comments:

ok,

New Host (still Apache)

but basic installation with Storefront theme (I'm using Avada on the other site)

I begining to suspect it was Avada theme doing something but Storefront behaves the same

So, I have the Input / Pots form on a page

and have added this //Set Session
if(isset($_POST['ordertype'])){
session_start();
$_SESSION['ordertype']=$_POST['ordertype'];
echo "<span class='test'><p>$ordertype</p></span>";
}

//set shipping
add_action( 'woocommerce_before_checkout_shipping_form', 'before_shipping');
function before_shipping( $checkout ) {
WC()->session->set( 'chosen_shipping_methods', array($_SESSION['ordertype']) );
WC()->cart->calculate_totals();
echo "<span class='test'><p>$ordertype</p></span>";
}


same - (1) Session is cleared & (2) Cart Shipping goes to default

//Set Session
if(isset($_POST['ordertype'])){
session_start();
$_SESSION['ordertype']=$_POST['ordertype'];
$ordertype=$_POST['ordertype'];
echo "<span class='test'><p>$ordertype</p></span>";
}

//set shipping
add_action( 'woocommerce_before_checkout_shipping_form', 'before_shipping');
function before_shipping( $checkout ) {
// WC()->session->set( 'chosen_shipping_methods', array($_SESSION['ordertype']) );
WC()->session->set( 'chosen_shipping_methods', array('free_shipping') );
WC()->cart->calculate_totals();
echo "<span class='test'><p>$ordertype</p></span>";
}


with this nothing is echoed so $ordertype is blank

But Manually set shipping - works


Rempty comments:

Ok, i tried on other hosting, and found the error.
In the functions.php

session_start();
if(isset($_POST['ordertype'])){
$_SESSION['ordertype']=$_POST['ordertype'];
}


And add this filter, because in the cart page(checkout page is working) the default shipping is not being overridden by the chossen before.

add_action( 'woocommerce_before_cart', 'before_shipping');