30秒。
http://www.themelocation.com/how-to-add-custom-field-to-woocommerce-checkout-page/
這裏是片段。
/**
* Add the field to the checkout page
*/
add_action('woocommerce_after_order_notes', 'some_custom_checkout_field');
functionsome_custom_checkout_field($checkout) {
echo '<div id="some_custom_checkout_field"><h2>' . __('Heading') . '</h2>';
woocommerce_form_field('some_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Additional Field'),
'placeholder' => __('Some hint'),
'required' => true,
), $checkout->get_value('some_field_name'));
echo '</div>';
}
/**
* Process the checkout
*/
add_action(‘woocommerce_checkout_process’, ‘some_custom_checkout_field_process’);
functionsome_custom_checkout_field_process() {
// if the field is set, if not then show an error message.
if (! $_POST[‘some_field_name’]){
wc_add_notice(__(‘Please enter value.’), ‘error’);
}
}
/**
* Update the order meta with field value
*/
add_action(‘woocommerce_checkout_update_order_meta’, ‘some_custom_checkout_field_update_order_meta’);
function some_custom_checkout_field_update_order_meta($order_id) {
if (! empty($_POST[‘some_field_name’])) {
update_post_meta($order_id, ‘Some Field’, sanitize_text_field($_POST[‘some_field_name’]));
}
}
我已經實現了這樣的事情,它應該工作,我還沒有測試過上面的代碼。
希望它有幫助。
來源
2015-10-16 09:12:06
Joe
在購物車頁面上添加它似乎是故意讓它比應該更難。只需在各個產品頁面上使用[Product Add ons](http://www.woothemes.com/products/product-add-ons)。 – helgatheviking