gpt4 book ai didi

php - 如何在woocommerce中使用billing_*获取所有字段?

转载 作者:行者123 更新时间:2023-12-04 07:51:54 24 4
gpt4 key购买 nike

我正在使用 Woocommerce 结帐字段编辑器 并创建了多个名为 billing_* 的计费字段。例如 billing_street、 billing_town、 billing_avenue 等,除了 billing_address_1 和 billing_address_2 是预定义的。
但是打电话时$order->get_formatted_billing_address();我只得到 billing_address_1 和 billing_address_2。
有什么方法可以调用名称为 billing_* 的字段的所有值吗?在woocommerce。
这是我尝试过的:$order->get_billing_address();但它没有用!
先感谢您!
截屏:
Screenshot
这是我得到的输出:
Screeshot 2
同时,其他字段显示在订单页面中,但不显示为账单明细。喜欢这里 not as billing details

最佳答案

您可以修改 get_formatted_billing_address 的输出WC_Order的方法通过 上课woocommerce_order_get_formatted_billing_address 过滤器 Hook (如您在 Adding newly added fields in billing address and shipping address in order emails woocommerce 中所见)。
在您的情况下,要添加自定义字段:

  • _billing_street
  • _billing_town
  • _billing_avenue

  • 您可以使用以下功能:

    Feel free to change the sort order of the billing fields (by changingthe values of the $data array).

    // adds the custom fields to the formatted billing address
    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_postcode(),
    $order->get_billing_city(),
    wc_strtoupper( $full_state ),
    $order->get_meta( '_billing_street', true ),
    $order->get_meta( '_billing_town', true ),
    $order->get_meta( '_billing_avenue', true ),
    );

    // 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;

    }
    该代码已经过测试并且可以正常工作。将它添加到您的事件主题的functions.php。

    So now you can use the $order->get_formatted_billing_address();method to get the billing address formatted with the custom fieldsincluded.

    关于php - 如何在woocommerce中使用billing_*获取所有字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66922269/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com