| 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 |
do_action( 'tiered_pricing_table/role_based_rules/delete_role_rule', $productId, $role ); |
| 42 |
} |
| 43 |
|
| 44 |
public static function deleteAllRoleDataForProduct( int $productId ) { |
| 45 |
|
| 46 |
$roles = array_keys( wp_roles()->roles ); |
| 47 |
|
| 48 |
foreach ( $roles as $role ) { |
| 49 |
if ( self::roleHasRules( $role, $productId ) ) { |
| 50 |
self::deleteAllDataForRole( $productId, $role ); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Return empty array if rules do not exist. |
| 57 |
*/ |
| 58 |
public static function getFixedPriceRules( int $productId, string $role, string $context = 'view' ): array { |
| 59 |
return self::getPriceRules( $productId, $role, 'fixed', $context ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Return empty array if rules do not exist. |
| 64 |
*/ |
| 65 |
public static function getPercentagePriceRules( int $productId, string $role, string $context = 'view' ): array { |
| 66 |
return self::getPriceRules( $productId, $role, 'percentage', $context ); |
| 67 |
} |
| 68 |
|
| 69 |
public static function getPriceRules( |
| 70 |
int $productId, |
| 71 |
string $role, |
| 72 |
?string $type = null, |
| 73 |
string $context = 'view' |
| 74 |
): array { |
| 75 |
|
| 76 |
$type = $type ? $type : self::getPricingType( $productId, $role, 'fixed', $context ); |
| 77 |
|
| 78 |
if ( 'fixed' === $type ) { |
| 79 |
$rules = (array) get_post_meta( $productId, "_{$role}_fixed_price_rules", true ); |
| 80 |
} else { |
| 81 |
$rules = (array) get_post_meta( $productId, "_{$role}_percentage_price_rules", true ); |
| 82 |
} |
| 83 |
|
| 84 |
$rules = ! empty( $rules ) ? array_filter( $rules ) : array(); |
| 85 |
ksort( $rules ); |
| 86 |
|
| 87 |
if ( 'edit' !== $context ) { |
| 88 |
|
| 89 |
$rules = apply_filters( 'tiered_pricing_table/role_based_rules/price/product_price_rules', $rules, |
| 90 |
$productId, $type ); |
| 91 |
} |
| 92 |
|
| 93 |
return $rules; |
| 94 |
} |
| 95 |
|
| 96 |
public static function getPricingType( |
| 97 |
int $productId, |
| 98 |
string $role, |
| 99 |
string $default = 'fixed', |
| 100 |
string $context = 'view' |
| 101 |
): string { |
| 102 |
|
| 103 |
$type = get_post_meta( $productId, "_{$role}_tiered_price_rules_type", true ); |
| 104 |
|
| 105 |
$type = in_array( $type, array( 'fixed', 'percentage' ) ) ? $type : $default; |
| 106 |
|
| 107 |
if ( 'edit' !== $context ) { |
| 108 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/type', $type, $role, $productId ); |
| 109 |
} |
| 110 |
|
| 111 |
return $type; |
| 112 |
} |
| 113 |
|
| 114 |
public static function getProductQtyMin( int $productId, string $role, string $context = 'view' ): ?int { |
| 115 |
|
| 116 |
$minimum = get_post_meta( $productId, "_{$role}_tiered_price_minimum_qty", true ); |
| 117 |
$minimum = ! Form::isEmpty( $minimum ) ? intval( $minimum ) : null; |
| 118 |
|
| 119 |
if ( 'view' === $context ) { |
| 120 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/minimum', $minimum, $role, $productId ); |
| 121 |
} |
| 122 |
|
| 123 |
return $minimum; |
| 124 |
} |
| 125 |
|
| 126 |
public static function getProductRegularRolePrice( |
| 127 |
int $productId, |
| 128 |
string $role, |
| 129 |
string $context = 'view' |
| 130 |
): ?float { |
| 131 |
|
| 132 |
$price = get_post_meta( $productId, "_{$role}_tiered_price_regular_price", true ); |
| 133 |
|
| 134 |
$price = ! Form::isEmpty( $price ) ? floatval( $price ) : null; |
| 135 |
|
| 136 |
if ( 'edit' !== $context ) { |
| 137 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/regular_price', $price, $role, |
| 138 |
$productId ); |
| 139 |
} |
| 140 |
|
| 141 |
return $price; |
| 142 |
} |
| 143 |
|
| 144 |
public static function getProductSaleRolePrice( int $productId, string $role, string $context = 'view' ): ?float { |
| 145 |
|
| 146 |
$price = get_post_meta( $productId, "_{$role}_tiered_price_sale_price", true ); |
| 147 |
|
| 148 |
$price = ! Form::isEmpty( $price ) ? floatval( $price ) : null; |
| 149 |
|
| 150 |
if ( 'edit' !== $context ) { |
| 151 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/sale_price', $price, $role, $productId ); |
| 152 |
} |
| 153 |
|
| 154 |
return $price; |
| 155 |
} |
| 156 |
|
| 157 |
public static function getProductDiscount( int $productId, string $role, string $context = 'view' ): ?float { |
| 158 |
$discount = get_post_meta( $productId, "_{$role}_tiered_price_discount", true ); |
| 159 |
|
| 160 |
$discount = ! Form::isEmpty( $discount ) ? floatval( $discount ) : null; |
| 161 |
|
| 162 |
if ( 'edit' !== $context ) { |
| 163 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/discount', $discount, $role, |
| 164 |
$productId ); |
| 165 |
} |
| 166 |
|
| 167 |
return $discount; |
| 168 |
} |
| 169 |
|
| 170 |
public static function getProductDiscountType( int $productId, string $role, string $context = 'view' ): string { |
| 171 |
$discountType = get_post_meta( $productId, "_{$role}_tiered_price_discount_type", true ); |
| 172 |
|
| 173 |
if ( 'edit' !== $context ) { |
| 174 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/discount_type', $discountType, $role, |
| 175 |
$productId ); |
| 176 |
} |
| 177 |
|
| 178 |
return in_array( $discountType, array( 'sale_price', 'regular_price' ) ) ? $discountType : 'sale_price'; |
| 179 |
} |
| 180 |
|
| 181 |
public static function getProductPricingType( int $productId, string $role, string $context = 'view' ): string { |
| 182 |
$pricingType = get_post_meta( $productId, "_{$role}_tiered_price_pricing_type", true ); |
| 183 |
|
| 184 |
$pricingType = in_array( $pricingType, array( 'flat', 'percentage' ) ) ? $pricingType : 'flat'; |
| 185 |
|
| 186 |
if ( 'edit' !== $context ) { |
| 187 |
return apply_filters( 'tiered_pricing_table/role_based_rules/price/pricing_type', $pricingType, $role, |
| 188 |
$productId ); |
| 189 |
} |
| 190 |
|
| 191 |
return $pricingType; |
| 192 |
} |
| 193 |
} |
| 194 |
|