PluginProbe
Tiered Pricing Table for WooCommerce / 2.3.4
Tiered Pricing Table for WooCommerce v2.3.4
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
tier-pricing-table / src / Frontend / Frontend.php

Frontend.php in Tiered Pricing Table for WooCommerce 2.3.4, at src/Frontend/Frontend.php

283 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Frontend;
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;
11
12 /**
13 * Class Frontend
14 *
15 * @package TierPricingTable\Frontend
16 */
17 class Frontend {
18
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 }