| 1 |
<?php namespace TierPricingTable\Addons\RoleBasedPricing; |
| 2 |
|
| 3 |
use TierPricingTable\Forms\Form; |
| 4 |
|
| 5 |
class RoleBasedPriceManager { |
| 6 |
|
| 7 |
public static function roleHasRules( string $role, int $productId, string $context = 'view' ): bool { |
| 8 |
|
| 9 |
$metadataToCheck = apply_filters( 'tiered_pricing_table/role_based_rules/rule_exists_meta', array( |
| 10 |
'_tiered_price_rules_type', |
| 11 |
'_tiered_price_pricing_type', |
| 12 |
), $role ); |
| 13 |
|
| 14 |
$productRoleRulesExists = false; |
| 15 |
|
| 16 |
foreach ( $metadataToCheck as $metaKey ) { |
| 17 |
if ( metadata_exists( 'post', $productId, "_{$role}{$metaKey}" ) ) { |
| 18 |
$productRoleRulesExists = true; |
| 19 |
|
| 20 |
break; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return $productRoleRulesExists; |
| 25 |
} |
| 26 |
|
| 27 |
public static function deleteAllDataForRole( $productId, $role ) { |
| 28 |
|
| 29 |
delete_post_meta( $productId, "_{$role}_tiered_price_regular_price" ); |
| 30 |
delete_post_meta( $productId, "_{$role}_tiered_price_sale_price" ); |
| 31 |
delete_post_meta( $productId, "_{$role}_tiered_price_discount" ); |
| 32 |
delete_post_meta( $productId, "_{$role}_tiered_price_discount_type" ); |
| 33 |
delete_post_meta( $productId, "_{$role}_tiered_price_pricing_type" ); |
| 34 |
|
| 35 |
delete_post_meta( $productId, "_{$role}_percentage_price_rules" ); |
| 36 |
delete_post_meta( $productId, "_{$role}_fixed_price_rules" ); |
| 37 |
delete_post_meta( $productId, "_{$role}_tiered_price_rules_type" ); |
| 38 |
|
| 39 |
delete_post_meta( $productId, "_{$role}_tiered_price_minimum_qty" ); |
| 40 |
|
| 41 |
delete_post_meta( $productId, "_{$role}_tiered_price_tax_status" ); |
| 42 |
delete_post_meta( $productId, "_{$role}_tiered_price_tax_class" ); |
| 43 |
|
| 44 |
do_action( 'tiered_pricing_table/role_based_rules/delete_role_rule', $productId, $role ); |
| 45 |
} |
| 46 |
|
| 47 |
public static function deleteAllRoleDataForProduct( int $productId ) { |
| 48 |
|
| 49 |
$roles = array_keys( wp_roles()->roles ); |
| 50 |
|
| 51 |
foreach ( $roles as $role ) { |
| 52 |
if ( self::roleHasRules( $role, $productId ) ) { |
| 53 |
self::deleteAllDataForRole( $productId, $role ); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Return empty array if rules do not exist. |
| 60 |
*/ |
| 61 |
public static function getFixedPriceRules( int $productId, string $role, string $context = 'view' ): array { |
| 62 |
return self::getPriceRules( $productId, $role, 'fixed', $context ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Return an empty array if rules do not exist. |
| 67 |
*/ |
| 68 |
public static function getPercentagePriceRules( int $productId, string $role, string $context = 'view' ): array { |
| 69 |
return self::getPriceRules( $productId, $role, 'percentage', $context ); |
| 70 |
} |
| 71 |
|
| 72 |
public static function getPriceRules( |
| 73 |
int $productId, |
| 74 |
string $role, |
| 75 |
?string $type = null, |
| 76 |
string $context = 'view' |
| 77 |
): array { |
| 78 |
|
| 79 |
$type = $type ? $type : self::getPricingType( $productId, $role, 'fixed', $context ); |
| 80 |
|
| 81 |
if ( 'fixed' === $type ) { |
| 82 |
$rules = (array) get_post_meta( $productId, "_{$role}_fixed_price_rules", true ); |
| 83 |
} else { |
| 84 |
$rules = (array) get_post_meta( $productId, "_{$role}_percentage_price_rules", true ); |
| 85 |
} |
| 86 |
|
| 87 |
$rules = ! empty( $rules ) ? array_filter( $rules ) : array(); |
| 88 |
ksort( $rules ); |
| 89 |
|
| 90 |
if ( 'edit' !== $context ) { |
| 91 |
|
| 92 |
$rules = apply_filters( 'tiered_pricing_table/role_based_rules/price/product_price_rules', $rules, |
| 93 |
$productId, $type ); |
| 94 |
} |
| 95 |
|
| 96 |
return $rules; |
| 97 |
} |
| 98 |
|
| 99 |
public static function getPricingType( |
| 100 |
int $productId, |
| 101 |
string $role, |
| 102 |
string $default = 'fixed', |
| 103 |
string $context = 'view' |
| 104 |
): string { |
| 105 |
|
| 106 |
$type = get_post_meta( $productId, "_{$role}_tiered_price_rules_type", true ); |
| 107 |
|
| 108 |
$type = in_array( $type, array( 'fixed', 'percentage' ) ) ? $type : $default; |
| 109 |
|
| 110 |
if ( 'edit' !== $context ) { |
| 111 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/type', $type, $role, $productId ); |
| 112 |
} |
| 113 |
|
| 114 |
return $type; |
| 115 |
} |
| 116 |
|
| 117 |
public static function getProductQtyMin( int $productId, string $role, string $context = 'view' ): ?int { |
| 118 |
|
| 119 |
$minimum = get_post_meta( $productId, "_{$role}_tiered_price_minimum_qty", true ); |
| 120 |
$minimum = ! Form::isEmpty( $minimum ) ? intval( $minimum ) : null; |
| 121 |
|
| 122 |
if ( 'view' === $context ) { |
| 123 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/minimum', $minimum, $role, $productId ); |
| 124 |
} |
| 125 |
|
| 126 |
return $minimum; |
| 127 |
} |
| 128 |
|
| 129 |
public static function getProductRegularRolePrice( |
| 130 |
int $productId, |
| 131 |
string $role, |
| 132 |
string $context = 'view' |
| 133 |
): ?float { |
| 134 |
|
| 135 |
$price = get_post_meta( $productId, "_{$role}_tiered_price_regular_price", true ); |
| 136 |
|
| 137 |
$price = ! Form::isEmpty( $price ) ? floatval( $price ) : null; |
| 138 |
|
| 139 |
if ( 'edit' !== $context ) { |
| 140 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/regular_price', $price, $role, |
| 141 |
$productId ); |
| 142 |
} |
| 143 |
|
| 144 |
return $price; |
| 145 |
} |
| 146 |
|
| 147 |
public static function getProductSaleRolePrice( int $productId, string $role, string $context = 'view' ): ?float { |
| 148 |
|
| 149 |
$price = get_post_meta( $productId, "_{$role}_tiered_price_sale_price", true ); |
| 150 |
|
| 151 |
$price = ! Form::isEmpty( $price ) ? floatval( $price ) : null; |
| 152 |
|
| 153 |
if ( 'edit' !== $context ) { |
| 154 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/sale_price', $price, $role, $productId ); |
| 155 |
} |
| 156 |
|
| 157 |
return $price; |
| 158 |
} |
| 159 |
|
| 160 |
public static function getProductDiscount( int $productId, string $role, string $context = 'view' ): ?float { |
| 161 |
$discount = get_post_meta( $productId, "_{$role}_tiered_price_discount", true ); |
| 162 |
|
| 163 |
$discount = ! Form::isEmpty( $discount ) ? floatval( $discount ) : null; |
| 164 |
|
| 165 |
if ( 'edit' !== $context ) { |
| 166 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/discount', $discount, $role, |
| 167 |
$productId ); |
| 168 |
} |
| 169 |
|
| 170 |
return $discount; |
| 171 |
} |
| 172 |
|
| 173 |
public static function getProductDiscountType( int $productId, string $role, string $context = 'view' ): string { |
| 174 |
$discountType = get_post_meta( $productId, "_{$role}_tiered_price_discount_type", true ); |
| 175 |
|
| 176 |
if ( 'edit' !== $context ) { |
| 177 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/discount_type', $discountType, $role, |
| 178 |
$productId ); |
| 179 |
} |
| 180 |
|
| 181 |
return in_array( $discountType, array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price'; |
| 182 |
} |
| 183 |
|
| 184 |
public static function getProductPricingType( int $productId, string $role, string $context = 'view' ): string { |
| 185 |
$pricingType = get_post_meta( $productId, "_{$role}_tiered_price_pricing_type", true ); |
| 186 |
|
| 187 |
$pricingType = in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat'; |
| 188 |
|
| 189 |
if ( 'edit' !== $context ) { |
| 190 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/pricing_type', $pricingType, $role, |
| 191 |
$productId ); |
| 192 |
} |
| 193 |
|
| 194 |
return $pricingType; |
| 195 |
} |
| 196 |
|
| 197 |
public static function getProductTaxStatus( int $productId, string $role, string $context = 'view' ): string { |
| 198 |
$taxStatus = get_post_meta( $productId, "_{$role}_tiered_price_tax_status", true ); |
| 199 |
|
| 200 |
if ( 'edit' !== $context ) { |
| 201 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/tax_status', $taxStatus, $role, $productId ); |
| 202 |
} |
| 203 |
|
| 204 |
return $taxStatus; |
| 205 |
} |
| 206 |
|
| 207 |
public static function getProductTaxClass( int $productId, string $role, string $context = 'view' ): string { |
| 208 |
$taxClass = get_post_meta( $productId, "_{$role}_tiered_price_tax_class", true ); |
| 209 |
|
| 210 |
if ( 'edit' !== $context ) { |
| 211 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/tax_class', $taxClass, $role, $productId ); |
| 212 |
} |
| 213 |
|
| 214 |
return $taxClass; |
| 215 |
} |
| 216 |
} |
| 217 |
|