问题
The WooCommerce 3.0 update has not been kind to me. I have added a custom required field to checkout for a domain name, and am having trouble figuring out how to get it to save now. This code adds the field properly still:
add_action( 'woocommerce_after_order_notes', 'add_domain_checkout_field' );
function add_domain_checkout_field( $checkout ) {
echo '<div id="add_domain_checkout_field"><h2>' . __('Domain') . '</h2>';
woocommerce_form_field( 'sitelink_domain', array(
'type' => 'text',
'required' => true,
'class' => array('my-field-class form-row-wide'),
'label' => __('Domain where SiteLink will be installed'),
'placeholder' => __('Enter your URL'),
), $checkout->get_value( 'sitelink_domain' ));
echo '</div>';
}
And I am trying to save it, this way:
add_action( 'woocommerce_checkout_create_order', 'add_domain_to_order_meta', 10, 2 );
function add_domain_to_order_meta( $order, $data ) {
if ( ! empty( $_POST['sitelink_domain'] ) ) {
$order->add_meta_data( 'ssla_sitelink_url', sanitize_text_field( $_POST['sitelink_domain'] ) );
}
}
However the meta does not appear to be added or saved anywhere.
I know that the $_POST
variable is there, I have error logged it out to see.
Testing some grabbing and error logging confuses me further:
$sitelink_domain = $subscription->get_meta_data( 'ssla_sitelink_url' );
error_log( print_r( $sitelink_domain, true ) );
// Output is:
[21-Apr-2017 01:26:27 UTC] Array
(
[0] => stdClass Object
(
[id] => 270086
[key] => _ssla_sitelink_url
[value] => lololol.com
)
[1] => stdClass Object
(
[id] => 270089
[key] => _download_permissions_granted
[value] => 1
)
)
However,
$sitelink_domain = $subscription->get_meta( 'ssla_sitelink_url' );
error_log( 'Domain: ' . $sitelink_domain );
Output is just:
[21-Apr-2017 01:27:39 UTC] Domain:
回答1:
First you need to validate the field when the checkout form is posted and the field is required and not optional using woocommerce_checkout_process
action hook:
add_action('woocommerce_checkout_process', 'domain_checkout_field_process');
function domain_checkout_field_process() {
// Check if it's set and if it's not set, we add an error.
if ( ! $_POST['sitelink_domain'] )
wc_add_notice( __( 'Please enter the domain where the SiteLink will be installed.' ), 'error' );
}
As this is a checkout custom field, you can use woocommerce_checkout_update_order_meta
action hook to save the new field to order custom fields:
add_action( 'woocommerce_checkout_update_order_meta', 'domain_checkout_field_update_order_meta' );
function domain_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['sitelink_domain'] ) ) {
update_post_meta( $order_id, 'ssla_sitelink_url', sanitize_text_field( $_POST['sitelink_domain'] ) );
}
}
Use woocommerce_admin_order_data_after_billing_address
action hook to display the custom field value on the admin order edition page:
add_action( 'woocommerce_admin_order_data_after_billing_address', 'domain_checkout_field_display_admin_order_meta', 10, 1 );
function domain_checkout_field_display_admin_order_meta($order){
// Get the custom field value
$domain_siteLink = get_post_meta( $order->get_id(), 'ssla_sitelink_url', true );
// Display the custom field:
echo '<p><strong>' . __('Domain SiteLink', 'woocommerce') . ': </strong>' . $domain_siteLink . '</p>';
}
Display the custom field label and value in frontend orders and email notifications:
add_action( 'woocommerce_order_item_meta_end', 'custom_custom', 10, 3 );
function custom_custom( $item_id, $item, $order ){
// Get the custom field value
$domain_siteLink = get_post_meta( $order->get_id(), 'ssla_sitelink_url', true );
// Display the custom field:
echo '<p><strong>' . __('Domain SiteLink', 'woocommerce') . ': </strong>' . $domain_siteLink . '</p>';
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code works for WooCommerce 3.0+
回答2:
You may add additional order meta data like below.
add_action('woocommerce_add_order_item_meta','add_values_to_order_item_meta',1,2);
if(!function_exists('add_values_to_order_item_meta'))
{
function add_values_to_order_item_meta($item_id, $values)
{
global $woocommerce,$wpdb;
$user_custom_values = $values['user_custom_data_value'];
if(!empty($user_custom_values))
{
wc_add_order_item_meta($item_id,'user_custom_data',$user_custom_values);
}
}
}
来源:https://stackoverflow.com/questions/43532208/add-custom-checkout-field-to-order-in-woocommerce