tier-pricing-table
/
src
/
Addons
/
TaxSettings
/
RoleBasedRulesIntegration
/
RoleBasedRulesIntegration.php
RoleBasedRulesIntegration.php in Tiered Pricing Table for WooCommerce 7.1.4, at src/Addons/TaxSettings/RoleBasedRulesIntegration/RoleBasedRulesIntegration.php
| 1 | <?php namespace TierPricingTable\Addons\TaxSettings\RoleBasedRulesIntegration; |
| 2 | |
| 3 | use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPriceManager; |
| 4 | use TierPricingTable\Forms\Form; |
| 5 | |
| 6 | class RoleBasedRulesIntegration { |
| 7 | |
| 8 | public function __construct() { |
| 9 | // Render UI |
| 10 | add_action( 'tiered_pricing_table/admin/role_based_rules/after_tiered_pricing_rules_field', |
| 11 | array( $this, 'renderTaxFields' ), 10, 3 ); |
| 12 | |
| 13 | // Save Data |
| 14 | add_action( 'tiered_pricing_table/role_based_rules/save_role_based_rules', array( $this, 'saveTaxFields' ), 10, |
| 15 | 4 ); |
| 16 | } |
| 17 | |
| 18 | public function renderTaxFields( $productId, $role, $loop ) { |
| 19 | |
| 20 | $taxStatus = RoleBasedPriceManager::getProductTaxStatus( $productId, $role ); |
| 21 | $taxClass = RoleBasedPriceManager::getProductTaxClass( $productId, $role ); |
| 22 | |
| 23 | $taxStatusId = Form::getFieldName( 'tax_status', $role, $loop ); |
| 24 | $taxClassId = Form::getFieldName( 'tax_class', $role, $loop ); |
| 25 | |
| 26 | $taxStatusOptions = array( |
| 27 | '' => __( 'Default', 'tier-pricing-table' ), |
| 28 | 'taxable' => __( 'Taxable', 'woocommerce' ), |
| 29 | 'shipping' => __( 'Shipping only', 'woocommerce' ), |
| 30 | 'none' => _x( 'None', 'Tax status', 'woocommerce' ), |
| 31 | ); |
| 32 | |
| 33 | $taxClassOptions = array( |
| 34 | '' => __( 'Default', 'tier-pricing-table' ), |
| 35 | 'standard' => __( 'Standard', 'woocommerce' ), |
| 36 | ); |
| 37 | |
| 38 | if ( function_exists( 'wc_get_product_tax_class_options' ) ) { |
| 39 | $classes = wc_get_product_tax_class_options(); |
| 40 | foreach ( $classes as $value => $label ) { |
| 41 | $taxClassOptions[ $value === '' ? 'standard' : $value ] = $label; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | ?> |
| 46 | <hr style="border-color: #f5f5f5;border-top: none;"> |
| 47 | <p class="form-field"> |
| 48 | <label for="<?php echo esc_attr( $taxStatusId ); ?>"> |
| 49 | <?php esc_html_e( 'Tax status', 'tier-pricing-table' ); ?> |
| 50 | </label> |
| 51 | <select name="<?php echo esc_attr( $taxStatusId ); ?>" id="<?php echo esc_attr( $taxStatusId ); ?>"> |
| 52 | <?php foreach ( $taxStatusOptions as $optionValue => $optionLabel ) : ?> |
| 53 | <option value="<?php echo esc_attr( $optionValue ); ?>" <?php selected( $taxStatus, |
| 54 | $optionValue ); ?>> |
| 55 | <?php echo esc_html( $optionLabel ); ?> |
| 56 | </option> |
| 57 | <?php endforeach; ?> |
| 58 | </select> |
| 59 | </p> |
| 60 | |
| 61 | <p class="form-field"> |
| 62 | <label for="<?php echo esc_attr( $taxClassId ); ?>"> |
| 63 | <?php esc_html_e( 'Tax class', 'tier-pricing-table' ); ?> |
| 64 | </label> |
| 65 | <select name="<?php echo esc_attr( $taxClassId ); ?>" id="<?php echo esc_attr( $taxClassId ); ?>"> |
| 66 | <?php foreach ( $taxClassOptions as $optionValue => $optionLabel ) : ?> |
| 67 | <option value="<?php echo esc_attr( $optionValue ); ?>" <?php selected( $taxClass, |
| 68 | $optionValue ); ?>> |
| 69 | <?php echo esc_html( $optionLabel ); ?> |
| 70 | </option> |
| 71 | <?php endforeach; ?> |
| 72 | </select> |
| 73 | </p> |
| 74 | <?php |
| 75 | } |
| 76 | |
| 77 | public function saveTaxFields( $productId, $data, $role, $loop ) { |
| 78 | $taxStatus = sanitize_text_field( Form::getFieldValue( 'tax_status', $role, $loop, '', $data ) ?? '' ); |
| 79 | $taxClass = sanitize_text_field( Form::getFieldValue( 'tax_class', $role, $loop, '', $data ) ?? '' ); |
| 80 | |
| 81 | update_post_meta( $productId, "_{$role}_tiered_price_tax_status", $taxStatus ); |
| 82 | update_post_meta( $productId, "_{$role}_tiered_price_tax_class", $taxClass ); |
| 83 | } |
| 84 | } |
| 85 |