将自定义结帐字段添加到WooCommerce中的订单 [英] Add Custom checkout field to Order in WooCommerce

查看:258
本文介绍了将自定义结帐字段添加到WooCommerce中的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WooCommerce 3.0更新对我而言并不友好.我已经添加了一个自定义必填字段来签出一个域名,并且在弄清楚如何立即保存该域名时遇到了麻烦.这段代码仍然正确地添加了字段:

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'] ) );
    }
}

但是,该meta似乎没有添加或保存在任何地方.

However the meta does not appear to be added or saved anywhere.

我知道 $_POST 变量在那里,我在将其注销时看到错误.

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 ) );

//输出为:

[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
        )

)

但是,

$sitelink_domain        = $subscription->get_meta( 'ssla_sitelink_url' );
error_log( 'Domain: ' . $sitelink_domain );

输出就是:

[21-Apr-2017 01:27:39 UTC] Domain:

推荐答案

首先,您需要在发布结帐表单时验证该字段,并且使用 woocommerce_checkout_process 操作该字段是必填字段,而不是可选字段钩子:

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' );
}

这是一个结帐自定义字段,因此您可以使用 woocommerce_checkout_update_order_meta 操作钩子将新字段保存为订购自定义字段:

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'] ) );
    }
}

使用 woocommerce_admin_order_data_after_billing_address 操作钩在管理订单版本页面上显示自定义字段值:

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>';
}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码适用于WooCommerce 3.0 +

This code works for WooCommerce 3.0+

这篇关于将自定义结帐字段添加到WooCommerce中的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆