| 1 |
<?php namespace TierPricingTable\Integrations\Plugins; |
| 2 |
|
| 3 |
class WooCommerceProductAddons { |
| 4 |
|
| 5 |
/** |
| 6 |
* WooCommerceProductAddons constructor. |
| 7 |
*/ |
| 8 |
public function __construct() { |
| 9 |
include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 10 |
|
| 11 |
if ( is_plugin_active( 'woocommerce-product-addons/woocommerce-product-addons.php' ) ) { |
| 12 |
|
| 13 |
add_action( 'wp_head', array( $this, 'addCompatibilityScript' ) ); |
| 14 |
|
| 15 |
add_action( 'tier_pricing_table/cart/product_cart_price', array( $this, 'addAddonsPrice' ), 10, 2 ); |
| 16 |
add_action( 'tier_pricing_table/cart/product_cart_price/item', array( $this, 'addAddonsPrice' ), 10, 2 ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Add extra addons costs to product price in cart. |
| 22 |
* |
| 23 |
* @param float $price |
| 24 |
* @param array $cart_item |
| 25 |
* |
| 26 |
* @return int|mixed |
| 27 |
*/ |
| 28 |
public function addAddonsPrice( $price, $cart_item ) { |
| 29 |
|
| 30 |
$extra_cost = 0; |
| 31 |
|
| 32 |
if ( isset( $cart_item['addons'] ) && false != $price ) { |
| 33 |
foreach ( $cart_item['addons'] as $addon ) { |
| 34 |
$price_type = $addon['price_type']; |
| 35 |
$addon_price = $addon['price']; |
| 36 |
|
| 37 |
switch ( $price_type ) { |
| 38 |
case 'percentage_based': |
| 39 |
$extra_cost += (float) ( $cart_item['data']->get_price( 'edit' ) * ( $addon_price / 100 ) ); |
| 40 |
break; |
| 41 |
case 'flat_fee': |
| 42 |
$extra_cost += (float) ( $addon_price / $cart_item['quantity'] ); |
| 43 |
break; |
| 44 |
default: |
| 45 |
$extra_cost += (float) $addon_price; |
| 46 |
break; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return $price + $extra_cost; |
| 51 |
} |
| 52 |
|
| 53 |
return $price; |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Render compatibility script |
| 59 |
*/ |
| 60 |
public function addCompatibilityScript() { |
| 61 |
?> |
| 62 |
<script> |
| 63 |
(function ($) { |
| 64 |
$(document).on('tiered_price_update', function (event, data) { |
| 65 |
$('#product-addons-total').data('price', data.price); |
| 66 |
}); |
| 67 |
})(jQuery); |
| 68 |
</script> |
| 69 |
<?php |
| 70 |
} |
| 71 |
} |
| 72 |
|