2016-06-11 186 views
2

我在我的WooCommerce單一產品上有自定義字段。它發送到購物車罰款,它顯示在結帳罰款,它顯示在儀表板罰款。將自定義字段數據添加到WooCommerce訂單

我現在試圖做的是將該值設置爲訂單頁面中的自定義字段,以便我可以在需要時修改文本。出於某種原因,當我提交表單時,此步驟不起作用。

,我在我的functions.php文件中使用的代碼:

// Add the field to the product 
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field'); 

function my_custom_checkout_field() { 
    echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>'; 
    echo '<label>fill in this field</label> <input type="text" name="my_field_name">'; 
    echo '</div>'; 
} 

// Store custom field 
function save_my_custom_checkout_field($cart_item_data, $product_id) { 
    if(isset($_REQUEST['my_field_name'])) { 
     $cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name']; 
     /* below statement make sure every add to cart action as unique line item */ 
     $cart_item_data['unique_key'] = md5(microtime().rand()); 
    } 
    return $cart_item_data; 
} 
add_action('woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2); 

// Render meta on cart and checkout 
function render_meta_on_cart_and_checkout($cart_data, $cart_item = null) { 
    $custom_items = array(); 
    /* Woo 2.4.2 updates */ 
    if(!empty($cart_data)) { 
     $custom_items = $cart_data; 
    } 
    if(isset($cart_item['my_field_name'])) { 
     $custom_items[] = array("name" => 'My Field', "value" => $cart_item['my_field_name']); 
    } 
    return $custom_items; 
} 
add_filter('woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2); 

// Display as order meta 
function my_field_order_meta_handler($item_id, $values, $cart_item_key) { 
    if(isset($values['my_field_name'])) { 
     wc_add_order_item_meta($item_id, "my_field_name", $values['my_field_name']); 
    } 
} 
add_action('woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3); 

/** THIS IS WHERE I'M STUCK **/ 

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process'); 
function my_custom_checkout_field_process() { 
    global $woocommerce; 

    // Check if set, if its not set add an error. This one is only requite for companies 
    if ($_POST['billing_company']) 
     if (!$_POST['my_field_name']) 
      $woocommerce->add_error(__('Please enter your XXX.')); 
} 
// Update the user meta with field value 
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta'); 
function my_custom_checkout_field_update_user_meta($user_id) { 
    if ($user_id && $_POST['my_field_name']) update_user_meta($user_id, 'my_field_name', esc_attr($_POST['my_field_name'])); 
} 

// Update the order meta with field value 

add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta'); 
function my_custom_checkout_field_update_order_meta($order_id) { 
    if ($_POST['my_field_name']) update_post_meta($order_id, 'My Field', esc_attr($_POST['my_field_name'])); 
} 

目前發生的事情截圖:

Screenshot of what currently happens



我想什麼情況發生:

what I would like to happen


任何幫助將不勝感激。

回答

4

更新時間:與Woocommerce版本的兼容性3+

你丟失了一些功能顯示訂單編輯頁面上此自定義字段值:

/** 
* Display field value on the order edit page 
*/ 
add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1); 
function my_custom_checkout_field_display_admin_order_meta($order){ 
    $order_id = method_exists($order, 'get_id') ? $order->get_id() : $order->id; 
    echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta($order_id, 'my_field_name', true) . '</p>'; 
} 

在引用鏈接下面,你有所有原始的wooThemes功能工作代碼片段。這是一個優秀的全功能教程。

參考文獻:[自定義使用操作和過濾器校驗字段] [1]


編輯:爲了項目元獲取顯示與您的自定義字段值的自定義標籤

要使用您自定義的字段值(訂購商品代碼)而不是類似m的slu get獲得像「我的字段名稱」的自定義標籤y_field_name,請參閱本胎面:

+0

謝謝你,這肯定把我在正確的軌道,我掙扎的最後一件事上是結帳處理階段。由於我的字段實際上已插入產品頁面,因此如何處理與此字段的結帳,而不必將其作爲文檔中所示的必填字段? –

相關問題