0 ) { return (float) $override; } // Variation with no override of its own — check the parent product. $parent_id = wp_get_post_parent_id( $product_id ); if ( $parent_id ) { $parent_override = get_post_meta( $parent_id, '_sdfw_amount', true ); if ( '' !== $parent_override && null !== $parent_override && is_numeric( $parent_override ) && (float) $parent_override > 0 ) { return (float) $parent_override; } } return sdfw_get_default_amount( $basis ); } } if ( ! function_exists( 'sdfw_get_default_notice_text' ) ) { /** * The default checkout/cart notice template, with its {amount} * placeholder. Single source of truth — used as the seeded value * on activation (aawahid-simple-deposits-for-woocommerce.php), the * settings field default (SDFW_Settings), and the get_option() * fallback wherever the notice is actually rendered (SDFW_Cart) — * so the three can never drift out of sync with each other. */ function sdfw_get_default_notice_text(): string { return __( 'Pay ৳{amount} now to confirm your order. The remaining amount is payable on delivery.', 'aawahid-simple-deposits-for-woocommerce' ); } } if ( ! function_exists( 'sdfw_format_price' ) ) { /** * Format an amount using WooCommerce's price formatting if available, * otherwise fall back to a plain Taka-symbol string so admin screens * never show a broken value if called too early. */ function sdfw_format_price( float $amount ): string { if ( function_exists( 'wc_price' ) ) { return wp_strip_all_tags( wc_price( $amount ) ); } return '৳' . number_format( $amount, 2 ); } } if ( ! function_exists( 'sdfw_is_processing_fee_enabled' ) ) { /** * Whether the deposit-only processing fee is switched on. */ function sdfw_is_processing_fee_enabled(): bool { return 'yes' === get_option( 'sdfw_processing_fee_enabled', 'no' ); } } if ( ! function_exists( 'sdfw_get_processing_fee_amount' ) ) { /** * Resolve the processing fee for a given resolved deposit amount, * per the merchant's configured fee type (flat currency amount or * a percentage of the deposit). Deposit-only by design: callers * are expected to pass 0.0 (or never call this) for full-payment * orders, so there's no separate "is this a deposit order" check * inside this function itself. * Note: this function only resolves the fee amount itself — where * it's applied (added to the on-delivery balance, not the online * deposit) is decided by the caller; see * SDFW_Gateway::maybe_apply_deposit_to_order(). * * Returns 0.0 if the feature is disabled or the deposit is 0 or * less — a defensive floor, not the primary gate (that lives in * * @param float $deposit The resolved deposit amount the fee is based on. */ function sdfw_get_processing_fee_amount( float $deposit ): float { if ( ! sdfw_is_processing_fee_enabled() || $deposit <= 0.0 ) { return 0.0; } $type = get_option( 'sdfw_processing_fee_type', 'flat' ); $amount = max( 0.0, (float) get_option( 'sdfw_processing_fee_amount', '0' ) ); $fee = ( 'percent' === $type ) ? ( $deposit * ( $amount / 100 ) ) : $amount; $decimals = function_exists( 'wc_get_price_decimals' ) ? wc_get_price_decimals() : 2; return round( max( 0.0, $fee ), $decimals ); } } // ── Tiered deposit rules ──────────────────────────────────────────── if ( ! function_exists( 'sdfw_are_tiers_enabled' ) ) { /** * Whether tiered (order-total based) deposit rules are switched on. */ function sdfw_are_tiers_enabled(): bool { return 'yes' === get_option( 'sdfw_tiers_enabled', 'no' ); } } if ( ! function_exists( 'sdfw_get_default_tiers' ) ) { /** * Starter rules: under 10,000 => 200, 10,000 and above => 500. * Used until the merchant saves their own tiers. */ function sdfw_get_default_tiers(): array { return [ [ 'min' => 0.0, 'max' => 10000.0, 'amount' => 200.0 ], [ 'min' => 10000.0, 'max' => '', 'amount' => 500.0 ], ]; } } if ( ! function_exists( 'sdfw_sanitize_tiers' ) ) { /** * Clean raw tier rows (from a form post or the database) into a * sorted list of ['min' => float, 'max' => float|'', 'amount' => float]. * * min is inclusive, max is exclusive, blank max = no upper limit. * Rows with no positive deposit, or with max <= min, are dropped. * * @param mixed $raw */ function sdfw_sanitize_tiers( $raw ): array { if ( ! is_array( $raw ) ) { return []; } $to_float = static function ( $value ): ?float { $value = trim( (string) $value ); if ( '' === $value ) { return null; } $value = function_exists( 'wc_format_decimal' ) ? wc_format_decimal( $value ) : $value; return is_numeric( $value ) ? (float) $value : null; }; $tiers = []; foreach ( $raw as $row ) { if ( ! is_array( $row ) ) { continue; } $min = $to_float( $row['min'] ?? '' ); $min = null === $min ? 0.0 : max( 0.0, $min ); $max = $to_float( $row['max'] ?? '' ); $amount = $to_float( $row['amount'] ?? '' ); if ( null === $amount || $amount <= 0.0 ) { continue; } if ( null !== $max && $max <= $min ) { continue; } $tiers[] = [ 'min' => round( $min, 2 ), 'max' => null === $max ? '' : round( $max, 2 ), 'amount' => round( $amount, 2 ), ]; } usort( $tiers, static function ( array $a, array $b ): int { $by_min = $a['min'] <=> $b['min']; if ( 0 !== $by_min ) { return $by_min; } $a_max = '' === $a['max'] ? INF : $a['max']; $b_max = '' === $b['max'] ? INF : $b['max']; return $a_max <=> $b_max; } ); return $tiers; } } if ( ! function_exists( 'sdfw_get_tiers' ) ) { /** * The merchant's saved tiers, sorted and sanitised. Falls back to * the starter rules only if nothing has ever been saved. */ function sdfw_get_tiers(): array { $stored = get_option( 'sdfw_deposit_tiers', null ); if ( ! is_array( $stored ) ) { return sdfw_get_default_tiers(); } return sdfw_sanitize_tiers( $stored ); } } if ( ! function_exists( 'sdfw_get_tier_amount' ) ) { /** * Deposit amount of the first tier whose range contains $basis * (min <= basis < max), or null if no tier matches. */ function sdfw_get_tier_amount( float $basis ): ?float { foreach ( sdfw_get_tiers() as $tier ) { if ( $basis >= $tier['min'] && ( '' === $tier['max'] || $basis < $tier['max'] ) ) { return (float) $tier['amount']; } } return null; } } if ( ! function_exists( 'sdfw_get_default_amount' ) ) { /** * The deposit a product gets when it has NO override of its own: * the matching tier when tiers are on and a basis is known, * otherwise the global amount (also the fallback for any gap * between tiers). */ function sdfw_get_default_amount( ?float $basis = null ): float { if ( null !== $basis && sdfw_are_tiers_enabled() ) { $tier_amount = sdfw_get_tier_amount( $basis ); if ( null !== $tier_amount ) { return $tier_amount; } } return sdfw_get_global_amount(); } }