Woocommerce | Setcookie then redirect with using hooks

I am using woocommerce on wordpress 4.5

I initialy had a problem with using woocommerce, I needed the user to be able to select and image change dimension etc. So instead of using the single-page.php from woocommerce i made my own page called “printophout.php”.
I used jcrop plugin to let the user select an image and crop the image. After they have done that the image url(cropped image is stored on server) and the price(based on the size the user selected) are send to the php in printophout.php by a form which uses PHP_SELF to post.

Read More

Then after that i set a cookie with the price value in it:

setcookie("prijs_cookie", $prijs, 0, "/");

(This is still on the same page “printophout.php”)

I also set the cookie for the image url, but if i fix the price cookie i can fix both. after that i used this code to add a product to the cart:

header("Location: http://example.nl/shopping-cart/?add-to-cart=222");

this will add the product to cart but it has 0.0 as a price, thats why i used the below code to change the price based on the value stored in the cookie:

function woo_add_prijs() {
global $woocommerce;

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    if($cart_item['data']->id == 222 && ! empty($_COOKIE['prijs_cookie'])){
        $cart_item['data']->set_price($_COOKIE['prijs_cookie']);
    }
}
}
add_action( 'woocommerce_before_calculate_totals', 'woo_add_prijs', 10, 0);

This code works but, whenever i add a second product the price of both producst will change. example: i get product and the price will be $45, i then go get the same product but as price $55 both products will be changed to $55.

I also tried to use this hook so every product gets a unique key and thus their own line but that did not work:

$cart_item_data['unique_key'] = md5( microtime().rand() );

Then i tried to use the hook “woocommerce_add_to_cart_item_data” this hook will execute code whenever a product is added to the shopping cart so my problem would be fixed if i set the price in that hook because it will be different for all products then. But that did not work because the price would be 0.0 i thought it was because of a cookie not being set because the hook is before the page fully loads and setcookie() needs the next page to be fully loaded(i think).

So to fix that i let the first webpage “printophout.php” redirect to “refresh.php” and then redirect to “http://example.nl/shopping-cart/?add-to-cart=222“. i thought that the refresh.php would count as a full page load so that my cookie would be set but that did not work. I use Header() function to redirect the page.

How do i fix this problem so my cookie will be set before “woocommerce_add_to_cart_item_data” hook will be executed.

TL:DR

Cookies are set data can be used with woocommerce_before_calculate_totals hook but not when i use woocommerce_add_cart_item_data.

Related posts

Leave a Reply

2 comments

  1. For me, the only way you have (without reloading page) is to create / manipulate / update / delete cookies with javascript (and jQuery if needed) or/and javascript/ajax.

    May be it will be better to create 2 cookies instead of one with php:

    • One for the price as you did it, but with javascript.
    • The other for quantity ( the quantity is always 1 at first time)

      This way you will be able to change quantity dynamically via javascript.

    The best solution is Ajax, but is the more complex way. It’s used by WooCommerce in cart processes.

    This could also solve your problem, using woocommerce_add_to_cart_item_data hook. Think about it and come back updating your question, then I will try to help with the code…

    You could use also sessionStorage.

    The only problem with cookies, is that they are not so secure, because they can be manipulated. So setting price in a cookie for a e-commerce web site is not recommended.


    Other solution (only if user is logged in) could be to create a custom usermeta field to store that price. You could also use in that case javascrit/ajax through wordpress admin_ajax, to update price or quantity… This data could be store as an array.

  2. I managed to fix the problem.
    When you set a cookie it will not be automatically set because the web is stateless. So you need to setcookie() then go to a page or reload the page so that html will output. In my refresh page i forgot to put the header of wordpress so no HTML was outputted and thus the cookie was not set.

    Also the woocommerce hook: woocommerce_add_to_cart_item_data will run before any html is outputted so you have to go via another page otherwise the cookie will not be set.