-
Notifications
You must be signed in to change notification settings - Fork 20
/
class-woocommerce-product-fees.php
86 lines (74 loc) · 2.24 KB
/
class-woocommerce-product-fees.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* WooCommerce Product Fees
*
* Add the fees at checkout.
*
* @class WooCommerce_Product_Fees
* @author Caleb Burks
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class WooCommerce_Product_Fees {
/**
* Constructor for the main product fees class.
*
* @access public
*/
public function __construct() {
if ( is_admin() ) {
// Product Settings
require_once 'admin/class-wcpf-admin-product-settings.php';
new WCPF_Admin_Product_Settings();
// Global Settings
require_once 'admin/class-wcpf-admin-global-settings.php';
new WCPF_Admin_Global_Settings();
}
// Fee Classes
require_once( 'fees/class-wcpf-fee.php' );
require_once( 'fees/class-wcpf-product-fee.php' );
require_once( 'fees/class-wcpf-variation-fee.php' );
// Text Domain
add_action( 'plugins_loaded', array( $this, 'text_domain' ) );
// Hook in for fees to be added
add_action( 'woocommerce_cart_calculate_fees', array( $this, 'add_fees' ), 15 );
}
/**
* Load Text Domain
*/
public function text_domain() {
load_plugin_textdomain( 'woocommerce-product-fees', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Add all fees at checkout.
*
* @access public
*/
public function add_fees() {
foreach( WC()->cart->get_cart() as $cart_item => $values ) {
// Assume there is no fee.
$fee = false;
if ( 0 !== $values['variation_id'] ) {
// Get variation fee. Will return false if there is no fee.
$fee = new WCPF_Variation_Fee( $values['product_id'], $values['quantity'], $values['data']->price, $values['variation_id'] );
}
if ( ! $fee ) {
// Get product fee. Will return false if there is no fee.
$fee = new WCPF_Product_Fee( $values['product_id'], $values['quantity'], $values['data']->price );
}
if ( $fee->return_fee() ) {
$data = $fee->return_fee();
do_action( 'wcpf_before_fee_is_added', $data );
// Check if taxes need to be added.
if ( get_option( 'wcpf_fee_tax_class', '' ) !== '' ) {
WC()->cart->add_fee( $data['name'], $data['amount'], true, get_option( 'wcpf_fee_tax_class' ) );
} else {
WC()->cart->add_fee( $data['name'], $data['amount'], false );
}
do_action( 'wcpf_after_fee_is_added', $data );
}
}
}
}