我正在尝试将一些元数据从产品页面传递到发票页面,以便在用户结帐后向他们显示。
除了获取用户从$ POST输入的实际数据之外,我已经完成了所有工作,如下所示。
添加到购物车功能 - $ POST问题在这里
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>
我在添加到购物车按钮上启用了ajax,我知道代码运行是因为硬编码字符串被传递并显示在发票上但我不明白为什么POST没有传递用户输入的数据
完整的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>
编辑:修正了一个拼写错误。
编辑2:添加了完整的HTML
答案 0 :(得分:0)
我认为这只是一个错字。 HTML说“肩膀”但你的PHP代码说“肩膀”。使用浏览器的开发人员工具或PHP调试器检查发布的内容。