【问题标题】:Adding newly added fields in billing address and shipping address in order emails woocommerce在订单电子邮件 woocommerce 中添加新添加的帐单地址和送货地址字段
【发布时间】:2021-06-06 13:57:18
【问题描述】:

在 woocommerce 订单电子邮件中,我想自定义字段以及帐单地址。这是我尝试过的。但它给出了错误。

add_filter('woocommerce_order_formatted_billing_address', array($this, 'woo_custom_order_formatted_billing_address'), 10, 2);
function woo_custom_order_formatted_billing_address( $address , $WC_Order ) {
    $address = array(
        'first_name'       => $WC_Order->billing_first_name . ' ' . $WC_Order->billing_last_name,
        'company'          => $WC_Order->billing_company,
        'address_1'        => $WC_Order->billing_address_1,
        'address_2'        => $WC_Order->billing_address_2,
        'city'             => $WC_Order->billing_city,
        'state'            => $WC_Order->billing_state,
        'area'             => $WC_Order->billing_area,
        'emirates'         => $WC_Order->billing_emirates,
        'nearest_landmark' => $WC_Order->billing_nearest_landmark,
        'country'          => $WC_Order->billing_country,
        'email'            => $WC_Order->billing_email,
        'phone'            => $WC_Order->billing_phone,
    );
    return $address;
}

请大家帮忙。

【问题讨论】:

  • 您遇到的错误是什么?你也在调用类函数,我没有看到任何类。第二个参数应该只是函数名。

标签: php wordpress woocommerce email-notifications


【解决方案1】:

您可以使用woocommerce_order_get_formatted_billing_address 过滤器而不是woocommerce_order_formatted_billing_address 来确保更改帐单地址的最终结果。

对于 billing_area、billing_emirates 和 billing_nearest_landmark 自定义字段,您需要将它们各自的值作为 order postmeta 获取。

字段的排序也将应用于 后端和前端中的帐单地址。

帐单电话和帐单电子邮件字段添加在 /woocommerce/emails/email-addresses.php 电子邮件中的帐单地址 模板。所以没有必要添加它们,否则它们会显示双倍。

以下代码应该可以工作。

add_filter( 'woocommerce_order_get_formatted_billing_address', 'add_custom_field_billing_address', 10, 3 );
function add_custom_field_billing_address( $address, $raw_address, $order ) {

    $countries = new WC_Countries();
    // gets country and state codes
    $billing_country = $order->get_billing_country();
    $billing_state = $order->get_billing_state();
    // gets the full names of the country and state
    $full_country = ( isset( $countries->countries[ $billing_country ] ) ) ? $countries->countries[ $billing_country ] : $billing_country;
    $full_state = ( $billing_country && $billing_state && isset( $countries->states[ $billing_country ][ $billing_state ] ) ) ? $countries->states[ $billing_country ][ $billing_state ] : $billing_state;

    $data = array(
        $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
        $order->get_billing_company(),
        $order->get_billing_address_1(),
        $order->get_billing_address_2(),
        $order->get_billing_city(),
        $full_state,
        $order->get_meta( '_billing_area', true ),             // or $order->get_meta( 'billing_area', true )
        $order->get_meta( '_billing_emirates', true ),         // or $order->get_meta( 'billing_emirates', true )
        $order->get_meta( '_billing_nearest_landmark', true ), // or $order->get_meta( 'billing_nearest_landmark', true )
        $full_country,
    );

    // removes empty fields from the array
    $data = array_filter( $data );
    // create the billing address using the "<br/>" element as a separator
    $address = implode( '<br/>', $data );

    return $address;

}

代码已经过测试并且可以运行。

【讨论】:

  • 添加这个时出现wordpress错误。
  • 立即尝试。我留下了参数号 2 而不是 3。如果您遇到更多错误,您应该显示日志文件行。
  • PHP 致命错误:未捕获的错误:在 functions.php 中不在对象上下文中时使用 $this
  • 尝试将add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'add_custom_field_billing_address'), 10, 3 ); 替换为add_filter( 'woocommerce_order_formatted_billing_address', 'add_custom_field_billing_address', 10, 3 );。我只是重用了你的代码。我不知道你是否在插件中使用它。
  • 我的意思是就像 woocommerce_order_get_formatted_billing_address 我们可以使用 woocommerce_order_get_formatted_shipping_address 来获取电子邮件中的自定义字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多