在所有电子邮件,订单信息和pdf发票中显示woocommerce自定义字段

时间:2019-03-25 16:16:25

标签: wordpress woocommerce

我使用一些代码在woocommerce计费和结帐中显示自定义字段。但我也想将自定义字段值传递给电子邮件通知,订单信息和pdf发票。

我正在使用Woocommerce 3.5.4和PHP7

/* Add field under my account billing  */
add_filter( 'woocommerce_billing_fields', 'my_woocommerce_billing_fields' );
function my_woocommerce_billing_fields( $fields ) {

    $user_id = get_current_user_id();
    $user    = get_userdata( $user_id );

    if ( !$user ) return;

    $fields['billing_vat'] = array(
        'type'      => 'text',
        'label'     => __('VAT', 'woocommerce'),
        'placeholder'   => _x('VAT Number', 'placeholder', 'woocommerce'),
        'required'  => false,
        'class'     => array('form-row'),
        'clear'     => true,
        'default'   => get_user_meta( $user_id, 'billing_vat', true ) // assing default value if any
    );

    return $fields;
}

/* Format custom field to show on my account billing  */
add_filter( 'woocommerce_my_account_my_address_formatted_address', 'custom_my_account_my_address_formatted_address', 10, 3 );
function custom_my_account_my_address_formatted_address( $fields, $customer_id, $name ) {

    $fields['vat']  = get_user_meta( $customer_id, $name . '_vat', true );

    return $fields;
}

/* Replace the key for custom field to show on my account billing  */
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $address, $args ) {
    $address['{vat}'] = '';

    if ( ! empty( $args['vat'] ) ) {
        $address['{vat}'] = __( 'VAT Number', 'woocommerce' ) . ': ' . $args['vat'];
    }

    return $address;
}   
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );
function custom_localisation_address_format( $formats ) {

    foreach($formats as $key => $value) :
        $formats[$key] .= "\n\n{vat}";
    endforeach;

    return $formats;
}

//Order the fields
add_filter("woocommerce_checkout_fields","order_fields");
function order_fields($fields){
    $order = array(
        "billing_first_name",
        "billing_last_name",
        "billing_company",
        "billing_vat",
        "billing_country",
        "billing_city",
        "billing_postcode",
        "billing_state",
        "billing_address_1",
        "billing_address_2",
        "billing_email",
        "billing_phone",
    );
foreach($order as $field){$ordered_fields[$field] = $fields["billing"][$field];}
$fields["billing"] = $ordered_fields;
return $fields;
}

我还使用了Easy Registration Forms插件,并将所有字段值映射到woocommerce计费字段,因此,当用户进行注册时,所有字段都在两个地方填充。

0 个答案:

没有答案