| Server IP : 104.171.130.249 / Your IP : 216.73.216.146 Web Server : nginx/1.25.5 System : Linux 0dac65491b7e 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : root ( 0) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/html/wp-content/themes/matreshka/inc/ |
Upload File : |
<?php
/**
* WooCommerce account, address, and checkout field customizations.
*/
/**
* ─── Edit-address page: save custom Matreshka address fields ───
*/
add_action( 'woocommerce_customer_save_address', 'matreshka_save_custom_address_fields', 10, 2 );
function matreshka_save_custom_address_fields( $user_id, $load_address ) {
if ( 'billing' !== $load_address ) {
return;
}
// Sync billing → shipping
$address = sanitize_text_field( $_POST['billing_address_1'] ?? '' );
update_user_meta( $user_id, 'shipping_address_1', $address );
// Save custom fields
$fields = [
'_matreshka_entrance' => 'matreshka_entrance',
'_matreshka_intercom' => 'matreshka_intercom',
'_matreshka_floor' => 'matreshka_floor',
'_matreshka_apartment' => 'matreshka_apartment',
'_matreshka_courier_comment' => 'matreshka_courier_comment',
];
foreach ( $fields as $meta_key => $post_key ) {
update_user_meta( $user_id, $meta_key, sanitize_text_field( $_POST[ $post_key ] ?? '' ) );
}
// Redirect to my-account with a nice success notice
wc_add_notice( 'Адрес доставки успешно сохранён ✓', 'success' );
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
exit;
}
/**
* Enqueue edit-address CSS on my-account pages.
*/
add_action( 'wp_enqueue_scripts', 'matreshka_enqueue_edit_address_css' );
function matreshka_enqueue_edit_address_css() {
if ( function_exists( 'is_account_page' ) && is_account_page() ) {
wp_enqueue_style(
'matreshka-edit-address',
get_template_directory_uri() . '/assets/css/edit-address.css',
array( 'matreshka-theme' ),
'1.0.0'
);
}
}
/**
* Simplify billing fields – only address_1 is required on edit-address page.
* Other fields are passed as hidden inputs to satisfy WC handler but should not block save.
*/
add_filter( 'woocommerce_billing_fields', 'matreshka_simplify_billing_fields', 9999 );
function matreshka_simplify_billing_fields( $fields ) {
// A pickup order does not require a delivery address.
if ( is_checkout() && ! is_order_received_page() && WC()->session && 'pickup' === WC()->session->get( 'matreshka_shipping_type', 'delivery' ) && isset( $fields['billing_address_1'] ) ) {
$fields['billing_address_1']['required'] = false;
$fields['billing_address_1']['validate'] = array();
}
// On my-account edit-address: only billing_address_1 required
if ( is_account_page() ) {
$keep_required = [ 'billing_address_1' ];
foreach ( $fields as $key => &$field ) {
if ( ! in_array( $key, $keep_required, true ) ) {
$field['required'] = false;
}
}
return $fields;
}
// On checkout: relax last_name, company, city, state, postcode requirements
if ( is_checkout() ) {
if ( isset( $fields['billing_phone'] ) ) {
$fields['billing_phone']['required'] = true;
$fields['billing_phone']['placeholder'] = '+7 XXX XXX XX XX';
}
$not_required = [
'billing_last_name', 'billing_company', 'billing_address_2',
'billing_city', 'billing_state', 'billing_postcode',
];
foreach ( $not_required as $key ) {
if ( isset( $fields[ $key ] ) ) {
$fields[ $key ]['required'] = false;
}
}
}
return $fields;
}
/**
* Remove last_name and display_name from required fields — we use first_name only.
*/
add_filter( 'woocommerce_save_account_details_required_fields', 'matreshka_edit_account_required_fields' );
function matreshka_edit_account_required_fields( $fields ) {
unset( $fields['account_last_name'] );
unset( $fields['account_display_name'] );
return $fields;
}
/**
* Ensure display_name is populated from first_name before WC validation runs.
*/
add_action( 'template_redirect', 'matreshka_fill_display_name_from_first', 5 );
function matreshka_fill_display_name_from_first() {
if ( ! empty( $_POST['action'] ) && 'save_account_details' === $_POST['action'] ) {
if ( empty( $_POST['account_display_name'] ) && ! empty( $_POST['account_first_name'] ) ) {
$_POST['account_display_name'] = $_POST['account_first_name'];
}
}
}
/**
* ─── Checkout: pre-fill custom fields from user profile meta ───
*/
add_filter( 'woocommerce_checkout_get_value', 'matreshka_prefill_checkout_fields', 10, 2 );
function matreshka_prefill_checkout_fields( $value, $input ) {
if ( ! is_user_logged_in() ) {
return $value;
}
// Only act on our custom fields + order_comments + phone fallback
$meta_map = [
'billing_entrance' => '_matreshka_entrance',
'billing_intercom' => '_matreshka_intercom',
'billing_floor' => '_matreshka_floor',
'billing_apartment' => '_matreshka_apartment',
'order_comments' => '_matreshka_courier_comment',
'billing_phone' => 'billing_phone',
];
if ( isset( $meta_map[ $input ] ) ) {
$meta_value = get_user_meta( get_current_user_id(), $meta_map[ $input ], true );
if ( $meta_value ) {
return $meta_value;
}
}
return $value;
}
/**
* ─── Edit-account: save birthday + sync display_name, redirect nicely ───
*/
add_action( 'woocommerce_save_account_details', 'matreshka_save_account_extra_fields' );
function matreshka_save_account_extra_fields( $user_id ) {
// Save birthday
if ( isset( $_POST['matreshka_birthday'] ) ) {
update_user_meta( $user_id, '_matreshka_birthday', sanitize_text_field( $_POST['matreshka_birthday'] ) );
}
// Sync display_name + billing_first_name with first_name
$first_name = sanitize_text_field( $_POST['account_first_name'] ?? '' );
if ( $first_name ) {
wp_update_user( [
'ID' => $user_id,
'display_name' => $first_name,
] );
update_user_meta( $user_id, 'billing_first_name', $first_name );
}
}
/**
* Redirect to my-account dashboard after saving account details.
*/
add_action( 'woocommerce_save_account_details', 'matreshka_redirect_after_account_save', 999 );
function matreshka_redirect_after_account_save() {
if ( 0 < wc_notice_count( 'error' ) ) {
return; // let WC show errors on the same page
}
wc_add_notice( 'Профиль успешно обновлён ✓', 'success' );
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) );
exit;
}