Скопируйте пользовательские поля woocommerce в поля по умолчанию

Я написал плагин для 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_streetname + 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] );

person Ewout    schedule 13.01.2013    source источник


Ответы (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;
}

Я не думаю, что этот код очень элегантный (в частности, часть проверки суффиксов), поэтому, если у кого-то есть советы по его улучшению - очень приветствую!

person Ewout    schedule 14.01.2013