| 1 |
<?php namespace TierPricingTable\Addons\TaxSettings\Overrides; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\TaxSettings\API\TaxSettingsEndpoints; |
| 4 |
|
| 5 |
class RoleBasedTaxOptions { |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
// Hook into options early |
| 9 |
add_filter( 'option_woocommerce_tax_display_shop', array( $this, 'overrideDisplayShop' ), 99 ); |
| 10 |
add_filter( 'option_woocommerce_tax_display_cart', array( $this, 'overrideDisplayCart' ), 99 ); |
| 11 |
add_filter( 'option_woocommerce_price_display_suffix', array( $this, 'overridePriceSuffix' ), 99 ); |
| 12 |
add_filter( 'option_woocommerce_prices_include_tax', array( $this, 'overridePricesIncludeTax' ), 99 ); |
| 13 |
|
| 14 |
// Tax class |
| 15 |
add_filter( 'woocommerce_product_get_tax_class', array( $this, 'overrideTaxClass' ), 99, 2 ); |
| 16 |
add_filter( 'woocommerce_product_variation_get_tax_class', array( $this, 'overrideTaxClass' ), 99, 2 ); |
| 17 |
|
| 18 |
// Exempt |
| 19 |
add_action( 'wp', array( $this, 'setTaxExempt' ), 99 ); |
| 20 |
} |
| 21 |
|
| 22 |
protected function getActiveRoleSettings() { |
| 23 |
if ( is_admin() && ! wp_doing_ajax() ) { |
| 24 |
return null; |
| 25 |
} |
| 26 |
|
| 27 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
$settings = get_option( TaxSettingsEndpoints::OPTION_KEY, array() ); |
| 32 |
|
| 33 |
if ( empty( $settings ) ) { |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
$roles = \TierPricingTable\TierPricingTablePlugin::getCurrentUserRoles(); |
| 38 |
|
| 39 |
if ( empty( $roles ) ) { |
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
// Check roles |
| 44 |
foreach ( $roles as $role ) { |
| 45 |
if ( isset( $settings[ $role ] ) ) { |
| 46 |
return $settings[ $role ]; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
public function overrideDisplayShop( $value ) { |
| 54 |
$roleSettings = $this->getActiveRoleSettings(); |
| 55 |
if ( $roleSettings && ! empty( $roleSettings['display_shop'] ) && 'default' !== $roleSettings['display_shop'] ) { |
| 56 |
return $roleSettings['display_shop']; // 'incl' or 'excl' |
| 57 |
} |
| 58 |
|
| 59 |
return $value; |
| 60 |
} |
| 61 |
|
| 62 |
public function overrideDisplayCart( $value ) { |
| 63 |
$roleSettings = $this->getActiveRoleSettings(); |
| 64 |
if ( $roleSettings && ! empty( $roleSettings['display_cart'] ) && 'default' !== $roleSettings['display_cart'] ) { |
| 65 |
return $roleSettings['display_cart']; // 'incl' or 'excl' |
| 66 |
} |
| 67 |
|
| 68 |
return $value; |
| 69 |
} |
| 70 |
|
| 71 |
public function overridePriceSuffix( $value ) { |
| 72 |
$roleSettings = $this->getActiveRoleSettings(); |
| 73 |
if ( $roleSettings && isset( $roleSettings['price_suffix'] ) && '' !== $roleSettings['price_suffix'] && 'default' !== $roleSettings['price_suffix'] ) { |
| 74 |
return $roleSettings['price_suffix']; |
| 75 |
} |
| 76 |
|
| 77 |
return $value; |
| 78 |
} |
| 79 |
|
| 80 |
public function overridePricesIncludeTax( $value ) { |
| 81 |
$roleSettings = $this->getActiveRoleSettings(); |
| 82 |
if ( $roleSettings && ! empty( $roleSettings['prices_include_tax'] ) && 'default' !== $roleSettings['prices_include_tax'] ) { |
| 83 |
return $roleSettings['prices_include_tax']; // 'yes' or 'no' |
| 84 |
} |
| 85 |
|
| 86 |
return $value; |
| 87 |
} |
| 88 |
|
| 89 |
public function overrideTaxClass( $taxClass, $product ) { |
| 90 |
|
| 91 |
$roleSettings = $this->getActiveRoleSettings(); |
| 92 |
if ( $roleSettings && ! empty( $roleSettings['tax_class'] ) && 'default' !== $roleSettings['tax_class'] ) { |
| 93 |
return 'standard' === $roleSettings['tax_class'] ? '' : $roleSettings['tax_class']; |
| 94 |
} |
| 95 |
|
| 96 |
return $taxClass; |
| 97 |
} |
| 98 |
|
| 99 |
public function setTaxExempt() { |
| 100 |
if ( ! function_exists( 'WC' ) || ! isset( WC()->customer ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$roleSettings = $this->getActiveRoleSettings(); |
| 105 |
|
| 106 |
if ( $roleSettings && isset( $roleSettings['tax_exempt'] ) && $roleSettings['tax_exempt'] ) { |
| 107 |
WC()->customer->set_is_vat_exempt( true ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|