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 +183 -91 1.12.2.2 View file →
@@ -1,13 +1,16 @@
1 -<?php namespace TierPricingTable\Frontend;
1 +<?php
2 2
3 -use TierPricingTable\Admin\Settings;
4 -use Premmerce\SDK\V2\FileManager\FileManager;
5 -use TierPricingTable\PriceManager;
6 -use WP_Post;
7 -use WC_Cart;
8 -use WC_Product;
3 +namespace TierPricingTable\Frontend;
9 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 ;
10 13 /**
11 14 * Class Frontend
12 15 *
13 16 * @package TierPricingTable\Frontend
@@ -16,106 +19,193 @@
16 19 {
17 20 /**
18 21 * @var FileManager
19 22 */
20 - private $fileManager;
21 -
23 + private $fileManager ;
22 24 /**
23 25 * @var Settings
24 26 */
25 - private $settings;
26 -
27 + private $settings ;
28 + /**s
29 + * @var CatalogPriceManager
30 + */
31 + private $catalogPriceManager ;
27 32 /**
33 + * @var CartPriceManager
34 + */
35 + private $cartPriceManager ;
36 + /**
37 + * @var Freemius
38 + */
39 + private $licence ;
40 + /**
28 41 * Frontend constructor.
42 + *
29 43 * @param FileManager $fileManager
44 + * @param Freemius $licence
30 45 * @param Settings $settings
31 46 */
32 - public function __construct(FileManager $fileManager, Settings $settings)
47 + public function __construct( FileManager $fileManager, Freemius $licence, Settings $settings )
33 48 {
34 49 $this->fileManager = $fileManager;
35 - $this->settings = $settings;
36 -
37 -
38 - if($this->settings->get('display') == 'yes'){
50 + $this->settings = $settings;
51 + $this->licence = $licence;
52 + $this->initManagers();
53 + if ( $this->settings->get( 'display', 'yes' ) == 'yes' ) {
39 54 // Render price table
40 - add_action($this->settings->get('position_hook'), 'render_price_quantity_table', -999);
55 + add_action( $this->settings->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), [ $this, 'displayPricingTable' ], -999 );
41 56 }
42 -
57 + // Wrap price
58 + add_action(
59 + 'woocommerce_get_price_html',
60 + [ $this, 'wrapPrice' ],
61 + 10,
62 + 2
63 + );
43 64 // Get table for variation
44 - add_action('wc_ajax_get_price_table', [$this, 'getPriceTableVariation']);
45 -
46 - add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
47 -
48 - add_action('woocommerce_before_calculate_totals', [$this, 'calculateTotals']);
49 -
50 - if($this->settings->get('display') == 'yes' && $this->settings->get('display_type') == 'tooltip'){
51 - add_filter('woocommerce_get_price_html', [$this, 'renderTooltip'], 1, 2);
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 + );
52 86 }
53 87 }
54 -
88 +
55 89 /**
56 - * @param string $price
57 - * @param WC_Product $instance
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 + *
58 124 * @return string
59 125 */
60 - public function renderTooltip($price, $instance)
126 + public function wrapPrice( $price_html, $product )
61 127 {
62 - $current_url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
63 - $current_product_id = url_to_postid($current_url);
64 -
65 - if(get_post_meta($instance->get_id(), '_price_rules', true)){
66 - if($instance->is_type('variation') && $instance->get_parent_id() === $current_product_id
67 - || (is_product() && $instance->is_type('simple') && $current_product_id == $instance->get_id())){
68 - return $price . $this->fileManager->renderTemplate('frontend/tooltip.php',
69 - [
70 - 'color' => $this->settings->get('tooltip_color'),
71 - 'size' => $this->settings->get('tooltip_size') . 'px',
72 - ]
73 - );
74 - }
128 + if ( is_single() && ($product->is_type( 'simple' ) || $product->is_type( 'variation' )) ) {
129 + return '<span data-tiered-price-wrapper>' . $price_html . '</span>';
75 130 }
76 -
77 - return $price;
131 + return $price_html;
78 132 }
133 +
79 134 /**
80 - * Change price by quantity rules
135 + * Render tooltip near product price if selected display type is "tooltip"
81 136 *
82 - * @param WC_Cart $cart
137 + * @param string $price
138 + * @param WC_Product $_product
139 + *
140 + * @return string
83 141 */
84 - public function calculateTotals($cart)
142 + public function renderTooltip( $price, $_product )
85 143 {
86 - foreach ($cart->cart_contents as $key => $product_item) {
87 - $product_id = !empty($product_item['variation_id']) ? $product_item['variation_id'] : $product_item['product_id'];
88 - $new_price = PriceManager::getPriceByRules($product_item['quantity'], $product_id);
89 -
90 - if ($new_price !== false) {
91 - $product_item['data']->set_price($new_price);
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 + }
92 156 }
157 +
93 158 }
159 +
160 + return $price;
94 161 }
95 -
162 +
96 163 /**
97 164 * Enqueue assets at simple product and variation product page.
165 + *
98 166 * @global WP_Post $post .
99 167 */
100 168 public function enqueueAssets()
101 169 {
102 - global $post;
103 -
104 - if (is_product()) {
105 - $product = wc_get_product($post->ID);
106 -
107 - if ($product->is_type('variable') || $product->is_type('simple')) {
108 - wp_enqueue_script('jquery');
109 - wp_enqueue_script('jquery-ui-tooltip');
110 - wp_enqueue_script('tier-pricing-table-front-js',
111 - $this->fileManager->locateAsset('frontend/product-tier-pricing-table.js'), 'jquery');
112 - wp_enqueue_style('tier-pricing-table-front-css', $this->fileManager->locateAsset('frontend/main.css'));
113 - wp_localize_script('tier-pricing-table-front-js', 'tierPricingTable', ['product_type' => $product->get_type(), 'settings' => $this->settings->getAll()]);
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 + ] );
114 202 }
203 +
115 204 }
205 +
116 206 }
117 -
207 +
118 208 /**
119 209 * Fired when user choose some variation. Render price rules table for it if it exists
120 210 * @global WP_Post $post .
121 211 */
@@ -120,39 +210,41 @@
120 210 * @global WP_Post $post .
121 211 */
122 212 public function getPriceTableVariation()
123 213 {
124 - $variation_id = isset($_POST['product_id']) ? $_POST['product_id'] : false;
125 - if ($variation_id) {
126 - $this->_renderPriceTable($variation_id);
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() );
127 218 }
128 219 }
129 -
220 +
130 221 /**
222 + * Main function for rendering pricing table for product
223 + *
224 + * @param int $product_id
131 225 * @param int $variation_id
132 226 */
133 - public function _renderPriceTable($variation_id = null)
227 + public function renderPricingTable( $product_id, $variation_id = null )
134 228 {
135 - global $post;
136 -
137 - $settings = $this->settings->getAll();
138 -
139 - $product = wc_get_product($post->ID);
140 - $product_id = !is_null($variation_id) ? $variation_id : $product->get_id();
141 -
142 - if ($product && !$product->is_type('simple') && !$product->is_type('variable')) {
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' )) ) {
143 233 return;
144 234 }
145 -
146 - $rules = PriceManager::getPriceRules($product_id);
147 - $real_price = !is_null($variation_id) ? wc_get_product($variation_id)->get_price() : $product->get_price();
148 -
149 - if (!empty($rules)) {
150 - $this->fileManager->includeTemplate('frontend/price-table.php', [
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, [
151 241 'price_rules' => $rules,
152 - 'real_price' => $real_price,
153 - 'product_id' => $product_id,
154 - 'settings' => $settings
155 - ]);
242 + 'real_price' => $real_price,
243 + 'product_id' => $product_id,
244 + 'settings' => $this->settings->getAll(),
245 + ] );
156 246 }
247 +
157 248 }
249 +
158 250 }