Easy Digital Downloads
  • Package
  • Function
  • Tree

Packages

  • EDD
    • Admin
      • Actions
      • Add-ons
      • Dashboard
      • Discounts
      • Downloads
      • Export
      • Notices
      • Pages
      • Payments
      • Reports
      • Settings
      • System
      • Upgrades
      • Upload
      • Welcome
    • Cart
    • Checkout
    • Classes
      • API
      • Fees
      • HTML
      • Roles
      • Session
    • Emails
    • Functions
      • AJAX
      • Compatibility
      • Errors
      • Formatting
      • Install
      • Login
      • Taxes
      • Templates
    • Gateways
    • Logging
    • Payments
    • Shortcodes
    • Widgets

Functions

  • edd_calculate_tax
  • edd_get_sales_tax_for_year
  • edd_get_tax_rate
  • edd_is_cart_taxed
  • edd_local_tax_opted_in
  • edd_local_taxes_only
  • edd_opt_into_local_taxes
  • edd_opt_out_local_taxes
  • edd_prices_include_tax
  • edd_prices_show_tax_on_checkout
  • edd_sales_tax_for_year
  • edd_taxes_after_discounts
  • edd_taxes_on_prices
  • edd_use_taxes
  1 <?php
  2 /**
  3  * Tax Functions
  4  *
  5  * These are functions used for checking if taxes are enabled, calculating taxes, etc.
  6  * Functions for retrieving tax amounts and such for individual payments are in
  7  * includes/payment-functions.php and includes/cart-functions.php
  8  *
  9  * @package     EDD
 10  * @subpackage  Functions/Taxes
 11  * @copyright   Copyright (c) 2013, Pippin Williamson
 12  * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 13  * @since       1.3.3
 14 */
 15 
 16 // Exit if accessed directly
 17 if ( !defined( 'ABSPATH' ) ) exit;
 18 
 19 /**
 20  * Checks if taxes are enabled by using the option set from the EDD Settings.
 21  * The value returned can be filtered.
 22  *
 23  * @since 1.3.3
 24  * @global $edd_options
 25  * @return bool Whether or not taxes are enabled
 26  */
 27 function edd_use_taxes() {
 28     global $edd_options;
 29 
 30     return apply_filters( 'edd_use_taxes', isset( $edd_options['enable_taxes'] ) );
 31 }
 32 
 33 /**
 34  * Check if only local taxes are enabled meaning users must opt in by using the
 35  * option set from the EDD Settings.
 36  *
 37  * @since 1.3.3
 38  * @global $edd_options
 39  * @return bool $local_only
 40  */
 41 function edd_local_taxes_only() {
 42     global $edd_options;
 43 
 44     $local_only = isset( $edd_options['tax_condition'] ) && $edd_options['tax_condition'] == 'local';
 45 
 46     return apply_filters( 'edd_local_taxes_only', $local_only );
 47 }
 48 
 49 /**
 50  * Checks if a customer has opted into local taxes
 51  *
 52  * @since 1.4.1
 53  * @uses EDD_Session::get()
 54  * @return bool
 55  */
 56 function edd_local_tax_opted_in() {
 57     $opted_in = EDD()->session->get( 'edd_local_tax_opt_in' );
 58     return ! empty( $opted_in );
 59 }
 60 
 61 /**
 62  * Sets a customer as opted into local taxes
 63  *
 64  * @since 1.4.1
 65  * @uses EDD_Session::get()
 66  * @return bool
 67 */
 68 function edd_opt_into_local_taxes() {
 69     EDD()->session->set( 'edd_local_tax_opt_in', '1' );
 70 }
 71 
 72 /**
 73  * Sets a customer as opted out of local taxes
 74  *
 75  * @since 1.4.1
 76  * @uses EDD_Session::get()
 77  * @return bool
 78  */
 79 function edd_opt_out_local_taxes() {
 80     EDD()->session->set( 'edd_local_tax_opt_in', null);
 81 }
 82 
 83 /**
 84  * Show taxes on individual prices?
 85  *
 86  * @since 1.4
 87  * @global $edd_options
 88  * @return bool Whether or not to show taxes on prices
 89  */
 90 function edd_taxes_on_prices() {
 91     global $edd_options;
 92     return apply_filters( 'edd_taxes_on_prices', isset( $edd_options['taxes_on_prices'] ) );
 93 }
 94 
 95 /**
 96  * Checks if the user has enabled the option to calculate taxes after discounts
 97  * have been entered
 98  *
 99  * @since 1.4.1
100  * @global $edd_options
101  * @return bool Whether or not taxes are calculated after discount
102  */
103 function edd_taxes_after_discounts() {
104     global $edd_options;
105     return apply_filters( 'edd_taxes_after_discounts', isset( $edd_options['taxes_after_discounts'] ) );
106 }
107 
108 /**
109  * Get taxation rate
110  *
111  * @since 1.3.3
112  * @global $edd_options
113  * @return float $trate Taxation rate
114  */
115 function edd_get_tax_rate() {
116     global $edd_options;
117 
118     $rate = isset( $edd_options['tax_rate'] ) ? (float) $edd_options['tax_rate'] : 0;
119 
120     if( $rate > 1 ) {
121         // Convert to a number we can use
122         $rate = $rate / 100;
123     }
124     return apply_filters( 'edd_tax_rate', $rate );
125 }
126 
127 /**
128  * Calculate the taxed amount
129  *
130  * @since 1.3.3
131  * @param $amount float The original amount to calculate a tax cost
132  * @return float $tax Taxed amount
133  */
134 function edd_calculate_tax( $amount, $sum = true ) {
135     global $edd_options;
136 
137     // Not using taxes
138     if ( !edd_use_taxes() ) return $amount;
139 
140     $rate = edd_get_tax_rate();
141     $tax = 0;
142     $prices_include_tax = isset( $edd_options['prices_include_tax'] ) ? $edd_options['prices_include_tax'] : 'no';
143 
144     if ( $prices_include_tax == 'yes' ) {
145         $tax = $amount - ( $amount / ( $rate + 1 ) );
146     }
147 
148     if ( $prices_include_tax == 'no' ) {
149         $tax = $amount * $rate;
150     }
151 
152     if ( $sum ) {
153 
154         if ( $prices_include_tax == 'yes' ) {
155             $tax = $amount - $tax;
156         } else {
157             $tax = $amount + $tax;
158         }
159 
160     }
161 
162     $tax = round( $tax, 2 );
163     return apply_filters( 'edd_taxed_amount', $tax, $rate );
164 }
165 
166 /**
167  * Stores the tax info in the payment meta
168  *
169  * @since 1.3.3
170  * @param $year int The year to retrieve taxes for, i.e. 2012
171  * @uses edd_get_sales_tax_for_year()
172  * @return void
173 */
174 function edd_sales_tax_for_year( $year = null ) {
175     echo edd_currency_filter( edd_format_amount( edd_get_sales_tax_for_year( $year ) ) );
176 }
177 
178 /**
179  * Gets the sales tax for the current year
180  *
181  * @since 1.3.3
182  * @param $year int The year to retrieve taxes for, i.e. 2012
183  * @uses edd_get_payment_tax()
184  * @return float $tax Sales tax
185  */
186 function edd_get_sales_tax_for_year( $year = null ) {
187     if ( empty( $year ) )
188         return 0;
189 
190     // Start at zero
191     $tax = 0;
192 
193     $args = array(
194         'post_type'         => 'edd_payment',
195         'posts_per_page'    => -1,
196         'year'              => $year,
197         'meta_key'          => '_edd_payment_mode',
198         'meta_value'        => edd_is_test_mode() ? 'test' : 'live',
199         'fields'            => 'ids'
200     );
201 
202     $payments = get_posts( $args );
203 
204     if( $payments ) :
205 
206         foreach( $payments as $payment ) :
207             $tax += edd_get_payment_tax( $payment );
208         endforeach;
209 
210     endif;
211 
212     return apply_filters( 'edd_get_sales_tax_for_year', $tax, $year );
213 }
214 
215 
216 /**
217  * Checks whether the user has enabled display of taxes on the checkout
218  *
219  * @since 1.5
220  * @global $edd_options
221  * @return bool $include_tax
222  */
223 function edd_prices_show_tax_on_checkout() {
224     global $edd_options;
225 
226     $include_tax = isset( $edd_options['checkout_include_tax'] ) ? $edd_options['checkout_include_tax'] : 'no';
227 
228     return ( $include_tax == 'yes' );
229 }
230 
231 /**
232  * Check if the individual product prices include tax
233  *
234  * @since 1.5
235  * @global $edd_options
236  * @return bool $include_tax
237 */
238 function edd_prices_include_tax() {
239     global $edd_options;
240 
241     $include_tax = isset( $edd_options['prices_include_tax'] ) ? $edd_options['prices_include_tax'] : 'no';
242 
243     return ( $include_tax == 'yes' );
244 }
245 
246 /**
247  * Is the cart taxed?
248  *
249  * @since 1.5
250  * @return bool
251  */
252 function edd_is_cart_taxed() {
253     return edd_use_taxes() && ( ( edd_local_tax_opted_in() && edd_local_taxes_only() ) || ! edd_local_taxes_only() );
254 }
Easy Digital Downloads API documentation generated by ApiGen 2.8.0