1

次のカスタム チェックアウト フィールドを作成する 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 にコピーしたいと思います。このような:

請求先住所 1 = 請求先住所 + 請求先番号 + 請求先番号サフィックス

私は次の(初歩的な)コードでこれをやろうとしました:

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] );
4

1 に答える 1

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

ただし、このコードはあまりエレガントではないと思います (具体的には接尾辞のチェック部分)。そのため、誰かがそれを改善するためのヒントを持っていれば、大歓迎です!

于 2013-01-14T12:56:23.053 に答える