我最终使用以下代码实现了它:
我使用一个php类来创建一个有组织的工作流,所以不必介意public 关于函数。。。
首先,我像这样注册我的过滤器:
public function registerFilters()
{
add_filter( \'woocommerce_default_address_fields\', array($this, \'woo_new_default_address_fields\'), 10, 2);
add_filter(\'woocommerce_order_formatted_billing_address\', array($this, \'woo_custom_order_formatted_billing_address\'), 10, 2);
add_filter(\'woocommerce_formatted_address_replacements\', array($this, \'myFunction\'), 10, 2);
add_filter(\'woocommerce_localisation_address_formats\', array($this, \'mySecondFunction\'), 10, 2);
return $this;
}
使用此功能,我可以添加/注册自定义字段,使其在默认情况下成为自定义字段,这样它将在插件部分以紫色字母显示(如页面顶部的屏幕截图所示):
public function woo_new_default_address_fields($fields)
{
$fields[\'colonia\'] = array(
\'label\' => __( \'Colonia\', \'woocommerce\' ),
\'class\' => array( \'form-row-wide\' ),
);
$fields[\'num_int\'] = array(
\'label\' => __( \'Num. Interior\', \'woocommerce\' ),
\'class\' => array( \'form-row-wide\' ),
);
$fields[\'num_ext\'] = array(
\'label\' => __( \'Num. Exterior\', \'woocommerce\' ),
\'class\' => array( \'form-row-wide\' ),
);
$fields[\'municipio\'] = array(
\'label\' => __( \'Municipio\', \'woocommerce\' ),
\'class\' => array( \'form-row-wide\' ),
);
$fields[\'reference\'] = array(
\'label\' => __( \'Referencias\', \'woocommerce\' ),
\'class\' => array( \'form-row-wide\' ),
);
$order = array(
"first_name",
"last_name",
"colonia",
"num_int",
"num_ext",
"municipio",
"reference",
"company",
"country",
"address_1",
"address_2",
"city",
"state",
"postcode"
);
foreach($order as $field) {
$ordered_fields[$field] = $fields[$field];
}
$fields = $ordered_fields;
return $fields;
}
然后我将它们添加到电子邮件的格式化帐单地址:
public function woo_custom_order_formatted_billing_address( $address , $WC_Order )
{
$address = array(
\'first_name\' => \'Nombre: \' . $WC_Order->billing_first_name . \' \' . $address[\'last_name\'],
\'address_1\' => \'Dirección: \' . $WC_Order->billing_address_1,
\'colonia\' => \'Colonia: \' . $WC_Order->billing_colonia,
\'num_ext\' => \'Num. Exterior: \' . $WC_Order->billing_num_ext,
\'num_int\' => \'Num. Interior: \' . $WC_Order->billing_num_int,
\'reference\' => \'Referencias: \' . $WC_Order->billing_reference,
\'city\' => \'Ciudad: \' . $WC_Order->billing_city,
\'state\' => \'Estado: \' . $WC_Order->billing_state,
\'municipio\' => \'Municipio: \' . $WC_Order->billing_municipio,
\'postcode\' => \'Código Postal: \' . $WC_Order->billing_postcode,
);
return $address;
}
在此,我将我的自定义字段替换/添加到地址中,以便在格式化的帐单地址上使用这些字段:
public function myFunction( $address, $args )
{
$address[\'{colonia}\'] = $args[\'colonia\'];
$address[\'{num_int}\'] = $args[\'num_int\'];
$address[\'{num_ext}\'] = $args[\'num_ext\'];
$address[\'{municipio}\'] = $args[\'municipio\'];
$address[\'{reference}\'] = $args[\'reference\'];
return $address;
}
然后,我将替换/添加它们,并说明其显示方式:
public function mySecondFunction($formats)
{
foreach($formats as $key => $value) :
$formats[$key] .= "\\n\\n{colonia}";
$formats[$key] .= "\\n\\n{num_int}";
$formats[$key] .= "\\n\\n{num_ext}";
$formats[$key] .= "\\n\\n{municipio}";
$formats[$key] .= "\\n\\n{reference}";
endforeach;
return $formats;
}
我希望你们都能理解我,我不是专家:)