PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.7
Tiered Pricing Table for WooCommerce v7.1.7
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Integrations / Plugins / WooCommerceDeposits.php

WooCommerceDeposits.php in Tiered Pricing Table for WooCommerce 7.1.7, at src/Integrations/Plugins/WooCommerceDeposits.php

58 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Integrations\Plugins;
2
3 class WooCommerceDeposits extends PluginIntegrationAbstract {
4
5 public function getTitle(): string {
6 return 'WooCommerce Deposits (by WooCommerce)';
7 }
8
9 public function getDescription(): string {
10 return __( 'Calculate deposit amounts and remaining balances correctly based on tiered pricing rules.', 'tier-pricing-table' );
11 }
12
13 public function getSlug(): string {
14 return 'woocommerce-deposits';
15 }
16
17 public function getAuthorURL(): string {
18 return 'https://woocommerce.com/products/woocommerce-deposits/';
19 }
20
21 public function getIconURL(): string {
22 return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/woocommerce-develop.jpeg' );
23 }
24
25 public function run() {
26 add_filter( 'tiered_pricing_table/cart/product_cart_price', function ( $new_price, $cart_item, $key ) {
27
28 if ( ! $new_price ) {
29 return $new_price;
30 }
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
49 return $new_price;
50
51 }, 10, 3 );
52 }
53
54 public function getIntegrationCategory(): string {
55 return 'custom_product_types';
56 }
57 }
58