PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.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 All 91 releases
tier-pricing-table / src / Frontend / ProductPageHandler.php

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

240 lines 9.2 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 Exception ;
6 use TierPricingTable\Admin\ProductManagers\ProductManager ;
7 use TierPricingTable\Core\ServiceContainerTrait ;
8 use TierPricingTable\PriceManager ;
9 use TierPricingTable\PricingTable ;
10 use TierPricingTable\Settings\Sections\GeneralSection\Subsections\ProductPagePriceSubsection ;
11 use TierPricingTable\TierPricingTablePlugin ;
12 use WC_Product ;
13 use WC_Product_Data_Store_CPT ;
14 /**
15 * Class ProductPageHandler
16 *
17 * @package TierPricingTable\Frontend
18 */
19 class ProductPageHandler
20 {
21 use ServiceContainerTrait ;
22 public function __construct()
23 {
24 // Wrap price
25 add_action(
26 'woocommerce_get_price_html',
27 array( $this, 'wrapPriceOnProductPage' ),
28 10,
29 2
30 );
31 // Render price table
32 add_action( $this->getContainer()->getSettings()->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), [ $this, 'renderPricingTableOnProductPage' ], -999 );
33 // Get table for variation
34 add_action(
35 'wc_ajax_get_pricing_table',
36 array( $this, 'getVariationPricingTable' ),
37 10,
38 1
39 );
40 // Render tooltip if enabled
41 add_filter(
42 'woocommerce_get_price_html',
43 array( $this, 'renderTooltip' ),
44 999,
45 2
46 );
47 add_filter(
48 'woocommerce_get_price_suffix',
49 function (
50 $suffix,
51 \WC_product $product,
52 $price,
53 $qty
54 ) {
55 if ( empty($suffix) ) {
56 return $suffix;
57 }
58 $html = '';
59 $suffix = get_option( 'woocommerce_price_display_suffix' );
60
61 if ( $suffix && wc_tax_enabled() && 'taxable' === $product->get_tax_status() ) {
62 if ( '' === $price ) {
63 $price = $product->get_price();
64 }
65 $replacements = array(
66 '{price_including_tax}' => '<span class="tiered-pricing-dynamic-price__including_tax">' . wc_price( wc_get_price_including_tax( $product, array(
67 'qty' => $qty,
68 'price' => $price,
69 ) ) ) . '</span>',
70 '{price_excluding_tax}' => '<span class="tiered-pricing-dynamic-price__excluding_tax">' . wc_price( wc_get_price_excluding_tax( $product, array(
71 'qty' => $qty,
72 'price' => $price,
73 ) ) ) . '</span>',
74 );
75 $html = str_replace( array_keys( $replacements ), array_values( $replacements ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' );
76 }
77
78 return $html;
79 },
80 10,
81 4
82 );
83 }
84
85 /**
86 * Wrap product price for managing it by JS
87 *
88 * @param string $priceHTML
89 * @param WC_Product $product
90 *
91 * @return string
92 */
93 public function wrapPriceOnProductPage( $priceHTML, WC_Product $product )
94 {
95 // Wrapping shouldn't work elsewhere except the product page
96 if ( !is_product() ) {
97 return $priceHTML;
98 }
99 $parentProductID = ( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() );
100 // Do not wrap any prices except the current product page product
101 if ( $parentProductID !== get_queried_object_id() ) {
102 return $priceHTML;
103 }
104 // Do not wrap prices if tiered pricing price formatting enabled on the product page.
105 if ( 'same_as_catalog' === ProductPagePriceSubsection::getFormatPriceType() && $product->get_type() !== 'variation' ) {
106 return $priceHTML;
107 }
108 if ( !apply_filters(
109 'tiered_pricing_table/frontend/wrap_price',
110 true,
111 $product,
112 $priceHTML
113 ) ) {
114 return $priceHTML;
115 }
116 $isVariable = in_array( $product->get_type(), TierPricingTablePlugin::getSupportedVariableProductTypes() );
117 $pricingRules = PriceManager::getPricingRule( $product->get_id() );
118 // Do not wrap if there is no pricing rules
119 if ( !$isVariable && empty($pricingRules->getRules()) ) {
120 return $priceHTML;
121 }
122 $supportedTypes = array_merge( TierPricingTablePlugin::getSupportedSimpleProductTypes(), array( 'variation' ) );
123 $wrapVariableProductPrice = apply_filters( 'tiered_pricing_table/frontend/wrap_variable_price', true, $product );
124 // Is "show total price" is enabled, we can wrap the variable product price or it's forced by the hook
125 if ( $wrapVariableProductPrice || $this->getContainer()->getSettings()->get( 'show_total_price', 'no' ) === 'yes' ) {
126 $supportedTypes = array_merge( $supportedTypes, TierPricingTablePlugin::getSupportedVariableProductTypes() );
127 }
128 if ( in_array( $product->get_type(), $supportedTypes ) ) {
129 return '<span class="tiered-pricing-dynamic-price-wrapper' . (( $isVariable ? ' tiered-pricing-dynamic-price-wrapper--variable' : '' )) . '" data-product-id="' . $product->get_id() . '">' . $priceHTML . '</span>';
130 }
131 return $priceHTML;
132 }
133
134 /**
135 * Render a pricing table on the product page
136 */
137 public function renderPricingTableOnProductPage()
138 {
139 global $post ;
140 if ( !$post ) {
141 return;
142 }
143 $variationId = $this->getVariationIdFromURL( $post->ID );
144 PricingTable::getInstance()->renderPricingTable( $post->ID, $variationId );
145 }
146
147 public function getVariationIdFromURL( $productId )
148 {
149 $attributes = [];
150 $attributeWordLen = strlen( 'attribute_' );
151 foreach ( $_REQUEST as $key => $value ) {
152 if ( strlen( $key ) < $attributeWordLen ) {
153 continue;
154 }
155 $string = substr( $key, 0, $attributeWordLen );
156 if ( strcasecmp( $string, 'attribute_' ) === 0 ) {
157 $attributes[$key] = $value;
158 }
159 }
160 if ( empty($attributes) ) {
161 return 0;
162 }
163 $product = wc_get_product( $productId );
164 if ( !$product || !in_array( $product->get_type(), TierPricingTablePlugin::getSupportedVariableProductTypes() ) ) {
165 return 0;
166 }
167 $productDataStore = new WC_Product_Data_Store_CPT();
168 return $productDataStore->find_matching_product_variation( $product, $attributes );
169 }
170
171 /**
172 * Render tooltip near product price if selected display type is "tooltip"
173 *
174 * @param string $price
175 * @param WC_Product $_product
176 *
177 * @return string
178 */
179 public function renderTooltip( $price, $_product )
180 {
181 // Do not render if not display
182 if ( 'yes' !== $this->getContainer()->getSettings()->get( 'display', 'yes' ) ) {
183 return $price;
184 }
185 $displayType = ProductManager::getProductTemplate( ( $_product->get_parent_id() ? $_product->get_parent_id() : $_product->get_id() ) );
186 $displayType = ( $displayType === 'default' ? $this->getContainer()->getSettings()->get( 'display_type', 'table' ) : $displayType );
187 // Do not display if display type is not the tooltip
188 if ( 'tooltip' !== $displayType ) {
189 return $price;
190 }
191
192 if ( is_product() ) {
193 $addTooltip = false;
194 $page_product_id = get_queried_object_id();
195
196 if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id ) {
197 $addTooltip = true;
198 } elseif ( $_product->get_id() === $page_product_id && TierPricingTablePlugin::isSimpleProductSupported( $_product ) ) {
199 $addTooltip = true;
200 }
201
202 if ( !$addTooltip ) {
203 return $price;
204 }
205 $pricingRule = PriceManager::getPricingRule( $_product->get_id() );
206 if ( !empty($pricingRule->getRules()) ) {
207 return $price . $this->getContainer()->getFileManager()->renderTemplate( 'frontend/tooltip.php', array(
208 'color' => $this->getContainer()->getSettings()->get( 'tooltip_color', '#96598A' ),
209 'size' => $this->getContainer()->getSettings()->get( 'tooltip_size', 15 ) . 'px',
210 ) );
211 }
212 }
213
214 return $price;
215 }
216
217 /**
218 * Fired when user choose some variation. Render price rules table for it if it exists
219 *
220 * @throws Exception
221 * @global WP_Post $post .
222 */
223 public function getVariationPricingTable()
224 {
225 $product_id = ( isset( $_POST['variation_id'] ) ? sanitize_text_field( $_POST['variation_id'] ) : false );
226 $nonce = ( isset( $_POST['nonce'] ) ? sanitize_key( $_POST['nonce'] ) : false );
227
228 if ( wp_verify_nonce( $nonce, 'get_pricing_table' ) ) {
229 $product = wc_get_product( $product_id );
230
231 if ( $product ) {
232 $parentProduct = wc_get_product( $product->get_parent_id() );
233 PricingTable::getInstance()->renderPricingTableHTML( $parentProduct, $product );
234 }
235
236 }
237
238 }
239
240 }