I am migrating some PHP code where we process a shopping cart form before passing it through to Woocommerce.

One of the names of a variables includes a bracket and I am wondering if this is why I cannot seem to post the data?

Essentially we have changed the product page as we need to process additional data in the backend. After manipulating the data we then post it to the shopping cart.

So for instance the original form includes the product ID like this

<input type="hidden" name="add-to-cart" value="230" />

We can post this to the cart using this

$_POST['add-to-cart']= "230";
$woocommerce->cart->add_to_cart($productID);
wp_redirect( $woocommerce->cart->get_checkout_url() );

However we also want to post data from the woocommerce products addon plugin. The original fom looks like this

<input type="text" class="input-text addon addon-custom" data-price="" name="addon-230-cropped-image[cropped-image]" value=""  />

However I have tried to post this data using the two examples below, both of which do not work.

    $_POST['addon-'.$productID.'-original-image-original_image']=  "data";
    $_POST['addon-'.$productID.'-original-image[original_image]']=  "data";
$woocommerce->cart->add_to_cart($productID);
wp_redirect( $woocommerce->cart->get_checkout_url() );

Any idea what I should use to post this data?


Solution:

Probably your variable is threaded as an array and should be captured as:

$_POST['addon-'.$productID.'-original-image']['original_image']

See PHP Variables. The first example explains something like your case.

Code part of first example on PHP Variables:

<form ....>
 <input name="person[0][first_name]" value="john" />
 <input name="person[0][last_name]" value="smith" />
 ...
 <input name="person[1][first_name]" value="jane" />
 <input name="person[1][last_name]" value="jones" />
</form>
<?php
 var_dump($_POST['person']);
 //will get you something like:
 array (
  0 => array('first_name'=>'john','last_name'=>'smith'),
  1 => array('first_name'=>'jane','last_name'=>'jones'),
 )
?>

Related Wordpress search:

, , , ,

Wordpress related questions and answers: