PluginProbe
Tiered Pricing Table for WooCommerce / 2.2.2
Tiered Pricing Table for WooCommerce v2.2.2
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
← All changes | src/Frontend/Frontend.php +241 -274 2.3.42.2.2 View file →
@@ -1,283 +1,250 @@
1 -<?php namespace TierPricingTable\Frontend;
1 +<?php
2 2
3 -use TierPricingTable\Freemius;
4 -use TierPricingTable\ProductManager;
5 -use TierPricingTable\Settings\Settings;
6 -use Premmerce\SDK\V2\FileManager\FileManager;
7 -use TierPricingTable\PriceManager;
8 -use TierPricingTable\TierPricingTablePlugin;
9 -use WP_Post;
10 -use WC_Product;
3 +namespace TierPricingTable\Frontend;
11 4
5 +use TierPricingTable\Freemius ;
6 +use TierPricingTable\ProductManager ;
7 +use TierPricingTable\Settings\Settings ;
8 +use Premmerce\SDK\V2\FileManager\FileManager ;
9 +use TierPricingTable\PriceManager ;
10 +use TierPricingTable\TierPricingTablePlugin ;
11 +use WP_Post ;
12 +use WC_Product ;
12 13 /**
13 14 * Class Frontend
14 15 *
15 16 * @package TierPricingTable\Frontend
16 17 */
17 -class Frontend {
18 +class Frontend
19 +{
20 + /**
21 + * @var FileManager
22 + */
23 + private $fileManager ;
24 + /**
25 + * @var Settings
26 + */
27 + private $settings ;
28 + /**s
29 + * @var CatalogPriceManager
30 + */
31 + private $catalogPriceManager ;
32 + /**
33 + * @var CartPriceManager
34 + */
35 + private $cartPriceManager ;
36 + /**
37 + * @var Freemius
38 + */
39 + private $licence ;
40 + /**
41 + * Frontend constructor.
42 + *
43 + * @param FileManager $fileManager
44 + * @param Freemius $licence
45 + * @param Settings $settings
46 + */
47 + public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings )
48 + {
49 + $this->fileManager = $fileManager;
50 + $this->settings = $settings;
51 + $this->licence = $licence;
52 + $this->initManagers();
53 + if ( $this->settings->get( 'display', 'yes' ) == 'yes' ) {
54 + // Render price table
55 + add_action( $this->settings->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), [ $this, 'displayPricingTable' ], -999 );
56 + }
57 + // Wrap price
58 + add_action(
59 + 'woocommerce_get_price_html',
60 + [ $this, 'wrapPrice' ],
61 + 10,
62 + 2
63 + );
64 + // Get table for variation
65 + add_action(
66 + 'wc_ajax_get_price_table',
67 + [ $this, 'getPriceTableVariation' ],
68 + 10,
69 + 1
70 + );
71 + // Enqueue frontend assets
72 + add_action(
73 + 'wp_enqueue_scripts',
74 + [ $this, 'enqueueAssets' ],
75 + 10,
76 + 1
77 + );
78 + // Render tooltip near product price if selected display type is "tooltip"
79 + if ( $this->settings->get( 'display', 'yes' ) === 'yes' && $this->settings->get( 'display_type' ) === 'tooltip' ) {
80 + add_filter(
81 + 'woocommerce_get_price_html',
82 + [ $this, 'renderTooltip' ],
83 + 10,
84 + 2
85 + );
86 + }
87 + }
88 +
89 + /**
90 + *
91 + */
92 + protected function initManagers()
93 + {
94 + $this->cartPriceManager = new CartPriceManager( $this->settings );
95 + }
96 +
97 + /**
98 + * Display table at frontend
99 + */
100 + public function displayPricingTable()
101 + {
102 + global $post ;
103 + if ( !$post ) {
104 + return;
105 + }
106 + $product = wc_get_product( $post->ID );
107 + if ( $product ) {
108 +
109 + if ( $product->is_type( 'simple' ) ) {
110 + $this->renderPricingTable( $product->get_id() );
111 + } elseif ( $product->is_type( 'variable' ) ) {
112 + echo '<div data-variation-price-rules-table></div>' ;
113 + }
114 +
115 + }
116 + }
117 +
118 + /**
119 + * Wrap product price for managing it by JS
120 + *
121 + * @param string $price_html
122 + * @param WC_Product $product
123 + *
124 + * @return string
125 + */
126 + public function wrapPrice( $price_html, $product )
127 + {
128 + if ( is_single() && ($product->is_type( 'simple' ) || $product->is_type( 'variation' )) ) {
129 + return '<span data-tiered-price-wrapper>' . $price_html . '</span>';
130 + }
131 + return $price_html;
132 + }
133 +
134 + /**
135 + * Render tooltip near product price if selected display type is "tooltip"
136 + *
137 + * @param string $price
138 + * @param WC_Product $_product
139 + *
140 + * @return string
141 + */
142 + public function renderTooltip( $price, $_product )
143 + {
144 +
145 + if ( is_product() ) {
146 + $page_product_id = get_queried_object_id();
147 +
148 + if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id || is_product() && $_product->is_type( 'simple' ) && $page_product_id === $_product->get_id() ) {
149 + $rules = PriceManager::getPriceRules( $_product->get_id() );
150 + if ( !empty($rules) ) {
151 + return $price . $this->fileManager->renderTemplate( 'frontend/tooltip.php', [
152 + 'color' => $this->settings->get( 'tooltip_color', '#cc99c2' ),
153 + 'size' => $this->settings->get( 'tooltip_size', 15 ) . 'px',
154 + ] );
155 + }
156 + }
157 +
158 + }
159 +
160 + return $price;
161 + }
162 +
163 + /**
164 + * Enqueue assets at simple product and variation product page.
165 + *
166 + * @global WP_Post $post .
167 + */
168 + public function enqueueAssets()
169 + {
170 + global $post ;
171 +
172 + if ( is_product() ) {
173 + $product = wc_get_product( $post->ID );
174 +
175 + if ( $product->is_type( 'variable' ) || $product->is_type( 'simple' ) ) {
176 + wp_enqueue_script( 'jquery' );
177 + wp_enqueue_script( 'jquery-ui-tooltip' );
178 + wp_enqueue_script(
179 + 'tier-pricing-table-front-js',
180 + $this->fileManager->locateAsset( 'frontend/product-tier-pricing-table.js' ),
181 + 'jquery',
182 + TierPricingTablePlugin::VERSION
183 + );
184 + wp_enqueue_style(
185 + 'tier-pricing-table-front-css',
186 + $this->fileManager->locateAsset( 'frontend/main.css' ),
187 + null,
188 + TierPricingTablePlugin::VERSION
189 + );
190 + wp_localize_script( 'tier-pricing-table-front-js', 'tieredPricingTable', [
191 + 'product_type' => $product->get_type(),
192 + 'settings' => $this->settings->getAll(),
193 + 'is_premium' => ( $this->licence->isFree() ? 'no' : 'yes' ),
194 + 'currency_options' => [
195 + 'currency_symbol' => get_woocommerce_currency_symbol(),
196 + 'decimal_separator' => wc_get_price_decimal_separator(),
197 + 'thousand_separator' => wc_get_price_thousand_separator(),
198 + 'decimals' => wc_get_price_decimals(),
199 + 'price_format' => get_woocommerce_price_format(),
200 + ],
201 + ] );
202 + }
203 +
204 + }
205 +
206 + }
207 +
208 + /**
209 + * Fired when user choose some variation. Render price rules table for it if it exists
210 + * @global WP_Post $post .
211 + */
212 + public function getPriceTableVariation()
213 + {
214 + $product_id = ( isset( $_POST['variation_id'] ) ? $_POST['variation_id'] : false );
215 + $product = wc_get_product( $product_id );
216 + if ( $product && $product->is_type( 'variation' ) ) {
217 + $this->renderPricingTable( $product->get_parent_id(), $product->get_id() );
218 + }
219 + }
220 +
221 + /**
222 + * Main function for rendering pricing table for product
223 + *
224 + * @param int $product_id
225 + * @param int $variation_id
226 + */
227 + public function renderPricingTable( $product_id, $variation_id = null )
228 + {
229 + $product = wc_get_product( $product_id );
230 + $product_id = ( !is_null( $variation_id ) ? $variation_id : $product->get_id() );
231 + // Exit if product is not valid
232 + if ( !$product || !($product->is_type( 'simple' ) || $product->is_type( 'variable' )) ) {
233 + return;
234 + }
235 + $rules = PriceManager::getPriceRules( $product_id );
236 + $real_price = ( !is_null( $variation_id ) ? wc_get_product( $variation_id )->get_price() : $product->get_price() );
237 +
238 + if ( !empty($rules) ) {
239 + $template = ( PriceManager::getPricingType( $product_id ) === 'percentage' ? 'price-table-percentage.php' : 'price-table-fixed.php' );
240 + $this->fileManager->includeTemplate( 'frontend/' . $template, [
241 + 'price_rules' => $rules,
242 + 'real_price' => $real_price,
243 + 'product_id' => $product_id,
244 + 'settings' => $this->settings->getAll(),
245 + ] );
246 + }
247 +
248 + }
18 249
19 - /**
20 - * @var FileManager
21 - */
22 - private $fileManager;
23 -
24 - /**
25 - * @var Settings
26 - */
27 - private $settings;
28 -
29 - /**s
30 - * @var CatalogPriceManager
31 - */
32 - private $catalogPriceManager;
33 -
34 - /**
35 - * @var CartPriceManager
36 - */
37 - private $cartPriceManager;
38 -
39 - /**
40 - * @var Freemius
41 - */
42 - private $licence;
43 -
44 - /**
45 - * Frontend constructor.
46 - *
47 - * @param FileManager $fileManager
48 - * @param Freemius $licence
49 - * @param Settings $settings
50 - */
51 - public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings ) {
52 - $this->fileManager = $fileManager;
53 - $this->settings = $settings;
54 - $this->licence = $licence;
55 -
56 - $this->initManagers();
57 -
58 - // Render price table
59 - add_action( $this->settings->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), [
60 - $this,
61 - 'displayPricingTable'
62 - ], - 999 );
63 -
64 - // Wrap price
65 - add_action( 'woocommerce_get_price_html', [ $this, 'wrapPrice' ], 10, 2 );
66 -
67 - // Get table for variation
68 - add_action( 'wc_ajax_get_price_table', [ $this, 'getPriceTableVariation' ], 10, 1 );
69 -
70 - // Enqueue frontend assets
71 - add_action( 'wp_enqueue_scripts', [ $this, 'enqueueAssets' ], 10, 1 );
72 -
73 - // Render tooltip near product price if selected display type is "tooltip"
74 - if ( $this->settings->get( 'display',
75 - 'yes' ) === 'yes' && $this->settings->get( 'display_type' ) === 'tooltip' ) {
76 - add_filter( 'woocommerce_get_price_html', [ $this, 'renderTooltip' ], 10, 2 );
77 - }
78 -
79 - if ( tpt_fs()->is__premium_only() ) {
80 - // Render summary block for the product
81 - if ( 'yes' === $this->settings->get( 'display_summary', 'yes' ) ) {
82 - add_action( $this->settings->get( 'summary_position_hook', 'woocommerce_after_add_to_cart_button' ),
83 - array( $this, 'renderSummary' ), 10 );
84 - }
85 - }
86 - }
87 -
88 - public function renderSummary() {
89 -
90 - global $post;
91 -
92 - if ( ! $post ) {
93 - return;
94 - }
95 -
96 - $product = wc_get_product( $post->ID );
97 -
98 - if ( ! $product ) {
99 - return;
100 - }
101 -
102 - $type = $this->settings->get( 'summary_type', 'table' );
103 -
104 - $this->fileManager->includeTemplate( 'frontend/summary-' . $type . '.php', array(
105 - 'needHide' => $product->is_type( 'variable' ),
106 - 'totalLabel' => $this->settings->get( 'summary_total_label', 'Total:' ),
107 - 'eachLabel' => $this->settings->get( 'summary_each_label', 'Each:' ),
108 - 'title' => $this->settings->get( 'summary_title', '' ),
109 - ) );
110 - }
111 -
112 - /**
113 - *
114 - */
115 - protected function initManagers() {
116 - $this->cartPriceManager = new CartPriceManager( $this->settings );
117 -
118 - if ( tpt_fs()->is__premium_only() ) {
119 - $this->catalogPriceManager = new CatalogPriceManager( $this->settings );
120 - }
121 -
122 - }
123 -
124 - /**
125 - * Display table at frontend
126 - */
127 - public function displayPricingTable() {
128 -
129 - global $post;
130 -
131 - if ( ! $post ) {
132 - return;
133 - }
134 -
135 - $product = wc_get_product( $post->ID );
136 -
137 - if ( $product ) {
138 - if ( $product->is_type( 'simple' ) ) {
139 - $this->renderPricingTable( $product->get_id() );
140 - } elseif ( $product->is_type( 'variable' ) ) {
141 - echo '<div data-variation-price-rules-table></div>';
142 - }
143 - }
144 - }
145 -
146 - /**
147 - * Wrap product price for managing it by JS
148 - *
149 - * @param string $price_html
150 - * @param WC_Product $product
151 - *
152 - * @return string
153 - */
154 - public function wrapPrice( $price_html, $product ) {
155 -
156 - if ( is_single() && ( $product->is_type( 'simple' ) || $product->is_type( 'variation' ) ) ) {
157 - return '<span data-tiered-price-wrapper>' . $price_html . '</span>';
158 - }
159 -
160 - return $price_html;
161 - }
162 -
163 - /**
164 - * Render tooltip near product price if selected display type is "tooltip"
165 - *
166 - * @param string $price
167 - * @param WC_Product $_product
168 - *
169 - * @return string
170 - */
171 - public function renderTooltip( $price, $_product ) {
172 -
173 - if ( is_product() ) {
174 -
175 - $page_product_id = get_queried_object_id();
176 -
177 - if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id
178 - || ( is_product() && $_product->is_type( 'simple' ) && $page_product_id === $_product->get_id() ) ) {
179 -
180 - $rules = PriceManager::getPriceRules( $_product->get_id() );
181 -
182 - if ( ! empty( $rules ) ) {
183 - return $price . $this->fileManager->renderTemplate( 'frontend/tooltip.php', [
184 - 'color' => $this->settings->get( 'tooltip_color', '#cc99c2' ),
185 - 'size' => $this->settings->get( 'tooltip_size', 15 ) . 'px',
186 - ]
187 - );
188 -
189 - }
190 - }
191 - }
192 -
193 - return $price;
194 - }
195 -
196 - /**
197 - * Enqueue assets at simple product and variation product page.
198 - *
199 - * @global WP_Post $post .
200 - */
201 - public function enqueueAssets() {
202 - global $post;
203 -
204 - if ( is_product() ) {
205 - $product = wc_get_product( $post->ID );
206 -
207 - wp_enqueue_script( 'jquery' );
208 - wp_enqueue_script( 'jquery-ui-tooltip' );
209 -
210 - wp_enqueue_script( 'tier-pricing-table-front-js',
211 - $this->fileManager->locateAsset( 'frontend/product-tier-pricing-table.js' ), 'jquery',
212 - TierPricingTablePlugin::VERSION );
213 -
214 - wp_enqueue_style( 'tier-pricing-table-front-css',
215 - $this->fileManager->locateAsset( 'frontend/main.css' ), null, TierPricingTablePlugin::VERSION );
216 -
217 - wp_localize_script( 'tier-pricing-table-front-js', 'tieredPricingTable',
218 - [
219 - 'product_type' => $product->get_type(),
220 - 'settings' => $this->settings->getAll(),
221 - 'is_premium' => $this->licence->isFree() ? 'no' : 'yes',
222 - 'currency_options' => [
223 - 'currency_symbol' => get_woocommerce_currency_symbol(),
224 - 'decimal_separator' => wc_get_price_decimal_separator(),
225 - 'thousand_separator' => wc_get_price_thousand_separator(),
226 - 'decimals' => wc_get_price_decimals(),
227 - 'price_format' => get_woocommerce_price_format(),
228 - ]
229 - ] );
230 - }
231 - }
232 -
233 - /**
234 - * Fired when user choose some variation. Render price rules table for it if it exists
235 - * @global WP_Post $post .
236 - */
237 - public function getPriceTableVariation() {
238 - $product_id = isset( $_POST['variation_id'] ) ? $_POST['variation_id'] : false;
239 -
240 - $product = wc_get_product( $product_id );
241 -
242 - if ( $product && $product->is_type( 'variation' ) ) {
243 - $this->renderPricingTable( $product->get_parent_id(), $product->get_id() );
244 - }
245 - }
246 -
247 - /**
248 - * Main function for rendering pricing table for product
249 - *
250 - * @param int $product_id
251 - * @param int $variation_id
252 - */
253 - public function renderPricingTable( $product_id, $variation_id = null ) {
254 -
255 - $product = wc_get_product( $product_id );
256 -
257 - $product_id = ! is_null( $variation_id ) ? $variation_id : $product->get_id();
258 -
259 - // Exit if product is not valid
260 - if ( ! $product || ! ( $product->is_type( 'simple' ) || $product->is_type( 'variable' ) ) ) {
261 - return;
262 - }
263 -
264 - $rules = PriceManager::getPriceRules( $product_id );
265 -
266 - $real_price = ! is_null( $variation_id ) ? wc_get_product( $variation_id )->get_price() : $product->get_price();
267 - $product_name = ! is_null( $variation_id ) ? wc_get_product( $variation_id )->get_name() : $product->get_name();
268 -
269 - if ( ! empty( $rules ) ) {
270 -
271 - $template = 'percentage' === PriceManager::getPricingType( $product_id ) ? 'price-table-percentage.php' : 'price-table-fixed.php';
272 -
273 - $this->fileManager->includeTemplate( 'frontend/' . $template, array(
274 - 'price_rules' => $rules,
275 - 'real_price' => $real_price,
276 - 'product_name' => $product_name,
277 - 'product_id' => $product_id,
278 - 'minimum' => PriceManager::getProductQtyMin( $product_id, 'view' ),
279 - 'settings' => $this->settings->getAll()
280 - ) );
281 - }
282 - }
283 250 }