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

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

242 lines 7.4 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 if ( $this->settings->get( 'display' ) == 'yes' ) {
54 // Render price table
55 add_action( $this->settings->get( 'position_hook' ), [ $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' && $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' ),
153 'size' => $this->settings->get( 'tooltip_size' ) . '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 ] );
194 }
195
196 }
197
198 }
199
200 /**
201 * Fired when user choose some variation. Render price rules table for it if it exists
202 * @global WP_Post $post .
203 */
204 public function getPriceTableVariation()
205 {
206 $product_id = ( isset( $_POST['variation_id'] ) ? $_POST['variation_id'] : false );
207 $product = wc_get_product( $product_id );
208 if ( $product && $product->is_type( 'variation' ) ) {
209 $this->renderPricingTable( $product->get_parent_id(), $product->get_id() );
210 }
211 }
212
213 /**
214 * Main function for rendering pricing table for product
215 *
216 * @param int $product_id
217 * @param int $variation_id
218 */
219 public function renderPricingTable( $product_id, $variation_id = null )
220 {
221 $product = wc_get_product( $product_id );
222 $product_id = ( !is_null( $variation_id ) ? $variation_id : $product->get_id() );
223 // Exit if product is not valid
224 if ( !$product || !($product->is_type( 'simple' ) || $product->is_type( 'variable' )) ) {
225 return;
226 }
227 $rules = PriceManager::getPriceRules( $product_id );
228 $real_price = ( !is_null( $variation_id ) ? wc_get_product( $variation_id )->get_price() : $product->get_price() );
229
230 if ( !empty($rules) ) {
231 $template = ( PriceManager::getPricingType( $product_id ) === 'percentage' ? 'price-table-percentage.php' : 'price-table-fixed.php' );
232 $this->fileManager->includeTemplate( 'frontend/' . $template, [
233 'price_rules' => $rules,
234 'real_price' => $real_price,
235 'product_id' => $product_id,
236 'settings' => $this->settings->getAll(),
237 ] );
238 }
239
240 }
241
242 }