【问题标题】:How to get all the fields with billing_* in woocommerce?如何在 woocommerce 中使用 billing_* 获取所有字段?
【发布时间】:2021-04-02 16:23:25
【问题描述】:

我正在使用 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。

有没有办法在woocommerce中调用名称为billing_*的字段的所有值。

这是我尝试过的:

$order->get_billing_address();

但是没用!

提前谢谢你!

截图: Screenshot

这是我得到的输出: Screeshot 2

同时,其他字段显示在订单页面中,但不显示为账单明细。喜欢这里

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您可以通过 woocommerce_order_get_formatted_billing_address 过滤钩子修改WC_Order 类的get_formatted_billing_address 方法的输出(如您在Adding newly added fields in billing address and shipping address in order emails woocommerce 中所见)。

    在您的情况下,添加自定义字段:

    • _billing_street
    • _billing_town
    • _billing_avenue

    您可以使用以下功能:

    随意更改计费字段的排序顺序(通过更改 $data 数组的值)

    // 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 中。

    所以现在您可以使用$order-&gt;get_formatted_billing_address(); 获取使用自定义字段格式化的帐单地址的方法 包括在内。

    【讨论】:

    • 那 _ 令人困惑,我需要添加 _billing_street 吗?以及将这段代码放在哪里,在functions.php中?
    • 我把这段代码放在我的主题的functions.php中,但它不起作用。而且我只在给定的代码中使用了 _ 。而在字段名称中,它没有下划线。没有错误显示。
    • 出现了疑问。自定义计费字段是如何添加的?您是否遵循本指南? docs.woocommerce.com/document/…
    • 我正在使用 WooCommerce Checkout 字段编辑器插件。
    • 这是关于账单明细的。现在如何获取其他一些自定义字段,例如我添加了 new_field。我怎样才能得到那个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2022-08-19
    • 2021-10-09
    • 1970-01-01
    相关资源
    最近更新 更多