NonLoggedUsersService.php
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php namespace MeowCrew\RoleAndCustomerBasedPricing\Services;
use MeowCrew\RoleAndCustomerBasedPricing\Core\ServiceContainerTrait;
class NonLoggedUsersService {
use ServiceContainerTrait;
public function __construct() {
add_action( 'init', function () {
if ( ! is_user_logged_in() ) {
if ( $this->getContainer()->getSettings()->doHidePricesForNonLoggedInUsers() ) {
add_filter( 'woocommerce_get_price_html', '__return_empty_string', 99999 );
}
if ( $this->getContainer()->getSettings()->doPreventPurchaseForNonLoggedInUsers() ) {
add_filter( 'woocommerce_add_to_cart_validation', function ( $valid ) {
if ( $valid ) {
wc_add_notice( $this->getContainer()->getSettings()->getNonLoggedInUsersPurchaseMessage(), 'error' );
}
return false;
} );
}
$label = $this->getContainer()->getSettings()->getNonLoggedInUsersAddToCartButtonLabel();
if ( $label ) {
add_filter( 'woocommerce_product_single_add_to_cart_text', function () use ( $label ) {
return $label;
} );
add_filter( 'woocommerce_product_add_to_cart_text', function () use ( $label ) {
return $label;
} );
}
}
} );
}
}