首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加WooCommerce增值税号码签出页

添加WooCommerce增值税号码签出页
EN

Stack Overflow用户
提问于 2022-04-20 19:17:47
回答 1查看 291关注 0票数 1

我按照说明给网站添加了一个功能。不幸的是,我在“接收订单”和管理面板“客户订单”中出现了一个错误。

代码语言:javascript
复制
/*****************************  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.
***************************/

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
            );
                
            $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 completion),
+++ Additional filters to format the printed output.
********/

// (1) Printing the Billing Address on My Account

add_filter( 'woocommerce_my_account_my_address_formatted_address', 'njengah_my_account_my_address_formatted_address', 10, 3 );

function njengah_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', 'njengah_add_vat_formatted_billing_address', 10, 2 );

function njengah_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', 'njengah_formatted_address_replacements', 10, 2 );

function njengah_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', 'njengah_localisation_address_format' );

function njengah_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', 'njengah_customer_meta_fields' );

function njengah_customer_meta_fields( $fields ) {

            $fields['billing']['fields']['billing_vat'] = array(

                        'label'       => __( 'VAT number', 'njengah' )

            );


            return $fields;

}


/***************************  ADMIN ORDER PAGE  ****************************************/


/********* 

Filter to add VAT to the Edit Form on Order --  Admin page

*********/

add_filter( 'woocommerce_admin_billing_fields', 'njengah_admin_billing_fields' );

function njengah_admin_billing_fields( $fields ) {

            $fields['vat'] = array(
                        'label' => __( 'VAT number', 'njengah' ),
                        'show'  => true
            );

            return $fields;

}

/****************

Filter to copy the VAT field from User meta fields to the Order Admin form (after clicking the dedicated button on the admin page)

******************/


add_filter( 'woocommerce_found_customer_details', 'njengah_found_customer_details' );

function njengah_found_customer_details( $customer_data ) {

            $customer_data['billing_vat'] = get_user_meta( $_POST['user_id'], 'billing_vat', true );

            return $customer_data;

}

出现的错误是

不应直接访问

顺序属性。回溯:要求(‘WP-admin/编辑-form-Advanced.php’),do_meta_boxes,WC_Meta_Box_Order_Data::output,WC_Order->get_formatted_billing_address,WC_Meta_Box_Order_Data::output_Hook->apply_filters,add_vat_formatted_billing_address,WC_admin_Legacy_Order->_get,wc_doing_it_wrong

有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-20 19:41:17

错误说明顺序属性不应直接被访问。

您已经像这个$order->billing_vat;一样调用了order元数据。相反,您可以获得像$order->get_meta('billing_vat');这样的元数据,或者通过使用函数get_post_meta($order->get_id(), 'billing_vat', true);获得

像这样更新函数。

代码语言:javascript
复制
// (2) Checkout -- Order Received (printed after having completed checkout)

    add_filter( 'woocommerce_order_formatted_billing_address', 'njengah_add_vat_formatted_billing_address', 10, 2 );
    
    function njengah_add_vat_formatted_billing_address( $fields, $order ) {
    
                $fields['vat'] = get_post_meta($order->get_id(), 'billing_vat', true);
    
                return $fields;
    
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71945124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档