PluginProbe
Tiered Pricing Table for WooCommerce / 2.3.3
Tiered Pricing Table for WooCommerce v2.3.3
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.3, at src/Frontend/Frontend.php

270 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Frontend;
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 ;
13 /**
14 * Class Frontend
15 *
16 * @package TierPricingTable\Frontend
17 */
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 // Render price table
54 add_action( $this->settings->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), [ $this, 'displayPricingTable' ], -999 );
55 // Wrap price
56 add_action(
57 'woocommerce_get_price_html',
58 [ $this, 'wrapPrice' ],
59 10,
60 2
61 );
62 // Get table for variation
63 add_action(
64 'wc_ajax_get_price_table',
65 [ $this, 'getPriceTableVariation' ],
66 10,
67 1
68 );
69 // Enqueue frontend assets
70 add_action(
71 'wp_enqueue_scripts',
72 [ $this, 'enqueueAssets' ],
73 10,
74 1
75 );
76 // Render tooltip near product price if selected display type is "tooltip"
77 if ( $this->settings->get( 'display', 'yes' ) === 'yes' && $this->settings->get( 'display_type' ) === 'tooltip' ) {
78 add_filter(
79 'woocommerce_get_price_html',
80 [ $this, 'renderTooltip' ],
81 10,
82 2
83 );
84 }
85 }
86
87 public function renderSummary()
88 {
89 global $post ;
90 if ( !$post ) {
91 return;
92 }
93 $product = wc_get_product( $post->ID );
94 if ( !$product ) {
95 return;
96 }
97 $type = $this->settings->get( 'summary_type', 'table' );
98 $this->fileManager->includeTemplate( 'frontend/summary-' . $type . '.php', array(
99 'needHide' => $product->is_type( 'variable' ),
100 'totalLabel' => $this->settings->get( 'summary_total_label', 'Total:' ),
101 'eachLabel' => $this->settings->get( 'summary_each_label', 'Each:' ),
102 'title' => $this->settings->get( 'summary_title', '' ),
103 ) );
104 }
105
106 /**
107 *
108 */
109 protected function initManagers()
110 {
111 $this->cartPriceManager = new CartPriceManager( $this->settings );
112 }
113
114 /**
115 * Display table at frontend
116 */
117 public function displayPricingTable()
118 {
119 global $post ;
120 if ( !$post ) {
121 return;
122 }
123 $product = wc_get_product( $post->ID );
124 if ( $product ) {
125
126 if ( $product->is_type( 'simple' ) ) {
127 $this->renderPricingTable( $product->get_id() );
128 } elseif ( $product->is_type( 'variable' ) ) {
129 echo '<div data-variation-price-rules-table></div>' ;
130 }
131
132 }
133 }
134
135 /**
136 * Wrap product price for managing it by JS
137 *
138 * @param string $price_html
139 * @param WC_Product $product
140 *
141 * @return string
142 */
143 public function wrapPrice( $price_html, $product )
144 {
145 if ( is_single() && ($product->is_type( 'simple' ) || $product->is_type( 'variation' )) ) {
146 return '<span data-tiered-price-wrapper>' . $price_html . '</span>';
147 }
148 return $price_html;
149 }
150
151 /**
152 * Render tooltip near product price if selected display type is "tooltip"
153 *
154 * @param string $price
155 * @param WC_Product $_product
156 *
157 * @return string
158 */
159 public function renderTooltip( $price, $_product )
160 {
161
162 if ( is_product() ) {
163 $page_product_id = get_queried_object_id();
164
165 if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id || is_product() && $_product->is_type( 'simple' ) && $page_product_id === $_product->get_id() ) {
166 $rules = PriceManager::getPriceRules( $_product->get_id() );
167 if ( !empty($rules) ) {
168 return $price . $this->fileManager->renderTemplate( 'frontend/tooltip.php', [
169 'color' => $this->settings->get( 'tooltip_color', '#cc99c2' ),
170 'size' => $this->settings->get( 'tooltip_size', 15 ) . 'px',
171 ] );
172 }
173 }
174
175 }
176
177 return $price;
178 }
179
180 /**
181 * Enqueue assets at simple product and variation product page.
182 *
183 * @global WP_Post $post .
184 */
185 public function enqueueAssets()
186 {
187 global $post ;
188
189 if ( is_product() ) {
190 $product = wc_get_product( $post->ID );
191
192 if ( $product->is_type( 'variable' ) || $product->is_type( 'simple' ) ) {
193 wp_enqueue_script( 'jquery' );
194 wp_enqueue_script( 'jquery-ui-tooltip' );
195 wp_enqueue_script(
196 'tier-pricing-table-front-js',
197 $this->fileManager->locateAsset( 'frontend/product-tier-pricing-table.js' ),
198 'jquery',
199 TierPricingTablePlugin::VERSION
200 );
201 wp_enqueue_style(
202 'tier-pricing-table-front-css',
203 $this->fileManager->locateAsset( 'frontend/main.css' ),
204 null,
205 TierPricingTablePlugin::VERSION
206 );
207 wp_localize_script( 'tier-pricing-table-front-js', 'tieredPricingTable', [
208 'product_type' => $product->get_type(),
209 'settings' => $this->settings->getAll(),
210 'is_premium' => ( $this->licence->isFree() ? 'no' : 'yes' ),
211 'currency_options' => [
212 'currency_symbol' => get_woocommerce_currency_symbol(),
213 'decimal_separator' => wc_get_price_decimal_separator(),
214 'thousand_separator' => wc_get_price_thousand_separator(),
215 'decimals' => wc_get_price_decimals(),
216 'price_format' => get_woocommerce_price_format(),
217 ],
218 ] );
219 }
220
221 }
222
223 }
224
225 /**
226 * Fired when user choose some variation. Render price rules table for it if it exists
227 * @global WP_Post $post .
228 */
229 public function getPriceTableVariation()
230 {
231 $product_id = ( isset( $_POST['variation_id'] ) ? $_POST['variation_id'] : false );
232 $product = wc_get_product( $product_id );
233 if ( $product && $product->is_type( 'variation' ) ) {
234 $this->renderPricingTable( $product->get_parent_id(), $product->get_id() );
235 }
236 }
237
238 /**
239 * Main function for rendering pricing table for product
240 *
241 * @param int $product_id
242 * @param int $variation_id
243 */
244 public function renderPricingTable( $product_id, $variation_id = null )
245 {
246 $product = wc_get_product( $product_id );
247 $product_id = ( !is_null( $variation_id ) ? $variation_id : $product->get_id() );
248 // Exit if product is not valid
249 if ( !$product || !($product->is_type( 'simple' ) || $product->is_type( 'variable' )) ) {
250 return;
251 }
252 $rules = PriceManager::getPriceRules( $product_id );
253 $real_price = ( !is_null( $variation_id ) ? wc_get_product( $variation_id )->get_price() : $product->get_price() );
254 $product_name = ( !is_null( $variation_id ) ? wc_get_product( $variation_id )->get_name() : $product->get_name() );
255
256 if ( !empty($rules) ) {
257 $template = ( 'percentage' === PriceManager::getPricingType( $product_id ) ? 'price-table-percentage.php' : 'price-table-fixed.php' );
258 $this->fileManager->includeTemplate( 'frontend/' . $template, array(
259 'price_rules' => $rules,
260 'real_price' => $real_price,
261 'product_name' => $product_name,
262 'product_id' => $product_id,
263 'minimum' => PriceManager::getProductQtyMin( $product_id, 'view' ),
264 'settings' => $this->settings->getAll(),
265 ) );
266 }
267
268 }
269
270 }