Skip to content

Instantly share code, notes, and snippets.

@jacobdubail
Created January 11, 2018 14:14
Show Gist options
  • Save jacobdubail/a8e50e70ed4c75627d10ccd2e2c95d06 to your computer and use it in GitHub Desktop.
Save jacobdubail/a8e50e70ed4c75627d10ccd2e2c95d06 to your computer and use it in GitHub Desktop.
FoxyShop VAT Pricing
<?php
add_action('admin_menu', 'jtd_vat_settings_menu');
function jtd_vat_settings_menu() {
add_submenu_page('edit.php?post_type=foxyshop_product', __('VAT Display', 'foxyshop'), __('VAT Display', 'foxyshop'), apply_filters('foxyshop_settings_perm', 'manage_options'), 'foxyshop_vat_display', 'foxyshop_vat_display');
}
function foxyshop_vat_display() {
global $foxyshop_settings, $foxycart_version_array;
if (!defined('FOXYSHOP_TEMPLATE_PATH')) define('FOXYSHOP_TEMPLATE_PATH',STYLESHEETPATH);
if (isset($_GET['saved'])) echo '<div class="updated"><p>' . __('Your Settings Have Been Saved.', 'foxyshop') . '</p></div>';
?>
<div id="foxyshop_settings_wrap" class="wrap">
<div class="icon32" id="icon-options-general"><br></div>
<h2>FoxyShop VAT Settings</h2>
<form method="post" name="foxycart_vat_settings_form" action="options.php" style="margin-top: 14px;">
<table class="widefat">
<thead>
<tr>
<th><?php _e('FoxyCart VAT Display Settings', 'foxyshop'); ?></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label for="foxyshop_display_vat"><?php echo __('Enable FoxyCart VAT Display', 'foxyshop'); ?></label>
<select name="foxyshop_display_vat" id="foxyshop_display_vat">
<?php
$vat_options = array(
'on' => 'Enable FoxyCart VAT Display',
'off' => 'Disable FoxyCart VAT Display',
'both' => 'Show price with and without FoxyCart VAT'
);
foreach ( $vat_options as $key => $val) {
echo '<option value="' . $key . '"' . ($foxyshop_settings['display_vat'] == $key ? ' selected="selected"' : '') . '>' . $val . ' </option>'."\n";
} ?>
</select>
</td>
</tr>
<tr>
<td>
<label for="foxyshop_vat_rate"><?php echo __('VAT Rate', 'foxyshop'); ?></label>
<input type="text" name="foxyshop_vat_rate" id="foxyshop_vat_rate" value="<?php echo esc_attr($foxyshop_settings['vat_rate']); ?>" />
<a href="#" class="foxyshophelp">Please enter a decimal. e.g. 0.2 = 20%, or .05 = 5%</a>
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="foxyshop_vat_settings_update" value="1" />
<?php wp_nonce_field('update-foxyshop-vat-options'); ?>
<p><input type="submit" class="button-primary" value="<?php _e('Save All Settings', 'foxyshop'); ?>" /></p>
</form>
<script>
jQuery(document).ready(function($) {
//Tooltip
xOffset = -10;
yOffset = 10;
$("a.foxyshophelp").hover(function(e) {
var tooltip_text = $(this).html();
$("body").append("<p id='tooltip'>"+ tooltip_text +"</p>");
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
}, function(){
$("#tooltip").remove();
}).mousemove(function(e){
$("#tooltip")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
}).click(function(e) {
e.preventDefault();
return false;
}).attr("tabindex", "99999");
});
</script>
</div>
<?php
}
add_action('admin_init', 'jtd_save_vat_display_settings');
function jtd_save_vat_display_settings() {
if (!isset($_POST['foxyshop_vat_settings_update'])) return;
if (!check_admin_referer('update-foxyshop-vat-options')) return;
global $foxyshop_settings;
if ( in_array( $_POST['foxyshop_display_vat'], array('on', 'off', 'both') ) ) {
$foxyshop_settings['display_vat'] = $_POST['foxyshop_display_vat'];
}
if ( is_numeric( $_POST['foxyshop_vat_rate'] ) ) {
$foxyshop_settings['vat_rate'] = $_POST['foxyshop_vat_rate'];
}
update_option("foxyshop_settings", $foxyshop_settings);
wp_redirect("edit.php?post_type=foxyshop_product&page=foxyshop_vat_display&saved=1");
exit;
}
function jtd_price($skip_sale_price = false, $echo_output = true) {
global $product, $foxyshop_settings;
$vat_display = $foxyshop_settings['display_vat'];
$vat_rate = $foxyshop_settings['vat_rate'];
$no_vat_price = number_format((double)get_post_meta($product['id'],'_price', 1),FOXYSHOP_DECIMAL_PLACES,".","");
$no_vat_sale_price = number_format((double)get_post_meta($product['id'],'_saleprice', 1),FOXYSHOP_DECIMAL_PLACES,".","");
if ( $vat_display == 'on' || $vat_display == 'both' ) {
$vat_price = ( $no_vat_price * $vat_rate ) + $no_vat_price;
$vat_sale_price = ( $no_vat_sale_price * $vat_rate ) + $no_vat_sale_price;
}
$write = '<div class="foxyshop_price">';
if ($product['price'] == $product['originalprice']) {
if ( $vat_display == 'on' || $vat_display == 'both' ) {
$write .= '<span class="">' . foxyshop_currency($vat_price) . '</span>';
$write .= ' <small style="font-size:11px;color:grey;">price includes VAT</small>';
} else {
$write .= '<span class="">' . foxyshop_currency($no_vat_price) . '</span>';
}
if ( $vat_display == 'both' ) {
$write .= '<br><span class="foxyshop_currentprice_no_vat">' . foxyshop_currency($no_vat_price) . '</span>';
$write .= ' <small style="font-size:11px;color:grey;">price excluding VAT</small>';
}
} else {
if (!$skip_sale_price) {
if ( $vat_display == 'on' || $vat_display == 'both' ) {
$write .= '<del class="old1">' . foxyshop_currency($vat_price) . '</del>';
$write .= ' <small style="font-size:11px;color:grey;">price includes VAT</small><br>';
} else {
$write .= '<del class="old1">' . foxyshop_currency($no_vat_price) . '</del>';
}
if ( $vat_display == 'both' ) {
$write .= '<del class="foxyshop_oldprice_no_vat old2">' . foxyshop_currency($no_vat_sale_price) . '</del>';
$write .= ' <small style="font-size:11px;color:grey;">price excluding VAT</small><br>';
}
}
if ( $vat_display == 'on' || $vat_display == 'both' ) {
$write .= '<span class=" foxyshop_saleprice old3">' . foxyshop_currency($vat_sale_price) . '</span>';
$write .= ' <small style="font-size:11px;color:grey;">price includes VAT</small>';
} else {
$write .= '<span class=" foxyshop_saleprice old3">' . foxyshop_currency($no_vat_sale_price) . '</span>';
}
if ( $vat_display == 'both' ) {
$write .= '<br><span class="foxyshop_currentprice_no_vat foxyshop_saleprice old4">' . foxyshop_currency($no_vat_sale_price) . '</span>';
$write .= ' <small style="font-size:11px;color:grey;">price excluding VAT</small>';
}
}
$write .= '</div>';
if ($echo_output) {
echo $write;
} else {
return $write;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment