| @@ -2,13 +2,13 @@ | ||
| 2 | 2 | |
| 3 | 3 | class WooCommerceDeposits extends PluginIntegrationAbstract { |
| 4 | 4 | |
| 5 | 5 | public function getTitle(): string { |
| 6 | - return __( 'WooCommerce Deposits (by WooCommerce)', 'tier-pricing-table' ); | |
| 6 | + return 'WooCommerce Deposits (by WooCommerce)'; | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | public function getDescription(): string { |
| 10 | - return __( 'Make tiered pricing properly work with deposit purchases.', 'tier-pricing-table' ); | |
| 10 | + return __( 'Calculate deposit amounts and remaining balances correctly based on tiered pricing rules.', 'tier-pricing-table' ); | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | public function getSlug(): string { |
| 14 | 14 | return 'woocommerce-deposits'; |
| @@ -23,24 +23,32 @@ | ||
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | public function run() { |
| 26 | 26 | add_filter( 'tiered_pricing_table/cart/product_cart_price', function ( $new_price, $cart_item, $key ) { |
| 27 | - | |
| 28 | - if ( $new_price ) { | |
| 29 | - // WooCommerce Deposit | |
| 30 | - $cart = wc()->cart; | |
| 31 | - | |
| 32 | - if ( isset( $cart->cart_contents[ $key ]['full_amount'] ) ) { | |
| 33 | - | |
| 34 | - $depositPercentage = 1 / ( $cart->cart_contents[ $key ]['full_amount'] / $cart->cart_contents[ $key ]['deposit_amount'] ); | |
| 35 | - | |
| 36 | - $cart->cart_contents[ $key ]['full_amount'] = $new_price; | |
| 37 | - $cart->cart_contents[ $key ]['deposit_amount'] = $cart->cart_contents[ $key ]['full_amount'] * $depositPercentage; | |
| 38 | - } | |
| 27 | + | |
| 28 | + if ( ! $new_price ) { | |
| 29 | + return $new_price; | |
| 39 | 30 | } |
| 40 | - | |
| 31 | + | |
| 32 | + // Only WooCommerce Deposits populates these cart-item keys, so their presence gates this to deposit products. | |
| 33 | + $cart = wc()->cart; | |
| 34 | + | |
| 35 | + if ( ! isset( $cart->cart_contents[ $key ]['full_amount'], $cart->cart_contents[ $key ]['deposit_amount'] ) ) { | |
| 36 | + return $new_price; | |
| 37 | + } | |
| 38 | + | |
| 39 | + $fullAmount = (float) $cart->cart_contents[ $key ]['full_amount']; | |
| 40 | + $depositAmount = (float) $cart->cart_contents[ $key ]['deposit_amount']; | |
| 41 | + | |
| 42 | + // Keep the original deposit-to-full ratio and re-apply it to the new tiered price. Guard the | |
| 43 | + // zero full amount (e.g. 100%-later plans, where the deposit is 0 too) to avoid dividing by zero. | |
| 44 | + $depositPercentage = $fullAmount > 0 ? ( $depositAmount / $fullAmount ) : 0; | |
| 45 | + | |
| 46 | + $cart->cart_contents[ $key ]['full_amount'] = $new_price; | |
| 47 | + $cart->cart_contents[ $key ]['deposit_amount'] = $new_price * $depositPercentage; | |
| 48 | + | |
| 41 | 49 | return $new_price; |
| 42 | - | |
| 50 | + | |
| 43 | 51 | }, 10, 3 ); |
| 44 | 52 | } |
| 45 | 53 | |
| 46 | 54 | public function getIntegrationCategory(): string { |