is_page()) return null; return intval(basename(strtok($_SERVER["REQUEST_URI"], '?'))); } private function get_order():?WC_Order { if (is_null($order_id = $this->get_order_id())) return null; return wc_get_order($order_id); } private function current_user_can(WC_Order $order):bool { return is_user_logged_in() && get_current_user_id() === $order->get_user_id(); } public function render_order_details():void { $order = $this->get_order(); if (!$this->current_user_can($order)) return; wc_get_template('order/order-details-customer.php', ['order' => $order]); } private function get_address_title(string $address_type, int $customer_id = 0):string { if (!$customer_id) $customer_id = get_current_user_id(); $addresses = apply_filters( 'woocommerce_my_account_get_addresses', [ 'billing' => __('Billing address', 'woocommerce'), 'shipping' => __('Shipping address', 'woocommerce'), ], $customer_id ); if (!array_key_exists($address_type, $addresses)) return ucwords($address_type); return $addresses[$address_type]; } public function render_edit_address_link(string $address_type, WC_Order $order):void { if (!$this->is_page()) return; printf( '
', esc_url(wc_get_endpoint_url('edit-address', $address_type, get_permalink(get_option('woocommerce_myaccount_page_id')))), esc_html(sprintf( __('Edit %s', 'woocommerce'), $this->get_address_title($address_type, $order->get_user_id()) )) ); } private function get_incomplete_order_statuses():array { return array_unique(array_merge(wc_get_is_pending_statuses(), [ OrderInternalStatus::PENDING, OrderInternalStatus::FAILED, ])); } private function get_incomplete_orders(int $user_id):array { return wc_get_orders([ 'limit' => -1, 'customer_id' => $user_id, 'date_created' => '<=' . time(), 'orderby' => 'date', 'order' => 'DESC', 'status' => $this->get_incomplete_order_statuses(), ]); } public function save_address(int $user_id, string $address_type, array $address_fields = [], WC_Customer|null $customer = null):void { if (empty($address_fields) || is_null($customer)) return; if (!in_array($address_type, ['billing', 'shipping'])) return; // Get customer's incomplete orders if (empty($orders = $this->get_incomplete_orders($user_id))) return; // Get address data from customer $address_data = []; foreach ($address_fields as $key => $field) { $data_key = strpos($key, "{$address_type}_") === 0 ? substr($key, strlen("{$address_type}_")) : ""; if (is_callable([$customer, "get_{$key}"])) { $address_data[$data_key] = $customer->{"get_{$key}"}(); } else { $address_data[$data_key] = $customer->get_meta($key); } } // Update orders with new address data foreach ($orders as $order) { $order->{"set_{$address_type}"}($address_data); $order->save(); } } } WCFPA::instance();