【问题标题】:WooCommerce Custom Field PHP 7 working PHP 7.1 and 7.2 Not workingWooCommerce 自定义字段 PHP 7 工作 PHP 7.1 和 7.2 不工作
【发布时间】:2019-04-05 14:49:55
【问题描述】:

我正在向我的 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;
}

非常感谢任何想法。

【问题讨论】:

  • 我建议先修正你的报价,比如这个add_filter( 'woocommerce_order_formatted_billing_address', custom_add_title_formatted_billing_address', 特别是这个custom_add_title_formatted_billing_address'你看到上面的代码块中的所有东西都颜色很奇怪.....

标签: php wordpress object methods woocommerce


【解决方案1】:

此问题可能来自第一个挂钩函数,其中$order->billing_title; 不正确,因为从 Woocommerce 3 开始,大多数对象属性都无法直接访问。 $order WC_Order Object 实例就是这种情况。相反,您可以使用继承的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 中造成问题的东西……

【讨论】:

  • 不幸的是,这并没有解决它。感谢您的帮助
  • @Ruth 尝试一一禁用启用挂钩功能,评论/取消评论它们,找出有罪。
  • 只是第一个过滤器本身导致它 // 在帐单地址显示中添加标题字段 add_filter( 'woocommerce_order_formatted_billing_address', 'custom_add_title_formatted_billing_address', 1, 1 );函数 custom_add_title_formatted_billing_address( $fields, $order ) { $fields['title'] = $order->get_meta('_billing_title');返回$字段; }
  • 还有这个 add_filter( 'woocommerce_formatted_address_replacements', 'custom_formatted_address_replacements' );
  • @Ruth 我已经更新了我的答案代码……我希望它能解决这个问题。
猜你喜欢
  • 1970-01-01
  • 2016-05-25
  • 2011-11-19
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多