| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers; |
| 2 |
|
| 3 |
use TierPricingTable\Core\ServiceContainerTrait; |
| 4 |
use TierPricingTable\Settings\Settings; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class NonLoggedInUsersService |
| 8 |
* |
| 9 |
* Handle non-logged-in users prices and purchase ability. |
| 10 |
* |
| 11 |
* @package TierPricingTable\Addons\NonLoggedInUsers |
| 12 |
*/ |
| 13 |
class NonLoggedInUsersService { |
| 14 |
|
| 15 |
use ServiceContainerTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* NonLoggedInUsersService constructor. |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
|
| 22 |
if ( is_user_logged_in() ) { |
| 23 |
return; |
| 24 |
} |
| 25 |
|
| 26 |
if ( $this->isHidePrices() ) { |
| 27 |
|
| 28 |
add_filter( 'woocommerce_get_price_html', function () { |
| 29 |
return '<div class="tpt-hidden-price price"><span>' . $this->getPriceMessage() . '</span></div>'; |
| 30 |
}, 9999 ); |
| 31 |
|
| 32 |
add_filter( 'tiered_pricing_table/should_render_pricing_table', '__return_false' ); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
if ( $this->isPreventPurchase() ) { |
| 37 |
|
| 38 |
add_filter( 'woocommerce_add_to_cart_validation', function ( $valid ) { |
| 39 |
|
| 40 |
if ( $valid ) { |
| 41 |
wc_add_notice( $this->getPurchaseMessage(), 'error' ); |
| 42 |
} |
| 43 |
|
| 44 |
return false; |
| 45 |
} ); |
| 46 |
} |
| 47 |
|
| 48 |
$label = $this->getAddToCartLabel(); |
| 49 |
|
| 50 |
if ( $label && $this->isPreventPurchase() ) { |
| 51 |
add_filter( 'woocommerce_product_single_add_to_cart_text', function () use ( $label ) { |
| 52 |
return $label; |
| 53 |
} ); |
| 54 |
|
| 55 |
add_filter( 'woocommerce_product_add_to_cart_text', function () use ( $label ) { |
| 56 |
return $label; |
| 57 |
} ); |
| 58 |
} |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public function isPreventPurchase(): bool { |
| 63 |
return get_option( Settings::SETTINGS_PREFIX . 'non_logged_in_users_prevent_purchase', 'no' ) === 'yes'; |
| 64 |
} |
| 65 |
|
| 66 |
public function getAddToCartLabel(): string { |
| 67 |
return get_option( Settings::SETTINGS_PREFIX . 'non_logged_in_users_add_to_cart_label', '' ); |
| 68 |
} |
| 69 |
|
| 70 |
public function getPurchaseMessage(): string { |
| 71 |
// translators: %s: account page link |
| 72 |
$default = sprintf( __( 'Please enter %s to make a purchase', 'tier-pricing-table' ), |
| 73 |
sprintf( '<a href="%s">%s</a>', wc_get_account_endpoint_url( 'dashboard' ), |
| 74 |
__( 'your account', 'tier-pricing-table' ) ) ); |
| 75 |
|
| 76 |
return get_option( Settings::SETTINGS_PREFIX . 'non_logged_in_users_purchase_message', $default ); |
| 77 |
} |
| 78 |
|
| 79 |
public function isHidePrices(): bool { |
| 80 |
return get_option( Settings::SETTINGS_PREFIX . 'non_logged_in_users_hide_prices', 'no' ) === 'yes'; |
| 81 |
} |
| 82 |
|
| 83 |
public function getPriceMessage(): string { |
| 84 |
// translators: %s: login page url |
| 85 |
$default = sprintf( __( '%s see prices', 'tier-pricing-table' ), |
| 86 |
sprintf( '<a href="%s">%s</a>', wc_get_account_endpoint_url( 'dashboard' ), |
| 87 |
__( 'Login', 'tier-pricing-table' ) ) ); |
| 88 |
|
| 89 |
return get_option( Settings::SETTINGS_PREFIX . 'non_logged_in_users_price_message', $default ); |
| 90 |
} |
| 91 |
} |
| 92 |
|