id, array( 'edit-shop_order', 'woocommerce_page_wc-orders' ), true ) ) { return; } // The HPOS order edit screen shares the list's screen id. // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only screen routing. if ( isset( $_GET['action'] ) && in_array( sanitize_key( wp_unslash( $_GET['action'] ) ), array( 'edit', 'new' ), true ) ) { return; } add_filter( 'woocommerce_get_formatted_order_total', array( $this, 'orders_list_full_total' ), 10, 4 ); } /** * Swap the deposit for the full order value while the list table is rendered. * * @param string $formatted_total Formatted total HTML. * @param WC_Order $order Order object. * @param string $tax_display Tax display mode. * @param bool $display_refunded Whether refunds are shown. * @return string */ public function orders_list_full_total( $formatted_total, $order, $tax_display = '', $display_refunded = true ) { // Bulk actions run before the page header and may send emails; leave those alone. if ( ! did_action( 'in_admin_header' ) || ! $order instanceof WC_Order || $order->get_total_refunded() ) { return $formatted_total; } if ( ! apply_filters( 'apd_display_full_order_total', true, $order ) || ! APD_Order::is_deposit_order( $order ) ) { return $formatted_total; } $total_amount = floatval( $order->get_meta( '_apd_total_amount' ) ); if ( $total_amount <= 0 ) { return $formatted_total; } return APD_Order::swap_in_full_total( (string) $formatted_total, $order, $total_amount ); } /** * Qualify the order items "Order Total" label on deposit orders. * * On a deposit order that total is the amount charged in the current payment, not the * order value, and WooCommerce prints the row with no hook of its own. The gettext * filter is added right before that row and removed right after the table, so it can * only ever touch this one label. * * @param int $order_id Order ID. */ public function start_order_total_label( $order_id ) { $order = wc_get_order( $order_id ); if ( ! $order || ! APD_Order::is_deposit_order( $order ) ) { return; } $full_total = floatval( $order->get_meta( '_apd_total_amount' ) ); if ( $full_total <= 0 || 0.0 === round( $full_total - (float) $order->get_total(), wc_get_price_decimals() ) ) { return; } $this->order_total_suffix = APD_Order::has_pending_balance_payment( $order ) ? __( 'Balance payment', 'advanced-partial-payment-or-deposit-for-woocommerce' ) : __( 'Deposit', 'advanced-partial-payment-or-deposit-for-woocommerce' ); add_filter( 'gettext_woocommerce', array( $this, 'filter_order_total_label' ), 10, 2 ); } /** * Append the deposit qualifier to WooCommerce's "Order Total" label. * * @param string $translation Translated text. * @param string $text Original text. * @return string */ public function filter_order_total_label( $translation, $text ) { if ( 'Order Total' !== $text || '' === $this->order_total_suffix ) { return $translation; } return sprintf( '%s (%s)', $translation, $this->order_total_suffix ); } /** * Show the full order value, paid amount and balance under the order items totals. * * @param int $order_id Order ID. */ public function render_order_totals_deposit_rows( $order_id ) { remove_filter( 'gettext_woocommerce', array( $this, 'filter_order_total_label' ), 10 ); $this->order_total_suffix = ''; $order = wc_get_order( $order_id ); $details = $order ? APD_Order::get_deposit_details( $order ) : false; if ( ! $details ) { return; } $currency = array( 'currency' => $order->get_currency() ); $rows = array( array( __( 'Full Order Total', 'advanced-partial-payment-or-deposit-for-woocommerce' ), $details['total_amount'], 'label label-highlight' ), array( __( 'Deposit Amount', 'advanced-partial-payment-or-deposit-for-woocommerce' ), $details['deposit_amount'], 'label' ), array( __( 'Total Paid', 'advanced-partial-payment-or-deposit-for-woocommerce' ), $details['amount_paid'], 'label' ), array( __( 'Balance Due', 'advanced-partial-payment-or-deposit-for-woocommerce' ), $details['balance_due'], 'label' ), ); foreach ( $rows as $row ) { ?> : get( \Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; add_meta_box( 'apd-deposit-details', __( '๐Ÿ’ฐ Deposit Details', 'advanced-partial-payment-or-deposit-for-woocommerce' ), array( $this, 'render_deposit_metabox' ), $screen, 'side', 'high' ); add_meta_box( 'apd-deposit-payment-record', __( 'Deposit Payment Record', 'advanced-partial-payment-or-deposit-for-woocommerce' ), array( $this, 'render_payment_record_metabox' ), $screen, 'normal', 'default' ); } /** * Render deposit metabox. */ public function render_deposit_metabox( $post_or_order ) { $order = $this->get_order_object( $post_or_order ); if ( ! $order || ! APD_Order::is_deposit_order( $order ) ) { echo '

' . esc_html__( 'This is not a deposit order.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) . '

'; if ( APD_Admin_Order_Deposit::is_enabled() && APD_Admin_Order_Deposit::can_add_deposit( $order ) ) { APD_Admin_Order_Deposit::render_form( $order ); } return; } $details = APD_Order::get_deposit_details( $order ); if ( ! $details ) { return; } $can_edit_deposit = APD_Admin_Order_Deposit::is_enabled() && APD_Admin_Order_Deposit::can_edit_deposit( $order ); $paid_color = $details['amount_paid'] > 0 ? '#00a32a' : '#646970'; // Suggest what is actually due next: the balance, or a payment plan's next instalment. $manual_amount_due = ! empty( $details['deposit_paid'] ) ? APD_Order::get_balance_amount_due_now( $order ) : min( $details['deposit_amount'], $details['balance_due'] ); ?>
0 ) : ?>

$entry ) : ?>
get_payment_type_label( $entry ) ); ?> ยท
get_order_object( $post_or_order ); if ( ! $order || ! APD_Order::is_deposit_order( $order ) ) { echo '

' . esc_html__( 'No deposit payment record available for this order.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) . '

'; return; } $details = APD_Order::get_deposit_details( $order ); if ( ! $details ) { echo '

' . esc_html__( 'No payment record found.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) . '

'; return; } $history = is_array( $details['history'] ) ? $details['history'] : array(); $running_total = 0; $paid_color = $details['amount_paid'] > 0 ? '#00a32a' : '#646970'; ?>
$entry ) : ?>
get_payment_type_label( $entry ) ); ?>
$label ) { $new_columns[ $key ] = $label; if ( 'order_total' === $key ) { $new_columns['apd_deposit_status'] = __( 'Deposit', 'advanced-partial-payment-or-deposit-for-woocommerce' ); } } return $new_columns; } /** * Render deposit column (legacy). */ public function render_order_columns( $column, $post_id ) { if ( 'apd_deposit_status' === $column ) { $order = wc_get_order( $post_id ); $this->render_deposit_column_content( $order ); } } /** * Render deposit column (HPOS). */ public function render_order_columns_hpos( $column, $order ) { if ( 'apd_deposit_status' === $column ) { $this->render_deposit_column_content( $order ); } } /** * Column content. */ private function render_deposit_column_content( $order ) { if ( ! $order || ! APD_Order::is_deposit_order( $order ) ) { echo 'โ€”'; return; } $details = APD_Order::get_deposit_details( $order ); if ( ! $details ) { return; } if ( $details['balance_due'] > 0 ) { // Until the deposit is confirmed, what the customer owes now is the deposit, not the whole balance. if ( $details['deposit_paid'] ) { $due = $details['balance_due']; $title = __( 'Balance Due', 'advanced-partial-payment-or-deposit-for-woocommerce' ); } else { $settings = get_option( 'apd_settings', array() ); $due = min( $details['deposit_amount'], $details['balance_due'] ); /* translators: %s: configured deposit label. */ $title = sprintf( __( '%s Due Now', 'advanced-partial-payment-or-deposit-for-woocommerce' ), $settings['deposit_label'] ?? __( 'Deposit', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); } printf( '%s %s', esc_attr( $title ), esc_html__( 'Due:', 'advanced-partial-payment-or-deposit-for-woocommerce' ), wp_kses_post( wc_price( $due, array( 'currency' => $order->get_currency() ) ) ) ); } else { echo 'โœ“ ' . esc_html__( 'Paid', 'advanced-partial-payment-or-deposit-for-woocommerce' ) . ''; } } /** * AJAX: Record manual payment. */ public function ajax_record_payment() { check_ajax_referer( 'apd_admin_nonce', 'nonce' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_send_json_error( __( 'Permission denied.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); } $order_id = intval( $_POST['order_id'] ?? 0 ); $amount = floatval( $_POST['amount'] ?? 0 ); if ( ! $order_id || $amount <= 0 ) { wp_send_json_error( __( 'Invalid data.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); } $result = APD_Order::record_payment( $order_id, $amount, __( 'Manual payment recorded by admin', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); if ( $result ) { wp_send_json_success( __( 'Payment recorded!', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); } else { wp_send_json_error( __( 'Failed to record payment.', 'advanced-partial-payment-or-deposit-for-woocommerce' ) ); } } /** * Custom styling for partially-paid status in orders list. */ public function order_status_styles() { $screen = get_current_screen(); if ( ! $screen ) { return; } ?> ID ) : $post_or_order; } /** * Format a readable payment type label. * * @param array $entry Payment history entry. * @return string */ private function get_payment_type_label( $entry ) { $type = $entry['type'] ?? ''; switch ( $type ) { case 'deposit': return __( 'Initial Deposit', 'advanced-partial-payment-or-deposit-for-woocommerce' ); case 'balance_payment': return __( 'Remaining Balance', 'advanced-partial-payment-or-deposit-for-woocommerce' ); default: return __( 'Payment', 'advanced-partial-payment-or-deposit-for-woocommerce' ); } } }