WooCommerce自定义字段PHP 7运行PHP 7.1和7.2无法运行

时间:2018-11-01 11:22:28

标签: php wordpress object methods woocommerce

我要在我的Woocommerce帐单和收货地址中添加一个“标题”字段。一切都适用于PHP 7,但是当我升级到7.1或7.2时,WooCommerce中的订单页面崩溃,无法正确显示订单。因此,即使有3页订单,我也只能看到第一个订单,如果我单击该订单,它只会显示部分详细信息,根本没有地址。

// Add Title field in billing address display
add_filter( 'woocommerce_order_formatted_billing_address', 
'custom_add_title_formatted_billing_address', 1, 1 );
function custom_add_title_formatted_billing_address( $fields, $order ) {
$fields['title'] = $order->billing_title;
return $fields;
}
add_filter( 'woocommerce_my_account_my_address_formatted_address', 
'custom_my_account_my_address_formatted_address', 1, 1 );
function custom_my_account_my_address_formatted_address( $fields, $customer_id, 
$type ) {
if ( $type == 'billing' ) {
    $fields['title'] = get_user_meta( $customer_id, 'billing_title', true );
}
return $fields;
}
add_filter( 'woocommerce_address_to_edit', 'custom_address_to_edit', 1 );
function custom_address_to_edit( $address ) {
global $wp_query;
if ( isset( $wp_query->query_vars['edit-address'] ) && $wp_query- 
>query_vars['edit-address'] != 'billing' ) {
    return $address;
}
if ( ! isset( $address['billing_title'] ) ) {
    $address['billing_title'] = array(
        'label'       => __( 'Title', 'your-domain' ),
        'placeholder' => _x( 'Mr', 'placeholder', 'your-domain' ),
        'required'    => false, //change to false if you do not need this field 
to be required
        'class'       => array( 'form-row-first' ),
        'value'       => get_user_meta( get_current_user_id(), 'billing_title', 
true )
    );
}
return $address;
}
add_filter( 'woocommerce_formatted_address_replacements', 
'custom_formatted_address_replacements' );
function custom_formatted_address_replacements( $address, $args ) {
$address['{title}'] = '';
if ( ! empty( $args['title'] ) ) {
    $address['{title}'] = __( 'Title', 'your-domain' ) . ' ' . $args['title'];
}
return $address;
}
add_filter( 'woocommerce_localisation_address_formats', 
'custom_localisation_address_format', 1 );
function custom_localisation_address_format( $formats ) {
$formats['IT'] .= "\n\n{title}";
return $formats;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 
1 );
function custom_admin_billing_fields( $fields ) {
$fields['title'] = array(
    'label' => __( 'Title', 'your-domain' ),
    'show'  => true
);
return $fields;
}
add_filter( 'woocommerce_found_customer_details', 
'custom_found_customer_details' );
function custom_found_customer_details( $customer_data ) {
$customer_data['billing_title'] = get_user_meta( $_POST['user_id'], 
'billing_title', true );
return $customer_data;
}
add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' 
);
function custom_customer_meta_fields( $fields ) {
$fields['billing']['fields']['billing_title'] = array(
    'label'       => __( 'Title', 'woocommerce' )
);
return $fields;
}

任何想法都很感激。

1 个答案:

答案 0 :(得分:0)

此问题可能来自第一个钩子函数,其中$order->billing_title;不正确,因为自Woocommerce 3以来,大多数对象属性无法直接访问。 $order WC_Order对象实例就是这种情况。相反,您可以使用继承的WC_Data方法get_meta()

钩子优先级和参数也有一些错误。

尝试以下重新访问的代码:

// Add Title field in billing address display
add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_title_formatted_billing_address', 10, 2 );
function custom_add_title_formatted_billing_address( $billing_address, $order ) {
    $billing_address['title'] = $order->get_meta( '_billing_title');
    return $billing_address;
}

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( $address, $customer_id, $address_type ) {
    if ( $address_type == 'billing' ) {
        $address['title'] = get_user_meta( $customer_id, 'billing_title', true );
    }
    return $address;
}

add_filter( 'woocommerce_address_to_edit', 'custom_address_to_edit', 10, 2 );
function custom_address_to_edit( $address, $load_address ) {
    global $wp_query;
    if ( isset( $wp_query->query_vars['edit-address'] ) && $wp_query->query_vars['edit-address'] != 'billing' ) {
        return $address;
    }
    if ( ! isset( $address['billing_title'] ) ) {
        $address['billing_title'] = array(
            'label'       => __( 'Title', 'your-domain' ),
            'placeholder' => _x( 'Mr', 'placeholder', 'your-domain' ),
            'required'    => false, //change to false if you do not need this field to be required
            'class'       => array( 'form-row-first' ),
            'value'       => get_user_meta( get_current_user_id(), 'billing_title', true )
        );
    }
    return $address;
}

add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args  ) {
    $replacements['{title}'] = '';
    if ( ! empty( $args['title'] ) ) {
        $replacements['{title}'] = __( 'Title', 'your-domain' ) . ' ' . $args['title'];
    }
    return $replacements;
}

add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format', 10, 1 );
function custom_localisation_address_format( $formats ) {
    $formats['IT'] .= "\n\n{title}";
    return $formats;
}

add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields', 10, 1 );
function custom_admin_billing_fields( $billing_fields ) {
    $billing_fields['title'] = array(
        'label' => __( 'Title', 'your-domain' ),
        'show'  => true
    );
    return $billing_fields;
}

add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details', 10, 1 );
function custom_found_customer_details( $customer_data ) {
    $customer_data['billing_title'] = get_user_meta( $_POST['user_id'], 'billing_title', true );
    return $customer_data;
}

add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields', 10, 1 );
function custom_customer_meta_fields( $show_fields ) {
    $show_fields['billing']['fields']['billing_title']['label'] = __( 'Title', 'woocommerce' );
    return $show_fields;
}

我看不到其他任何可能在PHP 7.2中造成问题的地方……