0

I'm trying to pass some metadata from the products page to the invoice page to show the users after they check out.

I've got everything to work apart from the getting the actual data the user has inputed from the $POST as you can see below.

Add to cart function - $POST problem is here

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',1,10);

function wdm_add_item_data($cart_item_data, $product_id) {

    global $woocommerce;
    $new_value = array();
    $new_value['chest'] = "This Works"; //but
    $new_value['shoulders'] = $_POST['shoulders']; //This doesn't

    if(empty($cart_item_data)) {
        return $new_value;
    } else {
        return array_merge($cart_item_data, $new_value);
    }
}

HTML

    <tr>
        <th>
            <label for="chest" id="chest_text">
            <?php _e('Chest Width (inches)', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="chest" id="chest" value="<?php if (!$user_ID==0) {echo esc_attr( get_the_author_meta( 'chest', $user_ID ));} ?>" class="regular-text" /><br />
        </td>
    </tr>

    <tr>
        <th>
            <label for="shoulders" id="shoulders_text">
            <?php _e('Shoulder Width (inches)', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="shoulders" id="shoulders" value="<?php if (!$user_ID==0) {echo esc_attr( get_the_author_meta( 'shoulders', $user_ID ));} ?>" class="regular-text" /><br />
        </td>
    </tr>

I have ajax enabled on the add to cart button and I know the code runs because hard coded string get passed through and displayed on the invoice but I don't see why the POST isn't delivering the data the user has entered.

FULL HTML

<table class="form-table">

    <tr>
        <th>
            <label for="chest">
            <?php _e('Chest Width', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="chest" id="chest" value="<?php echo esc_attr( get_the_author_meta( 'chest', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your chest width.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->

    <tr>
        <th>
            <label for="shoulders">
            <?php _e('Shoulder Width', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="shoulders" id="shoulders" value="<?php echo esc_attr( get_the_author_meta( 'shoulders', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your shoulders.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->

    <tr>
        <th>
            <label for="sleeve">
            <?php _e('Sleeve Length', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="sleeve" id="sleeve" value="<?php echo esc_attr( get_the_author_meta( 'sleeve', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your sleeve length.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->

    <tr>
        <th>
            <label for="arm">
            <?php _e('Arm Length', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="arm" id="arm" value="<?php echo esc_attr( get_the_author_meta( 'arm', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your arm length.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->

    <tr>
        <th>
            <label for="highwaist">
            <?php _e('High Waist', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="highwaist" id="highwaist" value="<?php echo esc_attr( get_the_author_meta( 'highwaist', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your high waist measurement.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->

    <tr>
        <th>
            <label for="lowwaist">
            <?php _e('Low Waist', 'eribootstrap'); ?>
        </label>
        </th>
        <td>
            <input type="text" name="lowwaist" id="lowwaist" value="<?php echo esc_attr( get_the_author_meta( 'lowwaist', $user->ID ) ); ?>" class="regular-text" /><br />
            <span class="description"><?php _e('Please enter your low waist measurement.', 'eribootstrap'); ?></span>
        </td>
    </tr><!-- field ends here -->
</table>

EDIT: fixed a typo.

EDIT 2: Added full HTML

  • use this => http://php.net/manual/en/function.error-reporting.php and you'll see the undefined index notice – Funk Forty Niner Jul 28 '15 at 13:58
  • now you edited your question with the typo fix, **without marking it as an edit**. You also mention Ajax so show us that code and your full HTML form with tags. – Funk Forty Niner Jul 28 '15 at 14:06
  • I didn't write the Ajax code; I was saying that it is enabled by default in Wordpress and I haven't changed it. I forgot to mark the edit, I'll do that now. – thanks_in_advance Jul 28 '15 at 14:10
  • use a ternary operator on the POST array or use `isset()` or `!empty()` on the variable/POST. make sure you are indeed using a POST method somewhere and that is what's unclear as well as if/where `$user_ID` is defined. Hard to say without NOT seeing your HTML form's tags. – Funk Forty Niner Jul 28 '15 at 14:14
  • I've added the full HTML as I suspect I'm doing something wrong there; I tried adding the following line in '$new_value['shoulders'] = isset($_POST['shoulders']) ? $_POST['shoulders'] : "Still doesn't work";' but I get the "Still doesn't work" text appearing. – thanks_in_advance Jul 28 '15 at 14:21
  • if that's your full code as you state, then you're missing `
    ` tags along with a POST method and optionally an action. Least, that's what I get from all this.
    – Funk Forty Niner Jul 28 '15 at 14:24
  • so.. where are we here? I asked you about the form tags etc. – Funk Forty Niner Jul 28 '15 at 14:44
  • I have moved on. Good luck. – Funk Forty Niner Jul 28 '15 at 14:57

1 Answers1

0

I think it's just a typo. The HTML says "shoulders" but your PHP code says "shoulder". Check what's posted using your browser's developer tools, or your PHP debugger.

André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • you voted to close and posted an answer. Hm.. – Funk Forty Niner Jul 28 '15 at 14:00
  • Yes, I voted "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." :) – André Laszlo Jul 28 '15 at 14:04
  • you should have chosen the "undefined index..." duplicate version instead ;-) some see this type of answer and voting to close as a form of monopoly. – Funk Forty Niner Jul 28 '15 at 14:05
  • OP made an edit, without marking it as such. It was just that; a typo but doubt that it will solve the issue. plus, potentially a lot of missing code. – Funk Forty Niner Jul 28 '15 at 14:07
  • The typo was introduced when I was typing up the question; it doesn't exist in the code I'm testing (I've updated the question). Thanks Andre – thanks_in_advance Jul 28 '15 at 14:09