| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\AbstractAddon; |
| 4 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 5 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\PricingTestLinks; |
| 6 |
|
| 7 |
class GlobalTieredPricingAddon extends AbstractAddon { |
| 8 |
|
| 9 |
public function getName(): string { |
| 10 |
return __( 'Global pricing rules', 'tier-pricing-table' ); |
| 11 |
} |
| 12 |
|
| 13 |
public function run() { |
| 14 |
|
| 15 |
// Enable pricing service |
| 16 |
add_filter( 'tiered_pricing_table/services/pricing_service_enabled', '__return_true' ); |
| 17 |
|
| 18 |
new LookupService(); |
| 19 |
new GlobalTieredPricingCPT(); |
| 20 |
new PricingTestLinks(); |
| 21 |
new GlobalTieredPricingCartManager(); |
| 22 |
new PricingService(); |
| 23 |
|
| 24 |
GlobalPricingRulesRepository::getInstance(); |
| 25 |
|
| 26 |
add_action( 'tiered_pricing_table/admin/pricing_tab_end', array( |
| 27 |
$this, |
| 28 |
'showMessageOnProductsTieredPricingTab', |
| 29 |
), 999 ); |
| 30 |
|
| 31 |
// The set of rules in force changes with schedules and priorities, so cached prices must change with it: |
| 32 |
// WooCommerce's variable price ranges and the plugin's own product data are keyed by this fingerprint. |
| 33 |
add_filter( 'woocommerce_get_variation_prices_hash', function ( $hash ) { |
| 34 |
if ( is_array( $hash ) ) { |
| 35 |
$hash[] = 'tpt-rules:' . self::getActiveRulesFingerprint(); |
| 36 |
} |
| 37 |
|
| 38 |
return $hash; |
| 39 |
} ); |
| 40 |
|
| 41 |
add_filter( 'tiered_pricing_table/cache/product_cache_key_salt', function ( $salt ) { |
| 42 |
return $salt . '|rules:' . self::getActiveRulesFingerprint(); |
| 43 |
} ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* The rules in force right now with their priorities, as one string. |
| 48 |
*/ |
| 49 |
public static function getActiveRulesFingerprint(): string { |
| 50 |
return implode( ',', array_map( function ( GlobalPricingRule $rule ) { |
| 51 |
return $rule->getId() . ':' . $rule->getPriority(); |
| 52 |
}, GlobalTieredPricingCPT::getGlobalRules() ) ); |
| 53 |
} |
| 54 |
|
| 55 |
public function showMessageOnProductsTieredPricingTab() { |
| 56 |
$globalRules = GlobalTieredPricingCPT::getGlobalRules( false ); |
| 57 |
|
| 58 |
if ( empty( $globalRules ) ) { |
| 59 |
$this->getContainer()->getFileManager()->includeTemplate( 'addons/global-rules/tiered-pricing-tab.php' ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function getDescription(): string { |
| 64 |
return __( 'Create reusable pricing rules and apply them globally.', 'tier-pricing-table' ); |
| 65 |
} |
| 66 |
|
| 67 |
public function getIcon(): string { |
| 68 |
return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>'; |
| 69 |
} |
| 70 |
|
| 71 |
public function getSlug(): string { |
| 72 |
return 'global-tier-pricing'; |
| 73 |
} |
| 74 |
} |
| 75 |
|