resolve_breakdown(); if ( null === $breakdown || ! function_exists( 'wc_print_notice' ) ) { return; } wc_print_notice( $this->build_notice_text( $breakdown ), 'notice' ); } /** * Two rows appended to the cart page's totals box. */ public function render_cart_totals_rows(): void { $this->render_totals_rows(); } /** * Same two rows, appended to the checkout page's order review box. */ public function render_checkout_totals_rows(): void { $this->render_totals_rows(); } /** * Shared row markup for both the cart totals box and the checkout * order review box — both use the same ', esc_html( $deposit_label ), esc_attr( $deposit_label ), esc_html( sdfw_format_price( $breakdown['advance'] ) ) ); if ( $breakdown['fee'] > 0.0 ) { $fee_label = __( 'Processing Fee', 'aawahid-simple-deposits-for-woocommerce' ); printf( '', esc_html( $fee_label ), esc_attr( $fee_label ), esc_html( sdfw_format_price( $breakdown['fee'] ) ) ); } printf( '', esc_html( $cod_label ), esc_attr( $cod_label ), esc_html( sdfw_format_price( $breakdown['cod_due'] ) ) ); // new — only appears when a fee is actually configured, so stores // with the fee disabled see no change to the totals box at all. if ( $breakdown['fee'] > 0.0 ) { $payable_label = __( 'Total Payable (Now + On Delivery)', 'aawahid-simple-deposits-for-woocommerce' ); // cod_due already includes the fee (see resolve_breakdown()) — // summing it again here would double-count it. $payable_amount = $breakdown['advance'] + $breakdown['cod_due']; printf( '', esc_html( $payable_label ), esc_attr( $payable_label ), esc_html( sdfw_format_price( $payable_amount ) ) ); } } /** * Build the notice text: the merchant's configured template * (sdfw_notice_text, {amount} placeholder) with the resolved * advance substituted in, plus an optional "(Deposit set by: * Product Name)" suffix when a specific product's override — not * the global default — is what set the amount. * * @param array $breakdown See resolve_breakdown(). */ private function build_notice_text( array $breakdown ): string { $template = (string) get_option( 'sdfw_notice_text', sdfw_get_default_notice_text() ); $message = strtr( $template, [ '{amount}' => sdfw_format_price( $breakdown['advance'] ) ] ); if ( $breakdown['fee'] > 0.0 ) { $message .= ' ' . sprintf( /* translators: %s: formatted processing fee added to the amount due on delivery */ __( 'A %s processing fee also applies, added to the amount due on delivery.', 'aawahid-simple-deposits-for-woocommerce' ), sdfw_format_price( $breakdown['fee'] ) ); } $source = $breakdown['source']; if ( $source instanceof \WC_Product ) { $message .= ' ' . sprintf( /* translators: %s: name of the product whose override set the deposit amount */ __( '(Deposit set by: %s)', 'aawahid-simple-deposits-for-woocommerce' ), $source->get_name() ); } return wp_kses_post( $message ); } /** * Resolve everything needed to render the breakdown, in one place, * from the live cart. Returns null whenever nothing should render — * callers don't need their own enabled/empty/zero checks. * * @return array{advance: float, cod_due: float, source: \WC_Product|null}|null */ private function resolve_breakdown(): ?array { if ( ! function_exists( 'WC' ) || ! WC()->cart instanceof \WC_Cart || WC()->cart->is_empty() ) { return null; } $cart = WC()->cart; $advance = SDFW_Calculator::get_advance_for_cart( $cart ); if ( $advance <= 0.0 ) { return null; } $total = (float) $cart->get_total( 'edit' ); $fee = SDFW_Calculator::get_processing_fee_for_cart( $cart ); // Fee is added to the on-delivery amount, not the online deposit — // mirrors SDFW_Gateway::maybe_apply_deposit_to_order() exactly, so // the cart/checkout display always agrees with what the order will // actually record once placed. $cod_due = SDFW_Calculator::get_cod_remainder( $total, $advance ) + $fee; $source = SDFW_Calculator::get_advance_source_product( $cart ); return [ 'advance' => $advance, 'cod_due' => $cod_due, 'source' => $source, 'fee' => $fee, ]; } }
/ shape, * so one implementation covers both hooks. */ private function render_totals_rows(): void { $breakdown = $this->resolve_breakdown(); if ( null === $breakdown ) { return; } $deposit_label = __( 'Pay Now (Deposit)', 'aawahid-simple-deposits-for-woocommerce' ); $cod_label = __( 'Pay on Delivery (COD)', 'aawahid-simple-deposits-for-woocommerce' ); printf( '
%1$s%3$s
%1$s%3$s
%1$s%3$s
%1$s%3$s