Skip to content

Instantly share code, notes, and snippets.

@itsariful
Last active September 25, 2019 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itsariful/b0ead8763e8f7d34564c3a0cf3a4344b to your computer and use it in GitHub Desktop.
Save itsariful/b0ead8763e8f7d34564c3a0cf3a4344b to your computer and use it in GitHub Desktop.
MS Custom Tax Addon
<?php
/**
* Plugin Name: MS Custom Tax Addon
* Description: This plugin implements a custom addon for MS to implement tax regardless of taxamo
* Author: Ariful Islam @ WPMUDEV
* Author URI: https://premium.wpmudev.org/profile/itsarifulislam
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'Render_MS_Addon_Custom' ) ) {
function Render_MS_Addon_Custom() {
if ( ! class_exists('MS_Addon') ) return;
class MS_Addon_Custom extends MS_Addon {
const ID = 'addon_custom';
const AJAX_SAVE_SETTING = 'membership_set_custom_field';
public function get_id() {
return self::ID;
}
static public function is_active() {
return MS_Model_Addon::is_enabled( self::ID );
}
public function init() {
if ( ! self::is_active() ) return;
add_filter( 'ms_view_membership_tab_payment_fields', array( $this, 'tax_custom_fields' ), 10, 1 );
add_action( 'ms_view_membership_tab_payment_form', array( $this, 'tax_custom_html' ), 10, 2 );
add_action( 'admin_footer', array( $this, 'add_scripts' ) );
add_filter( 'ms_apply_taxes', array( $this, 'apply_taxes' ), 10, 2 );
add_filter( 'ms_invoice_tax_rate', array( $this, 'invoice_tax_rate' ), 10, 2 );
add_filter( 'ms_invoice_tax_name', array( $this, 'invoice_tax_name' ), 10, 2 );
add_filter( 'ms_model_addon_is_enabled_addon_taxamo', array( $this, 'ms_model_addon_is_enabled_addon_taxamo' ), 10, 2 );
}
public function ms_model_addon_is_enabled_addon_taxamo( $status ) {
if ( self::is_active() ) return true;
return $status;
}
public function invoice_tax_rate( $rate, $invoice ) {
$membership = $invoice->get_membership();
if ( ! $this->membership_tax_enabled( $membership ) ) return $rate;
return $this->get_custom_data( $membership, 'custom_tax_rate', 0 );
}
public function invoice_tax_name( $rate, $invoice ) {
$membership = $invoice->get_membership();
if ( ! $this->membership_tax_enabled( $membership ) ) return $rate . ' %';
$tax_rate = $this->get_custom_data( $membership, 'custom_tax_rate', 0 );
return $tax_rate . ' %';
}
public function apply_taxes( $net_value, $membership ) {
if ( ! $this->membership_tax_enabled( $membership ) ) return $net_value;
$gross_value = 0;
if ( is_numeric( $net_value ) ) {
$tax_rate = (int) $this->get_custom_data( $membership, 'custom_tax_rate', 0 );
$gross_value = $net_value + $this->get_tax_amount( $tax_rate, $net_value );
}
return $gross_value;
}
public function get_tax_amount( $tax_rate, $net_value ) {
return $net_value * $tax_rate / 100;
}
public function add_scripts() {
?>
<script>
(function($){
function toggleCustomTaxTypeBlock() {
var $me = $('#custom_tax_enabled'),
block = $me.closest('.ms-custom-tax').find('.ms-custom-tax_rate-wrapper'),
custom_tax_enabled = ( $me.val() == 'true' || $me.val() == '1' ) ? true : false,
method = custom_tax_enabled ? 'show' : 'hide';
block[method]();
}
toggleCustomTaxTypeBlock();
jQuery(document).on( 'ms-ajax-updated', toggleCustomTaxTypeBlock );
})(jQuery);
</script>
<?php
}
public function membership_tax_enabled( $membership ) {
return $membership->get_custom_data( 'custom_tax_enabled' ) == 'true';
}
public function tax_custom_fields( $fields ) {
$membership_id = $fields['membership_id']['value'];
if ( ! $membership_id ) {
return $fields;
}
$membership = MS_Factory::load(
'MS_Model_Membership',
$membership_id
);
$fields = array_merge( $fields, $this->get_fields($membership) );
return $fields;
}
public function get_fields( $membership ) {
$action = MS_Addon_Custom::AJAX_SAVE_SETTING;
$custom_tax_rate = $this->get_custom_data( $membership, 'custom_tax_rate', 0 );
$custom_tax_enabled = $this->membership_tax_enabled( $membership );
return array(
'custom_tax_enabled' => array(
'id' => 'custom_tax_enabled',
'type' => 'radio_slider',
'title' => '<strong>' . __( 'Custom Tax', 'membership2' ) . '</strong>',
'after' => __( 'Apply your custom tax', 'membership2' ),
'value' => $custom_tax_enabled,
'ajax_data' => array(
'field' => 'custom_tax_enabled',
'action' => $action,
'membership_id' => $membership->id
),
),
'custom_tax_rate' => array(
'id' => 'custom_tax_rate',
'title' => __( 'Tax Rate', 'membership2' ),
'type' => MS_Helper_Html::INPUT_TYPE_NUMBER,
'value' => $custom_tax_rate,
'class' => 'ms-text-small',
'after' => __( '%', 'membership2' ),
'config' => array(
'step' => 1,
'min' => 0,
),
'ajax_data' => array(
'field' => 'custom_tax_rate',
'action' => $action,
'membership_id' => $membership->id
),
)
);
}
public function get_custom_data( $membership, $field_name, $default = null ) {
$value = $membership->get_custom_data( $field_name );
if ( empty($value) && $default !== null ) return $default;
return $value;
}
public function tax_custom_html( $class, $membership ) {
$fields = $this->get_fields($membership);
$custom_tax_enabled = $this->membership_tax_enabled( $membership );
?>
<div class="cf ms-custom-tax">
<?php MS_Helper_Html::html_separator(); ?>
<div class="ms-custom-tax-wrapper ms-custom-tax_enabled-wrapper">
<?php
MS_Helper_Html::html_element( $fields['custom_tax_enabled'] );
?>
</div>
<div class="ms-custom-tax-wrapper ms-custom-tax_rate-wrapper" style="<?php echo $custom_tax_enabled ? '' : 'display: none;' ?>">
<?php
MS_Helper_Html::html_element( $fields['custom_tax_rate'] );
?>
</div>
</div>
<?php
}
public function register( $list ) {
$list[ self::ID ] = (object) array(
'name' => __( 'Tax Custom', 'membership2' ),
'description' => __( 'Implement your custom tax', 'membership2' ),
'icon' => 'wpmui-fa wpmui-fa-dollar'
);
return $list;
}
}
}
}
if ( ! function_exists( 'Register_MS_Addon_Custom' ) ) {
function Register_MS_Addon_Custom() {
if ( ! class_exists( 'MS_Addon_Custom' ) ) return;
MS_Factory::load( 'MS_Addon_Custom' );
}
}
// Load the MS_Addon_Custom addon class
add_action( 'plugins_loaded', 'Render_MS_Addon_Custom' );
// Register the MS_Addon_Custom addon
add_action( 'ms_model_addon_load', 'Register_MS_Addon_Custom' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment