WooCommerce默认运输邮政编码

时间:2014-10-27 18:42:23

标签: php wordpress woocommerce

我正在尝试使用客户数据预先填充WooCommerce送货地址。这适用于除shipping_postcode之外的所有字段。我有什么想法吗?

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
    $customer_id = get_current_user_id();
    $fields['shipping']['shipping_first_name']['default'] = get_user_meta( $customer_id, $name . 'shipping_first_name', true );
    $fields['shipping']['shipping_last_name']['default'] = get_user_meta( $customer_id, $name . 'shipping_last_name', true );
    $fields['shipping']['shipping_company']['default'] = get_user_meta( $customer_id, $name . 'shipping_company', true );
    $fields['shipping']['shipping_address_1']['default'] = get_user_meta( $customer_id, $name . 'shipping_address_1', true );
    $fields['shipping']['shipping_address_2']['default'] = get_user_meta( $customer_id, $name . 'shipping_address_2', true );
    $fields['shipping']['shipping_city']['default'] = get_user_meta( $customer_id, $name . 'shipping_city', true );
    $fields['shipping']['shipping_postcode']['default'] = get_user_meta( $customer_id, $name . 'shipping_postcode', true );
    $fields['shipping']['shipping_country']['default'] = get_user_meta( $customer_id, $name . 'shipping_country', true );
    $fields['shipping']['shipping_state']['default'] = get_user_meta( $customer_id, $name . 'shipping_state', true );
    return $fields;
}

我甚至尝试过一个没有运气的简单作业:

    $fields['shipping']['shipping_postcode']['default'] = "90210";

1 个答案:

答案 0 :(得分:0)

我们找到了解决方法。

由于某种原因,这一行不起作用:

$fields['shipping']['shipping_postcode']['default'] = get_user_meta( $customer_id, $name . 'shipping_postcode', true );

但是这段代码完成了我们想要的东西:

$woocommerce->customer->set_shipping_postcode(get_user_meta( $customer_id, $name . 'shipping_postcode', true ));