gpt4 book ai didi

php - 将自定义账单数据添加到 Woocommerce 管理订单中的账单格式地址

转载 作者:行者123 更新时间:2023-12-02 20:03:14 25 4
gpt4 key购买 nike

通过 Woocommerce,我希望使用 woocommerce_localization_address_formats Hook 在管理订单编辑页面中查看用户元数据。

基于"Woocommerce Edit order on admin dashboard "答案代码,我做了如下轻微更改:

add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
if( is_admin() ){
$address_formats = array();

$countries = array('default', 'AU', 'AT', 'BE', 'CA', 'CH', 'CL', 'CN', 'CZ',
'DE', 'EE', 'FI', 'DK', 'FR', 'HK', 'HU', 'IN', 'IS', 'IT', 'JP', 'TW', 'LI',
'NL', 'NZ', 'NO', 'PL', 'PT', 'SK', 'SI', 'ES', 'SE', 'TR', 'US', 'VN' );

foreach( $countries as $country_code ) {
$address_formats[$country_code] = "{company}\n{name}\n{address_1} {address_2} {postcode}\n{city} {state}\n{country}\n{billing_nombrecontacto}";
}
}
return $address_formats;
}

其中 {billing_nombrecontacto} 应该是要插入管理订单页面的自定义数据......但它不会产生任何结果。

要添加billing_nombrecontacto,我使用了以下代码:

add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields,$fields2)
{
$fields['billing_nombrecontacto'] = array(
'label' => __('Nombre de contacto', 'woocommerce'), // Add custom field label
'placeholder' => _x('Nombre de contacto', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name

);

$fields['billing_cuit'] = array(
'label' => __('CUIT', 'woocommerce'), // Add custom field label
'placeholder' => _x('CUIT', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name

);

$fields['billing_emaildelvendedor'] = array(
'label' => __('Email del Contacto', 'woocommerce'), // Add custom field label
'placeholder' => _x('email del Vendedor', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name

);

$fields['billing_nombrecontacto']['priority'] = 102;

return $fields;
}

数据收集正确,我可以在我的帐户中的此自定义字段中看到它。

如何让它发挥作用?我被困住了。

最佳答案

更新: 添加了一个额外的缺失函数,显示 billing_nombrecontacto 字段值...

我重新访问了您现有的代码并添加了更多代码以使其正常工作:

// Add / Display additional billing fields in checkout and My account > Edit adresses > Billing form
add_filter( 'woocommerce_billing_fields', 'additional_billing_fields', 10, 1 );
function additional_billing_fields( $fields )
{
$fields['billing_nombrecontacto'] = array(
'type' => 'text', // add field type
'label' => __('Nombre de contacto', 'woocommerce'), // Add custom field label
'placeholder' => _x('Nombre de contacto', 'placeholder', 'woocommerce'), // Add custom field placeholder
'class' => array('my-css'), // add class name
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'priority' => 102, // define the priority
);

$fields['billing_cuit'] = array(
'type' => 'text', // add field type
'label' => __('CUIT', 'woocommerce'), // Add custom field label
'placeholder' => _x('CUIT', 'placeholder', 'woocommerce'), // Add custom field placeholder
'class' => array('my-css'), // add class name
'required' => false, // if field is required or not
'clear' => false, // add clear or not

);

$fields['billing_emaildelvendedor'] = array(
'type' => 'text', // add field type
'label' => __('Email del Contacto', 'woocommerce'), // Add custom field label
'placeholder' => _x('email del Vendedor', 'placeholder', 'woocommerce'), // Add custom field placeholder
'class' => array('my-css'), // add class name
'required' => false, // if field is required or not
'clear' => false, // add clear or not

);

return $fields;
}

// Adding custom placeholder to woocommerce formatted billing address only on Backend
add_filter( 'woocommerce_localisation_address_formats', 'admin_localisation_address_formats', 50, 1 );
function admin_localisation_address_formats( $address_formats ){
// Only in backend (Admin)
if( is_admin() ) {
foreach( $address_formats as $country_code => $address_format ) {
$address_formats[$country_code] .= "\n{nombrecontacto}";
}
}
return $address_formats;
}

// Custom placeholder replacement to woocommerce formatted billing address
add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args ) {
$replacements['{nombrecontacto}'] = ! empty($args['nombrecontacto']) ? $args['nombrecontacto'] : '';

return $replacements;
}

// Get the field values to be displayed in admin Order edit pages
add_filter('woocommerce_order_formatted_billing_address', 'add_woocommerce_order_fields', 10, 2);
function add_woocommerce_order_fields($address, $order ) {
$address['nombrecontacto'] = $order->get_meta('_billing_nombrecontacto');

return $address;
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

enter image description here


奖励 - 使字段可编辑:

// Make the custom billing field Editable in Admin order pages
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
$billing_fields['nombrecontacto'] = array( 'label' => __('Nombre contacto', 'woocommerce') );

return $billing_fields;
}

enter image description here

关于php - 将自定义账单数据添加到 Woocommerce 管理订单中的账单格式地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55367931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com