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 / WooCommerceProductAddons.php

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

123 lines 3.3 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 use TierPricingTable\PriceManager;
4 use WC_Product;
5
6 class WooCommerceProductAddons extends PluginIntegrationAbstract {
7
8 /**
9 * Add extra addons costs to product price in cart.
10 *
11 * @param float $price
12 * @param array $cart_item
13 *
14 * @return float
15 */
16 public function addAddonsPrice( $price, $cart_item ) {
17
18 $extra_cost = 0;
19
20 if ( isset( $cart_item['addons'] ) && false != $price ) {
21 foreach ( $cart_item['addons'] as $addon ) {
22 $price_type = $addon['price_type'];
23 $addon_price = $addon['price'];
24
25 switch ( $price_type ) {
26
27 case 'percentage_based':
28 $extra_cost += (float) ( $price * ( $addon_price / 100 ) );
29 break;
30 case 'flat_fee':
31 $extra_cost += (float) ( $addon_price / $cart_item['quantity'] );
32 break;
33 default:
34 $extra_cost += (float) $addon_price;
35 break;
36 }
37 }
38
39 return $price + $extra_cost;
40 }
41
42 return $price;
43 }
44
45 /**
46 * Render compatibility script
47 */
48 public function addCompatibilityScript() {
49 ?>
50 <script>
51 (function ($) {
52 $('.tpt__tiered-pricing').on('tiered_price_update', function (event, data) {
53 $('#product-addons-total').filter('[data-product-id=' + data.parentId + ']').data('price', data.price);
54 });
55 })(jQuery);
56 </script>
57 <?php
58 }
59
60 public function getTitle(): string {
61 return 'Product Add-ons (by WooCommerce)';
62 }
63
64 public function getDescription(): string {
65 return __( 'Calculate tiered pricing accurately when WooCommerce Product Add-ons are selected.', 'tier-pricing-table' );
66 }
67
68 public function getSlug(): string {
69 return 'product-add-ons';
70 }
71
72 public function getAuthorURL(): string {
73 return 'https://woocommerce.com/products/product-add-ons/';
74 }
75
76 public function getIconURL(): string {
77 return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/woocommerce-develop.jpeg' );
78 }
79
80 public function run() {
81 include_once ABSPATH . 'wp-admin/includes/plugin.php';
82
83 if ( is_plugin_active( 'woocommerce-product-addons/woocommerce-product-addons.php' ) ) {
84
85 add_action( 'wp_footer', array( $this, 'addCompatibilityScript' ) );
86
87 add_action( 'tiered_pricing_table/cart/product_cart_price', array( $this, 'addAddonsPrice' ), 20, 2 );
88 add_action( 'tiered_pricing_table/cart/product_cart_price/item', array( $this, 'addAddonsPrice' ), 20, 2 );
89
90 // Handle the case when product addons request product price via AJAX
91 add_filter( 'woocommerce_product_addons_ajax_get_product_price_excluding_tax',
92 function ( $price, $qty, WC_Product $product ) {
93
94 $pricingRule = PriceManager::getPricingRule( $product->get_id() );
95 $newPrice = $pricingRule->getTierPrice( $qty, false );
96
97 if ( $newPrice ) {
98 return $newPrice * $qty;
99 }
100
101 return $price;
102 }, 10, 3 );
103
104 add_filter( 'woocommerce_product_addons_ajax_get_product_price_including_tax',
105 function ( $price, $qty, WC_Product $product ) {
106
107 $pricingRule = PriceManager::getPricingRule( $product->get_id() );
108 $newPrice = $pricingRule->getTierPrice( $qty );
109
110 if ( $newPrice ) {
111 return $newPrice * $qty;
112 }
113
114 return $price;
115 }, 10, 3 );
116 }
117 }
118
119 public function getIntegrationCategory(): string {
120 return 'product_addons';
121 }
122 }
123