class-shortcode-handler.php
2.93 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* Simple Product Role Based Price Settings
*
* @link https://wordpress.org/plugins/woocommerce-role-based-price/
* @since 1.0
* @package WooCommerce_Role_Based_Price
* @subpackage WooCommerce_Role_Based_Price/admin
* @author Varun Sridharan <varunsridharan23@gmail.com>
*/
if( ! defined('WPINC') ) {
exit;
}
class WooCommerce_Role_Based_Price_Shortcode_Handler {
private static $_instance = NULL;
private static $db_prices = NULL;
public function __construct() {
add_shortcode('wc_rbp', array( $this, 'shortcodehandler' ));
}
function is_aeliacs_active() {
return isset($GLOBALS['woocommerce-aelia-currencyswitcher']) && is_object($GLOBALS['woocommerce-aelia-currencyswitcher']);
}
public function shortcodehandler($attrs) {
$vars = shortcode_atts(array(
'id' => NULL,
'price' => 'regular_price',
'role' => 'current',
), $attrs, 'wc_rbp');
if( $vars['id'] == NULL ) {
global $product;
$id = '';
if( wc_rbp_is_wc_v('>=', '3.0.1') ) {
$id = $product->get_id();
} else {
$product->id;
}
if( ! isset($id) ) {
return __('Invalid Product ID Given', WC_RBP_TXT);
}
$vars['id'] = $id;
}
if( $vars['role'] == NULL ) {
return __('Invalid User Role Given', WC_RBP_TXT);
}
if( $vars['price'] == 'product_regular_price' || $vars['price'] == 'product_selling_price' ) {
return self::get_base_product_price($vars['id'], $vars['price']);
}
if( $vars['price'] != 'regular_price' && $vars['price'] != 'selling_price' ) {
return __('Invalid Price Type Given', WC_RBP_TXT);
}
$product_status = product_rbp_status($vars['id']);
$this->rbpPP = '';
if( $product_status ) {
$this->rbpPP = new WooCommerce_Role_Based_Price_Product_Pricing;
if( $vars['role'] == 'current' ) {
$vars['role'] = wc_rbp_get_current_user();
}
return self::get_selprice($vars['role'], $vars['price'], $vars['id']);
}
return '';
}
public function get_base_product_price($id, $price) {
if( ! defined('WC_RBP_SHORTCODE_PRODUCT_BASE_PRICING') ) {
define('WC_RBP_SHORTCODE_PRODUCT_BASE_PRICING', TRUE);
}
$product = new WC_Product($id);
if( $price == 'product_regular_price' ) {
return $product->get_regular_price();
}
if( $price == 'product_selling_price' ) {
return $product->get_sale_price();
}
}
public function get_selprice($role, $price = 'all', $product_id) {
$product = wc_get_product($product_id);
$p = $this->rbpPP->get_product_price('', $product, $price, $role);
return wc_price($p);
}
}