| 1 |
<?php namespace TierPricingTable\Addons\UserBasedPricing; |
| 2 |
|
| 3 |
use TierPricingTable\TierPricingTablePlugin; |
| 4 |
use WC_Product; |
| 5 |
|
| 6 |
class UserBasedPricingRulesRepository { |
| 7 |
|
| 8 |
protected static $instance; |
| 9 |
|
| 10 |
public static function getInstance(): self { |
| 11 |
if ( ! self::$instance ) { |
| 12 |
self::$instance = new self(); |
| 13 |
} |
| 14 |
|
| 15 |
return self::$instance; |
| 16 |
} |
| 17 |
|
| 18 |
public function getCurrentUserRule( WC_Product $product ): ?UserBasedPricingRule { |
| 19 |
|
| 20 |
$userId = get_current_user_id(); |
| 21 |
|
| 22 |
if ( $userId ) { |
| 23 |
|
| 24 |
if ( UserBasedPriceManager::userHasRules( $userId, $product->get_id() ) ) { |
| 25 |
return UserBasedPricingRule::build( $product->get_id(), $userId ); |
| 26 |
} |
| 27 |
|
| 28 |
// Check also for parent level |
| 29 |
if ( TierPricingTablePlugin::isVariationProductSupported( $product ) ) { |
| 30 |
if ( UserBasedPriceManager::userHasRules( $userId, $product->get_parent_id() ) ) { |
| 31 |
return UserBasedPricingRule::build( $product->get_parent_id(), $userId ); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
return null; |
| 37 |
} |
| 38 |
} |
| 39 |
|