Skip to content

Instantly share code, notes, and snippets.

@claygriffiths
Created June 28, 2020 03:18
Show Gist options
  • Save claygriffiths/700f3b59c4c6d72f4d6126bd0318cd4b to your computer and use it in GitHub Desktop.
Save claygriffiths/700f3b59c4c6d72f4d6126bd0318cd4b to your computer and use it in GitHub Desktop.
GP eCommerce Fields: Sync Tax Field Rate with WooCommerce Tax Rate
<?php
/**
* Note: See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install snippets.
*
* The following snippet will the specified tax field to use the rate from a specified WooCommerce tax class.
*
* Instructions:
* * Set $form_id
* * Set $tax_field_id
* * Set $wc_tax_rate_id
*/
$form_id = 17; /* Set me */
add_filter( 'gform_pre_render_' . $form_id, 'gwiz_link_tax_field_to_wc_tax' );
function gwiz_link_tax_field_to_wc_tax( $form ) {
$tax_field_id = 4; /* Set me */
$wc_tax_rate_id = 1; /* Set me */
$tax_instance = new WC_Tax;
$tax_rate = $tax_instance->_get_tax_rate( $wc_tax_rate_id );
foreach ( $form['fields'] as &$field ) {
if ( $field->type !== 'tax' || $field->id !== $tax_field_id ) {
continue;
}
$field->taxAmount = rgar( $tax_rate, 'tax_rate', $field->taxAmount );
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment