[英]Add Custom checkout field to Order in WooCommerce
The WooCommerce 3.0 update has not been kind to me. WooCommerce 3.0更新对我而言并不友好。 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. 我知道
$_POST
变量在那里,我将其注销后看到错误。
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:
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: 首先,您需要在发布结帐表单时验证该字段,并且该字段是必填字段,而不是可选字段,可使用
woocommerce_checkout_process
操作挂钩:
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: 由于这是一个结帐自定义字段,因此可以使用
woocommerce_checkout_update_order_meta
操作钩子将新字段保存为订购自定义字段:
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: 使用
woocommerce_admin_order_data_after_billing_address
操作挂钩在管理订单版本页面上显示自定义字段值:
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. 这段代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中。
This code works for WooCommerce 3.0+ 此代码适用于WooCommerce 3.0+
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.