wordpress 添加自定义增值税字段到woocommerce电子邮件

6bc51xsx  于 2022-12-03  发布在  WordPress
关注(0)|答案(1)|浏览(158)

我真的卡住了,已经尝试了太阳下的每一个电子邮件挂钩。我用了别人开发的过时的PHP代码,并为新的woocommerce挂钩修改了它(因为代码是4岁)。一切工作完美,但我需要客户字段“billing_vat”出现在新的订单管理电子邮件。我将分享我的代码,任何帮助将不胜感激,请!
谢谢

//create vat number billing field
add_filter('woocommerce_billing_fields' , 'display_billing_vat_fields');
function display_billing_vat_fields($billing_fields){
$billing_fields['billing_vat'] = array(
    'type' => 'text',
    'label' =>  __('VAT number',  'woocommerce' ),
    'class' => array('form-row-wide'),
    'required' => false,
    'clear' => true,
    'priority' => 30, // To change the field location increase or decrease this value
);

return $billing_fields;
}

// Printing the Billing Address on My Account
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, $type ) {

if ( $type == 'billing' ) {
    $fields['vat'] = get_user_meta( $customer_id, 'billing_vat', true );
}

return $fields;
}

// Checkout -- Order Received (printed after having completed checkout)
add_filter( 'woocommerce_order_formatted_billing_address', 
'custom_add_vat_formatted_billing_address', 10, 2 );
function custom_add_vat_formatted_billing_address( $fields, $order ) {
$fields['vat'] = $order->get_meta('billing_vat');

return $fields;
}

// Creating merger VAT variables for printing formatting
add_filter( 'woocommerce_formatted_address_replacements', 
'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args  ) {
$replacements['{vat}'] = ! empty($args['vat']) ? $args['vat'] : '';
$replacements['{vat_upper}'] = ! empty($args['vat']) ? strtoupper($args['vat']) : '';

return $replacements;
}

//Defining the Spanish formatting to print the address, including VAT.
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );
function custom_localisation_address_format( $formats ) {
foreach($formats as $country => $string_address ) {
    $formats[$country] = str_replace('{company}\n', '{company}\n{vat_upper}\n', $string_address);
}
return $formats;

}

add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' );
function custom_customer_meta_fields( $fields ) {
$fields['billing']['fields']['billing_vat'] = array(
    'label'       => __( 'VAT number', 'woocommerce' )
);

return $fields;

}

add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );
function custom_admin_billing_fields( $fields ) {
$fields['vat'] = array(
    'label' => __( 'VAT number', 'woocommerce' ),
    'show'  => true
);

return $fields;

}

add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details' );
function custom_found_customer_details( $customer_data ) {
$customer_data['billing_vat'] = get_user_meta( $_POST['user_id'], 'billing_vat', true );

return $customer_data;

}

add_filter('woocommerce_email_order_meta_fields', 'supine_add_email_order_meta_fields', 10, 3 );
function supine_add_email_order_meta_fields( $fields, $sent_to_admin, $order_obj ) {

$billing = get_post_meta( $order_obj->get_order_number(), 'billing_vat', true );

return $fields;

}

bihw5rsg

bihw5rsg1#

在发布这篇文章后不久,我找到了一个修复方法。我必须定义新的键“billing_vat”,这样它才能与其他用户信息一起发布。上面的所有代码都是正确的,除了最后一个add_filter钩子。用下面的代码替换它,你就在你的结帐、管理后端和订单电子邮件中添加了一个可选的vat_number字段。下面是替换最后一个“add_filter”钩子的代码片段:

// VAT Number in emails
add_filter( 'woocommerce_email_order_meta_keys', 
'supine_vat_number_display_email' );

function supine_vat_number_display_email( $keys ) {
 $keys['VAT Number'] = '_billing_vat';
 return $keys;
}

代码放在你的活动子主题的functions.php中。测试过了,可以用了。

相关问题