Woocommerce中的自定义增值税字段问题 [英] Custom VAT field issue in Woocommerce

查看:79
本文介绍了Woocommerce中的自定义增值税字段问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码在woocommerce地址中添加增值税字段.它可以工作,但是会引发此错误:

I use the code below to add a VAT field in woocommerce address. It's works but, it throws this error:

注意:biling_vat estappeléede la mauvaiseManière.命令属性不应直接访问.回溯:require('wp-admin/edit-form-advanced.php'),do_meta_boxes,WC_Meta_Box_Order_Data :: output,WC_Order-> get_formatted_billing_address,apply_filters('woocommerce_order_formatted_billing_address'),WP_Hook-> apply_filters,custom_add_vat_formatted_billing_address,WC_Abstract_Legacy_Order-> __ get,wc_doing_it_wrong Veuillez lireDébogagedans WordPress(zh)倒加d信息.(CE消息aétéajoutéàla 3.0版.)中/var/www/vhosts/mydomain.be/httpdocs/wp-includes/functions.php,第5167行

Notice: billing_vat est appelée de la mauvaise manière. Order properties should not be accessed directly. Backtrace: require('wp-admin/edit-form-advanced.php'), do_meta_boxes, WC_Meta_Box_Order_Data::output, WC_Order->get_formatted_billing_address, apply_filters('woocommerce_order_formatted_billing_address'), WP_Hook->apply_filters, custom_add_vat_formatted_billing_address, WC_Abstract_Legacy_Order->__get, wc_doing_it_wrong Veuillez lire Débogage dans WordPress (en) pour plus d’informations. (Ce message a été ajouté à la version 3.0.) in /var/www/vhosts/mydomain.be/httpdocs/wp-includes/functions.php on line 5167

代码:

/*****************************  FRONTEND  ****************************************/

/**************************
Filter to add a VAT field to:

- My Account - Edit Form -- Billing fields
- Checkout - Edit Form - Billing Fields

This function is also reordering the form fields

Source:  https://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters
***************************/
function add_woocommerce_billing_fields($billing_fields){

    //reorder woo my billing address form fields
    $billing_fields2['billing_first_name'] = $billing_fields['billing_first_name'];
    $billing_fields2['billing_last_name'] = $billing_fields['billing_last_name'];
 
    $billing_fields2['billing_vat'] = array(
        'type' => 'text',
        'label' =>  __('VAT number',  'keyelp-shop-customization' ),
        'class' => array('form-row-wide'),
        'required' => false,
        'clear' => true
    );
    
    //unimos el resto de campos 
    $merged_billing_fields =  $billing_fields2 + $billing_fields;

    return $merged_billing_fields;
}
add_filter('woocommerce_billing_fields' , 'add_woocommerce_billing_fields');


    /*********
    Filters to add VAT when printing billing address on:
    - (1) My account  
    - (2) Checkout - Order Received (after checkout compeltion), 
    
    +++ Additional filters to format the printed output.
    
    ********/
    
    // (1) 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;
    }
    
    // (2) 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->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( $address, $args ) {
        $address['{vat}'] = '';
        $address['{vat_upper}']= '';
    
        if ( ! empty( $args['vat'] ) ) {
            $address['{vat}'] = $args['vat'];
            $address['{vat_upper}'] = strtoupper($args['vat']);
        }
        return $address;
    }
    
    //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 ) {
        $formats['ES'] = "{name}\n{company}\n{vat_upper}\n{address_1}\n{address_2}\n{postcode} {city}\n{state}\n{country}";
    
        return $formats;
    }
    
    /*****************************  ADMIN USER PROFILE PAGE  ****************************************/
    
    /*************** 
    Filter to add VAT Customer meta fields (user profile field on the billing address grouping)
    *****************/
    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', 'keyelp-shop-customization' )
        );
    
        return $fields;
    }
    
    /***************************  ADMIN ORDER PAGE  ****************************************/
    
    /*********  
    Filter to add VAT to the Edit Form on:  Order --  Admin page
    *********/
    add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );
    function custom_admin_billing_fields( $fields ) {
        $fields['vat'] = array(
            'label' => __( 'VAT number', 'keyelp-shop-customization' ),
            'show'  => true
        );
        
        return $fields;
    }
    
    
    /**************** 
    Filter to copy the VAT field from User meta fields to the Order Admin form (after clicking dedicated button on admin page)
    ******************/
    
    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;
    }

推荐答案

自2017年WooCommerce 3版本以来,此代码有些过时了.…之间还有一些其他更改.

This code is a bit outdated since 2017 WooCommerce 3 version… Also some other things have changed in between.

这是重新访问的代码:

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' => 35, // 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;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试和工作.

Code goes in function.php file of your active child theme (or active theme). Tested and work.

相关答案:

这篇关于Woocommerce中的自定义增值税字段问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆