PluginProbe
Tiered Pricing Table for WooCommerce / 1.1
Tiered Pricing Table for WooCommerce v1.1
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 1.1, at src/Frontend/Frontend.php

158 lines 5.0 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\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;
9
10 /**
11 * Class Frontend
12 *
13 * @package TierPricingTable\Frontend
14 */
15 class Frontend
16 {
17 /**
18 * @var FileManager
19 */
20 private $fileManager;
21
22 /**
23 * @var Settings
24 */
25 private $settings;
26
27 /**
28 * Frontend constructor.
29 * @param FileManager $fileManager
30 * @param Settings $settings
31 */
32 public function __construct(FileManager $fileManager, Settings $settings)
33 {
34 $this->fileManager = $fileManager;
35 $this->settings = $settings;
36
37
38 if($this->settings->get('display') == 'yes'){
39 // Render price table
40 add_action($this->settings->get('position_hook'), 'render_price_quantity_table', -999);
41 }
42
43 // 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);
52 }
53 }
54
55 /**
56 * @param string $price
57 * @param WC_Product $instance
58 * @return string
59 */
60 public function renderTooltip($price, $instance)
61 {
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 }
75 }
76
77 return $price;
78 }
79 /**
80 * Change price by quantity rules
81 *
82 * @param WC_Cart $cart
83 */
84 public function calculateTotals($cart)
85 {
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);
92 }
93 }
94 }
95
96 /**
97 * Enqueue assets at simple product and variation product page.
98 * @global WP_Post $post .
99 */
100 public function enqueueAssets()
101 {
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()]);
114 }
115 }
116 }
117
118 /**
119 * Fired when user choose some variation. Render price rules table for it if it exists
120 * @global WP_Post $post .
121 */
122 public function getPriceTableVariation()
123 {
124 $variation_id = isset($_POST['product_id']) ? $_POST['product_id'] : false;
125 if ($variation_id) {
126 $this->_renderPriceTable($variation_id);
127 }
128 }
129
130 /**
131 * @param int $variation_id
132 */
133 public function _renderPriceTable($variation_id = null)
134 {
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')) {
143 return;
144 }
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', [
151 'price_rules' => $rules,
152 'real_price' => $real_price,
153 'product_id' => $product_id,
154 'settings' => $settings
155 ]);
156 }
157 }
158 }