【问题标题】:Copy custom woocommerce fields into default fields将自定义 woocommerce 字段复制到默认字段中
【发布时间】:2013-01-13 22:04:13
【问题描述】:

我编写了一个 woocommerce 插件,它创建了以下自定义结帐字段:

billing_street_name
billing_house_number
billing_house_number_suffix

shipping_street_name
shipping_house_number
shipping_house_number_suffix

我也将此添加到管理页面,但由于我无法连接到 get_formatted_billing_address 和 get_formatted_shipping_address(它们都用于显示 writepanel-order_data.php 和 shop_order.php 中的地址)我想将它们复制到默认值billing_address_1 和 shipping_address_1 像这样:

billing_address_1 = billing_street_name + billing_house_number + billing_house_number_suffix

我尝试使用以下(基本)代码来做到这一点:

add_action( 'woocommerce_process_checkout_field_billing_address_1', array( &$this, 'combine_street_number_suffix' ) );

public function combine_street_number_suffix () {
$key = $_POST['billing_street_name'] . ' ' . $_POST['billing_house_number'];

return $key;
}

但这不起作用 - 我认为 $_POST 变量根本不会被传递?

这是在 class-wc-checkout.php 中创建钩子的方式:

// Hook to allow modification of value
$this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[$key] );

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    使用“woocommerce_checkout_update_order_meta”钩子修复了这个问题:

    add_action('woocommerce_checkout_update_order_meta', array( &$this, 'combine_street_number_suffix' ) );
    
    public function combine_street_number_suffix ( $order_id ) {
        // check for suffix
        if ( $_POST['billing_house_number_suffix'] ){
            $billing_house_number = $_POST['billing_house_number'] . '-' . $_POST['billing_house_number_suffix'];
        } else {
            $billing_house_number = $_POST['billing_house_number'];
        }
    
        // concatenate street & house number & copy to 'billing_address_1'
        $billing_address_1 = $_POST['billing_street_name'] . ' ' . $billing_house_number;
        update_post_meta( $order_id,  '_billing_address_1', $billing_address_1 );
    
        // check if 'ship to billing address' is checked
        if ( $_POST['shiptobilling'] ) {
            // use billing address
            update_post_meta( $order_id,  '_shipping_address_1', $billing_address_1 );
        } else {
            if ( $_POST['shipping_house_number_suffix'] ){
                $shipping_house_number = $_POST['shipping_house_number'] . '-' . $_POST['shipping_house_number_suffix'];
            } else {
                $shipping_house_number = $_POST['shipping_house_number'];
            }
    
            // concatenate street & house number & copy to 'shipping_address_1'
            $shipping_address_1 = $_POST['shipping_street_name'] . ' ' . $shipping_house_number;
            update_post_meta( $order_id,  '_shipping_address_1', $shipping_address_1 );         
        }
    
    
        return;
    }
    

    我不认为这段代码很优雅(特别是后缀检查部分),所以如果有人有改进它的提示 - 非常欢迎!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-30
      • 2021-07-10
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2017-11-05
      • 2022-01-25
      相关资源
      最近更新 更多