Woocommerce 订单格式的帐单地址重新排序和自定义帐单字段

Jos*_*eal 3 wordpress e-commerce woocommerce woothemes

多亏了过滤器“WooCommerce 管理帐单字段”,我已通过电子邮件订购了通知页脚中的帐单字段,但是当我尝试插入我的自定义帐单字段时没有出现。

add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_reorder_billing_fields', 10, 2 );

function woo_reorder_billing_fields( $address, $wc_order ) {

    $address = array(
        'first_name'    => $wc_order->billing_first_name,
        'last_name'     => $wc_order->billing_last_name,
        'company'       => $wc_order->billing_company,
        'my_field'      => $wc_order->billing_my_field,
        'country'       => $wc_order->billing_country
        );

    return $address;
}
Run Code Online (Sandbox Code Playgroud)

为了管理员编辑,我可以显示我的自定义计费字段,这要归功于过滤器“woocommerce_admin_billing_fields”,首先添加字段然后重新排序数组。

我注意到我之前添加过,并且在我的结账中使用过滤器“woocommerce_checkout_fields”重新排序了这个字段。

如果“$ wc_order”对象在结帐中存储该字段,为什么不显示我的自定义字段?

有任何想法吗?

提前致谢!

Jos*_*eal 6

是的,这里的解释:

我们将注册新的 cusotm 字段,在此示例中为字段“VAT”,用于存储欧盟公司的财务凭证。有很多关于如何注册自定义字段并在管理/用户面板中显示它们的文档。

add_filter( 'woocommerce_default_address_fields', 'woo_new_default_address_fields' );

function woo_new_default_address_fields( $fields ) {

    $fields['vat'] = array(

        'label' => __( 'VAT number', 'woocommerce' ),
        'class' => array( 'form-row-wide', 'update_totals_on_change' ),

    );
    
    return $fields;

}
Run Code Online (Sandbox Code Playgroud)

然后仅将新字段“增值税”添加到邮件计费字段的注册中,我们不希望它出现在地址字段部分。

add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address', 10, 2 );

function woo_custom_order_formatted_billing_address( $address, $WC_Order ) {
    
    $address = array(
        'first_name'    => $WC_Order->billing_first_name,
        'last_name'     => $WC_Order->billing_last_name,
        'vat'           => $WC_Order->billing_vat,
        'company'       => $WC_Order->billing_company,
        'address_1'     => $WC_Order->billing_address_1,
        'address_2'     => $WC_Order->billing_address_2,
        'city'          => $WC_Order->billing_city,
        'state'         => $WC_Order->billing_state,
        'postcode'      => $WC_Order->billing_postcode,
        'country'       => $WC_Order->billing_country
        );
    
    return $address;

}
Run Code Online (Sandbox Code Playgroud)

以下代码自定义地址的外观,这是我们必须将调用添加到新的 VAT 字段的地方。这允许我们独立地为每个国家定制地址视图。

add_filter( 'woocommerce_formatted_address_replacements', function( $replacements, $args ){
    
    $replacements['{vat}'] = $args['vat'];
    return $replacements;

}, 10, 2 );

add_filter( 'woocommerce_localisation_address_formats' , 'woo_includes_address_formats', 10, 1);

function woo_includes_address_formats($address_formats) {

    $address_formats['ES'] = "{name}\n{company}\n{vat}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
    $address_formats['default'] = "{name}\n{company}\n{vat}\n{nif}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}";
    
    return $address_formats;

}
Run Code Online (Sandbox Code Playgroud)

有任何问题问!