Last active
May 6, 2019 13:39
-
-
Save lucasstark/0d191cedd87646c1051eb91aede8d9df to your computer and use it in GitHub Desktop.
Include Tax Display for Gravity Forms when WooCommerce is set to enter prices exclusive of tax, but show inclusive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_add_cart_item_data', 'remove_adjust_for_tax_display', 10, 1 ); | |
add_filter( 'woocommerce_get_cart_item_from_session', 'remove_adjust_for_tax_display', 10, 1 ); | |
add_filter( 'woocommerce_get_item_data', 'remove_adjust_for_tax_display', 10, 1 ); | |
add_filter( 'woocommerce_add_cart_item', 'remove_adjust_for_tax_display', 10, 1 ); | |
add_action( 'woocommerce_checkout_create_order_line_item', 'remove_adjust_for_tax_display', 10, 1 ); | |
add_filter( 'woocommerce_add_to_cart_validation', 'remove_adjust_for_tax_display', 99, 1 ); | |
function remove_adjust_for_tax_display( $arg ) { | |
if ( isset( $_POST['gform_old_submit'] ) ) { | |
remove_filter( 'gform_pre_render', 'adjust_for_tax_display' ); | |
} | |
return $arg; | |
} | |
add_filter( 'gform_pre_render', 'adjust_for_tax_display' ); | |
function adjust_for_tax_display( $form ) { | |
if ( ! isset( $_POST['gform_old_submit'] ) ) { | |
$woocommerce_product = wc_get_product(); | |
if ( empty( $woocommerce_product ) ) { | |
return $form; | |
} | |
foreach ( $form['fields'] as &$field ) { | |
switch ( $field->type ) { | |
case "product" : | |
if ( $field->inputType == 'hiddenproduct' ) { | |
$price = GFCommon::to_number( $field->basePrice ); | |
$display_price = wc_gforms_product_addons_format_price( wc_gforms_product_addons_get_price_to_display( $woocommerce_product, $price ) ); | |
$field->basePrice = $display_price; | |
} elseif ( $field->inputType == 'select' || $field->inputType == 'radio' ) { | |
foreach ( $field->choices as $index => $choice ) { | |
$price = GFCommon::to_number( $field->choices[ $index ]['price'] ); | |
$display_price = wc_gforms_product_addons_get_price_to_display( $woocommerce_product, $price ); | |
$field->choices[ $index ]['price'] = $display_price; | |
} | |
} | |
break; | |
case 'option': | |
foreach ( $field->choices as $index => $choice ) { | |
$price = GFCommon::to_number( $field->choices[ $index ]['price'] ); | |
$display_price = wc_gforms_product_addons_get_price_to_display( $woocommerce_product, $price ); | |
$field->choices[ $index ]['price'] = $display_price; | |
} | |
break; | |
default : | |
break; | |
} | |
} | |
} | |
return $form; | |
} | |
add_filter( 'woocommerce_gforms_get_cart_item_total', function ( $total, $cart_item ) { | |
return wc_gforms_remove_tax($total, $cart_item); | |
}, 10, 2 ); | |
function wc_gforms_product_addons_get_price_to_display( $woocommerce_product, $price ) { | |
return 'incl' === get_option( 'woocommerce_tax_display_shop' ) ? | |
wc_get_price_including_tax( | |
$woocommerce_product, array( | |
'qty' => 1, | |
'price' => $price, | |
) | |
) : | |
wc_get_price_excluding_tax( | |
$woocommerce_product, array( | |
'qty' => 1, | |
'price' => $price, | |
) | |
); | |
} | |
function wc_gforms_product_addons_format_price( $price ) { | |
$args = array( | |
'ex_tax_label' => false, | |
'currency' => '', | |
'decimal_separator' => wc_get_price_decimal_separator(), | |
'thousand_separator' => wc_get_price_thousand_separator(), | |
'decimals' => wc_get_price_decimals(), | |
'price_format' => get_woocommerce_price_format(), | |
); | |
$negative = $price < 0; | |
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * - 1 : $price ) ); | |
$price = apply_filters( 'formatted_woocommerce_price', number_format( $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ), $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ); | |
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $args['price_format'], get_woocommerce_currency_symbol( $args['currency'] ), $price ); | |
$formatted_price = html_entity_decode( $formatted_price ); | |
return $formatted_price; | |
} | |
function wc_gforms_remove_tax( $price, $cart_item ) { | |
$product = $cart_item['data']; | |
$args = array( | |
'qty' => 1, | |
'price' => $price, | |
); | |
$price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $product->get_price(); | |
$qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1; | |
if ( '' === $price ) { | |
return ''; | |
} elseif ( empty( $qty ) ) { | |
return 0.0; | |
} | |
$line_price = $price * $qty; | |
if ( $product->is_taxable() ){ | |
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); | |
$base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); | |
$remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true ); | |
$return_price = $line_price - array_sum( $remove_taxes ); | |
}else { | |
$return_price = $line_price; | |
} | |
return $return_price; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment