自定义字段不会显示在购物车中

时间:2014-12-11 14:06:03

标签: php wordpress textbox woocommerce e-commerce

我在我的产品页面上创建了一些自定义字段,并希望将它们显示在购物车中,但对于我的生活,我无法弄清楚如何使用?

我希望这些字段保存并显示在购物车和管理员的订单中。我不太确定如何显示信息,并希望得到任何帮助。

这是我的代码:

//add the fields to the form
add_action( 'woocommerce_before_add_to_cart_button', 'my_custom_checkout_field' );
function my_custom_checkout_field($fields) {
    echo '<table class="table variations">
        <tr>
            <td class="label"><label class="control-label span3" for="inputTextone">'.__('Text Line 1', 'woocommerce').' </label></td>
            <td class="value"><input type="text" name="text_line_1" id="inputTextone" class="span11"></td>
        </tr>
        <tr>
            <td class="label"><label class="control-label span3" for="inputTexttwo">'.__('Text Line 2', 'woocommerce').' </label></td>
            <td class="value"><input type="text" name="text_line_2" id="inputTexttwo" class="span11"></td>
        </tr>
        <tr>
            <td class="label"><label class="control-label span3">'.__('Additional Info', 'woocommerce').' </label></td>
            <td class="value"><input type="text" name="additional_info" id="inputAdditional" class="span11"></td>
        </tr>   
    </table>';
}

//Store the custom field  
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data_vase', 10, 2 );
function add_cart_item_custom_data_vase( $cart_item_meta, $product_id ) {

  global $woocommerce;
  $cart_item_meta['text_line_1'] = $_POST['text_line_1'];
  $cart_item_meta['text_line_2'] = $_POST['text_line_2'];
  $cart_item_meta['additional_info'] = $_POST['additional_info'];

  return $cart_item_meta; 

 }

//add the data to the session
add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_items_from_session', 1, 3 );
function get_cart_items_from_session( $item, $values, $key ) {

   if (array_key_exists( 'text_line_1', $values ) ) {
        $item['text_line_1'] = $values['text_line_1'];
      }       
   return $item;
}

//display the data on the cart page
add_filter('woocommerce_get_item_data', 'show_custom_data');
function show_cart_data( $custom_fields ) {

   $custom_fields['text_line_1'] = $_POST['text_line_1'];

   return $custom_fields;

}

1 个答案:

答案 0 :(得分:1)