| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\CartRules; |
| 2 |
|
| 3 |
use TierPricingTable\Settings\CustomOptions\TPTCheckboxListOption; |
| 4 |
use TierPricingTable\Settings\Settings; |
| 5 |
|
| 6 |
class CartRulesSettings { |
| 7 |
|
| 8 |
const PREFIX = Settings::SETTINGS_PREFIX . 'cart_rules_'; |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
new RoleMinimumsOption(); |
| 12 |
|
| 13 |
// WooCommerce keeps computed shipping rates in the session until its shipping version changes; |
| 14 |
// a changed rule must show on the next cart load, not on the next cart change |
| 15 |
foreach ( array( 'added_option', 'updated_option', 'deleted_option' ) as $hook ) { |
| 16 |
add_action( $hook, array( $this, 'onOptionChanged' ) ); |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
public function onOptionChanged( $option ) { |
| 21 |
if ( 0 === strpos( (string) $option, self::PREFIX ) && class_exists( 'WC_Cache_Helper' ) ) { |
| 22 |
\WC_Cache_Helper::get_transient_version( 'shipping', true ); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
public static function optionId( string $key ): string { |
| 27 |
return self::PREFIX . $key; |
| 28 |
} |
| 29 |
|
| 30 |
protected static function get( string $key, $default = null ) { |
| 31 |
return get_option( self::optionId( $key ), $default ); |
| 32 |
} |
| 33 |
|
| 34 |
public static function isEnabled(): bool { |
| 35 |
return 'yes' === self::get( 'enabled', 'no' ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return array<string, array{amount: float, quantity: int}> |
| 40 |
*/ |
| 41 |
public static function getMinimums(): array { |
| 42 |
return CartRules::normalizeMinimums( self::get( 'minimums', '' ) ); |
| 43 |
} |
| 44 |
|
| 45 |
public static function getAmountMessage(): string { |
| 46 |
return (string) self::get( 'amount_message', self::defaultAmountMessage() ); |
| 47 |
} |
| 48 |
|
| 49 |
public static function getQuantityMessage(): string { |
| 50 |
return (string) self::get( 'quantity_message', self::defaultQuantityMessage() ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function defaultAmountMessage(): string { |
| 54 |
return __( 'A minimum order of {minimum} is required to check out. Your order subtotal is {subtotal}.', 'tier-pricing-table' ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function defaultQuantityMessage(): string { |
| 58 |
return __( 'A minimum of {minimum} items is required to check out. Your cart has {count}.', 'tier-pricing-table' ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @return string[] roles that may use the payment method; empty for everyone |
| 63 |
*/ |
| 64 |
public static function getGatewayRoles( string $gatewayId ): array { |
| 65 |
return TPTCheckboxListOption::toArray( self::get( 'gateway_' . sanitize_key( $gatewayId ), '' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return string[] roles that may use the shipping method instance; empty for everyone |
| 70 |
*/ |
| 71 |
public static function getShippingRoles( int $instanceId ): array { |
| 72 |
return TPTCheckboxListOption::toArray( self::get( 'shipping_' . $instanceId, '' ) ); |
| 73 |
} |
| 74 |
} |
| 75 |
|