Woocommerce | Setcookie then redirect with using hooks
<p>I am using woocommerce on wordpress 4.5</p>

<p>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. </p>

<p>Then after that i set a cookie with the price value in it:</p>

<pre><code>setcookie("prijs_cookie", $prijs, 0, "/");
</code></pre>

<p>(This is still on the same page "printophout.php")</p>

<p>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:</p>

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

<p>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:</p>

<pre><code>function woo_add_prijs() {
global $woocommerce;

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

<p>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.</p>

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

<pre><code>$cart_item_data['unique_key'] = md5( microtime().rand() );
</code></pre>

<p>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).</p>

<p>So to fix that i let the first webpage "printophout.php" redirect to "refresh.php" and then redirect to "<a href="http://example.nl/shopping-cart/?add-to-cart=222" rel="nofollow">http://example.nl/shopping-cart/?add-to-cart=222</a>". 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.</p>

<p>How do i fix this problem so my cookie will be set before "woocommerce_add_to_cart_item_data" hook will be executed.</p>

<p>TL:DR</p>

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