| @@ -1,11 +1,10 @@ | ||
| 1 | -<?php | |
| 1 | +<?php namespace TierPricingTable\Services; | |
| 2 | 2 | |
| 3 | -namespace TierPricingTable\Services; | |
| 4 | - | |
| 5 | 3 | use Exception; |
| 6 | 4 | use TierPricingTable\Admin\ProductPage\TieredPricingTab; |
| 7 | 5 | use TierPricingTable\Core\ServiceContainerTrait; |
| 6 | +use TierPricingTable\Settings\Sections\GeneralSection\GeneralSection; | |
| 8 | 7 | use TierPricingTable\PriceManager; |
| 9 | 8 | use TierPricingTable\PricingTable; |
| 10 | 9 | use TierPricingTable\Settings\Sections\GeneralSection\Subsections\ProductPagePriceSubsection; |
| 11 | 10 | use TierPricingTable\TierPricingTablePlugin; |
| @@ -11,8 +10,9 @@ | ||
| 11 | 10 | use TierPricingTable\TierPricingTablePlugin; |
| 12 | 11 | use WC_Product; |
| 13 | 12 | use WC_Product_Data_Store_CPT; |
| 14 | 13 | use WP_Post; |
| 14 | + | |
| 15 | 15 | /** |
| 16 | 16 | * Class ProductPageHandler |
| 17 | 17 | * |
| 18 | 18 | * @package TierPricingTable\Frontend |
| @@ -17,289 +17,307 @@ | ||
| 17 | 17 | * |
| 18 | 18 | * @package TierPricingTable\Frontend |
| 19 | 19 | */ |
| 20 | 20 | class ProductPageService { |
| 21 | - use ServiceContainerTrait; | |
| 22 | - public function __construct() { | |
| 23 | - add_action( | |
| 24 | - 'woocommerce_quantity_input_classes', | |
| 25 | - function ( $classes, $product ) { | |
| 26 | - if ( $product instanceof WC_Product ) { | |
| 27 | - $classes[] = 'quantity-input-product-' . $product->get_id(); | |
| 28 | - } | |
| 29 | - return $classes; | |
| 30 | - }, | |
| 31 | - 10, | |
| 32 | - 2 | |
| 33 | - ); | |
| 34 | - add_filter( | |
| 35 | - 'woocommerce_available_variation', | |
| 36 | - function ( $data, WC_Product $product ) { | |
| 37 | - $data['parent_id'] = $product->get_id(); | |
| 38 | - return $data; | |
| 39 | - }, | |
| 40 | - 10, | |
| 41 | - 2 | |
| 42 | - ); | |
| 43 | - // Wrap price | |
| 44 | - add_action( | |
| 45 | - 'woocommerce_get_price_html', | |
| 46 | - array($this, 'wrapPrice'), | |
| 47 | - 101, | |
| 48 | - 2 | |
| 49 | - ); | |
| 50 | - // Render price table | |
| 51 | - add_action( $this->getContainer()->getSettings()->get( 'position_hook', 'woocommerce_before_add_to_cart_button' ), array($this, 'renderPricingTableOnProductPage'), -999 ); | |
| 52 | - // Get table for variation | |
| 53 | - add_action( | |
| 54 | - 'wc_ajax_get_pricing_table', | |
| 55 | - array($this, 'getVariationPricingTable'), | |
| 56 | - 10, | |
| 57 | - 1 | |
| 58 | - ); | |
| 59 | - // Render tooltip if enabled | |
| 60 | - add_filter( | |
| 61 | - 'woocommerce_get_price_html', | |
| 62 | - array($this, 'renderTooltip'), | |
| 63 | - 999, | |
| 64 | - 2 | |
| 65 | - ); | |
| 66 | - add_filter( | |
| 67 | - 'woocommerce_get_price_suffix', | |
| 68 | - function ( | |
| 69 | - $suffix, | |
| 70 | - \WC_product $product, | |
| 71 | - $price, | |
| 72 | - $qty | |
| 73 | - ) { | |
| 74 | - // Allow 3rd-party to control this | |
| 75 | - if ( !apply_filters( | |
| 76 | - 'tiered_pricing_table/frontend/modify_price_suffix', | |
| 77 | - true, | |
| 78 | - $suffix, | |
| 79 | - $product, | |
| 80 | - $price, | |
| 81 | - $qty | |
| 82 | - ) ) { | |
| 83 | - return $suffix; | |
| 84 | - } | |
| 85 | - if ( empty( $suffix ) ) { | |
| 86 | - return $suffix; | |
| 87 | - } | |
| 88 | - $html = ''; | |
| 89 | - $suffix = get_option( 'woocommerce_price_display_suffix' ); | |
| 90 | - if ( $suffix && wc_tax_enabled() && 'taxable' === $product->get_tax_status() ) { | |
| 91 | - if ( '' === $price ) { | |
| 92 | - $price = $product->get_price(); | |
| 93 | - } | |
| 94 | - $replacements = array( | |
| 95 | - '{price_including_tax}' => '<span class="tiered-pricing-dynamic-price__including_tax">' . wc_price( wc_get_price_including_tax( $product, array( | |
| 96 | - 'qty' => $qty, | |
| 97 | - 'price' => $price, | |
| 98 | - ) ) ) . '</span>', | |
| 99 | - '{price_excluding_tax}' => '<span class="tiered-pricing-dynamic-price__excluding_tax">' . wc_price( wc_get_price_excluding_tax( $product, array( | |
| 100 | - 'qty' => $qty, | |
| 101 | - 'price' => $price, | |
| 102 | - ) ) ) . '</span>', | |
| 103 | - ); | |
| 104 | - $html = str_replace( array_keys( $replacements ), array_values( $replacements ), ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' ); | |
| 105 | - } | |
| 106 | - return $html; | |
| 107 | - }, | |
| 108 | - 10, | |
| 109 | - 4 | |
| 110 | - ); | |
| 111 | - } | |
| 112 | 21 | |
| 113 | - /** | |
| 114 | - * Wrap product price for managing it by JS | |
| 115 | - * | |
| 116 | - * @param mixed $priceHTML | |
| 117 | - * @param WC_Product $product | |
| 118 | - * | |
| 119 | - * @return string | |
| 120 | - */ | |
| 121 | - public function wrapPrice( $priceHTML, WC_Product $product ) { | |
| 122 | - if ( is_admin() && !defined( 'DOING_AJAX' ) ) { | |
| 123 | - return $priceHTML; | |
| 124 | - } | |
| 125 | - // Do not wrap price in quick edit and inline edit in admin | |
| 126 | - if ( isset( $_POST['action'] ) && in_array( $_POST['action'], ['inline-save'], true ) ) { | |
| 127 | - return $priceHTML; | |
| 128 | - } | |
| 129 | - // Allow 3rd-party to control this | |
| 130 | - if ( !apply_filters( | |
| 131 | - 'tiered_pricing_table/frontend/wrap_price', | |
| 132 | - true, | |
| 133 | - $product, | |
| 134 | - $priceHTML | |
| 135 | - ) ) { | |
| 136 | - return $priceHTML; | |
| 137 | - } | |
| 138 | - $parentProductID = ( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ); | |
| 139 | - $variationID = ( TierPricingTablePlugin::isVariationProductSupported( $product ) ? $product->get_id() : null ); | |
| 140 | - $productPagePrice = get_queried_object_id() === $parentProductID && is_product(); | |
| 141 | - $priceDisplayContext = ( $productPagePrice ? 'product-page' : 'shop-loop' ); | |
| 142 | - $priceType = apply_filters( | |
| 143 | - 'tiered_pricing_table/frontend/default_price_behaviour_type', | |
| 144 | - 'dynamic', | |
| 145 | - $product, | |
| 146 | - $priceHTML, | |
| 147 | - $priceDisplayContext | |
| 148 | - ); | |
| 149 | - if ( $productPagePrice && $product->get_type() !== 'variation' ) { | |
| 150 | - // Do not format prices if tiered pricing formatting enabled on the product page. | |
| 151 | - if ( 'same_as_catalog' === ProductPagePriceSubsection::getFormatPriceType() ) { | |
| 152 | - $priceType = 'static'; | |
| 153 | - } | |
| 154 | - } | |
| 155 | - $isVariable = in_array( $product->get_type(), TierPricingTablePlugin::getSupportedVariableProductTypes() ); | |
| 156 | - $pricingRules = PriceManager::getPricingRule( $product->get_id() ); | |
| 157 | - // Do not wrap if there is no pricing rules | |
| 158 | - if ( !$isVariable && empty( $pricingRules->getRules() ) ) { | |
| 159 | - $priceType = 'no-rules'; | |
| 160 | - } | |
| 161 | - $supportedTypes = array_merge( TierPricingTablePlugin::getSupportedSimpleProductTypes(), array('variation') ); | |
| 162 | - $wrapVariableProductPrice = apply_filters( 'tiered_pricing_table/frontend/wrap_variable_price', true, $product ); | |
| 163 | - // Is "show total price" is enabled, we can wrap the variable product price, or it's forced by the hook | |
| 164 | - if ( $wrapVariableProductPrice || $this->getContainer()->getSettings()->get( 'show_total_price', 'no' ) === 'yes' ) { | |
| 165 | - $supportedTypes = array_merge( $supportedTypes, TierPricingTablePlugin::getSupportedVariableProductTypes() ); | |
| 166 | - } | |
| 167 | - if ( in_array( $product->get_type(), $supportedTypes ) ) { | |
| 168 | - ob_start(); | |
| 169 | - ?> | |
| 170 | - <span class="tiered-pricing-dynamic-price-wrapper<?php | |
| 171 | - echo ( $isVariable ? ' tiered-pricing-dynamic-price-wrapper--variable' : '' ); | |
| 172 | - ?>" | |
| 173 | - data-display-context="<?php | |
| 174 | - echo esc_attr( $priceDisplayContext ); | |
| 175 | - ?>" | |
| 176 | - data-price-type="<?php | |
| 177 | - echo esc_attr( $priceType ); | |
| 178 | - ?>" | |
| 179 | - data-product-id="<?php | |
| 180 | - echo esc_attr( $product->get_id() ); | |
| 181 | - ?>" | |
| 182 | - data-parent-id="<?php | |
| 183 | - echo esc_attr( ( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ) ); | |
| 184 | - ?>"> | |
| 185 | - <?php | |
| 186 | - return ob_get_clean() . $priceHTML . '</span>'; | |
| 187 | - } | |
| 188 | - return $priceHTML; | |
| 189 | - } | |
| 22 | + use ServiceContainerTrait; | |
| 190 | 23 | |
| 191 | - /** | |
| 192 | - * Render a pricing table on the product page | |
| 193 | - */ | |
| 194 | - public function renderPricingTableOnProductPage() { | |
| 195 | - global $post; | |
| 196 | - if ( !$post || !is_product() ) { | |
| 197 | - return; | |
| 198 | - } | |
| 199 | - $variationId = $this->getVariationIdFromURL( $post->ID ); | |
| 200 | - PricingTable::getInstance()->renderPricingTable( $post->ID, $variationId ); | |
| 201 | - } | |
| 24 | + public function __construct() { | |
| 202 | 25 | |
| 203 | - public function getVariationIdFromURL( $productId ) : ?int { | |
| 204 | - $attributes = array(); | |
| 205 | - $attributeWordLen = strlen( 'attribute_' ); | |
| 206 | - foreach ( $_REQUEST as $key => $value ) { | |
| 207 | - if ( strlen( $key ) < $attributeWordLen ) { | |
| 208 | - continue; | |
| 209 | - } | |
| 210 | - $string = substr( $key, 0, $attributeWordLen ); | |
| 211 | - if ( strcasecmp( $string, 'attribute_' ) === 0 ) { | |
| 212 | - $attributes[$key] = $value; | |
| 213 | - } | |
| 214 | - } | |
| 215 | - if ( empty( $attributes ) ) { | |
| 216 | - return null; | |
| 217 | - } | |
| 218 | - $product = wc_get_product( $productId ); | |
| 219 | - if ( !$product ) { | |
| 220 | - return null; | |
| 221 | - } | |
| 222 | - if ( !TierPricingTablePlugin::isVariableProductSupported( $product ) ) { | |
| 223 | - return null; | |
| 224 | - } | |
| 225 | - return ( new WC_Product_Data_Store_CPT() )->find_matching_product_variation( $product, $attributes ); | |
| 226 | - } | |
| 26 | + add_action( 'woocommerce_quantity_input_classes', function ( $classes, $product ) { | |
| 27 | + if ( $product instanceof WC_Product ) { | |
| 28 | + $classes[] = 'quantity-input-product-' . $product->get_id(); | |
| 29 | + } | |
| 227 | 30 | |
| 228 | - /** | |
| 229 | - * Render tooltip near product price if selected display type is "tooltip" | |
| 230 | - * | |
| 231 | - * @param ?string $price | |
| 232 | - * @param WC_Product $_product | |
| 233 | - * | |
| 234 | - * @return string | |
| 235 | - */ | |
| 236 | - public function renderTooltip( ?string $price, WC_Product $_product ) : ?string { | |
| 237 | - // Do not render if not display | |
| 238 | - if ( 'yes' !== $this->getContainer()->getSettings()->get( 'display', 'yes' ) ) { | |
| 239 | - return $price; | |
| 240 | - } | |
| 241 | - $displayType = TieredPricingTab::getProductTemplate( ( $_product->get_parent_id() ? $_product->get_parent_id() : $_product->get_id() ) ); | |
| 242 | - $displayType = ( 'default' === $displayType ? $this->getContainer()->getSettings()->get( 'display_type', 'table' ) : $displayType ); | |
| 243 | - // Do not display if display type is not the tooltip | |
| 244 | - if ( 'tooltip' !== $displayType ) { | |
| 245 | - return $price; | |
| 246 | - } | |
| 247 | - if ( is_product() ) { | |
| 248 | - $addTooltip = false; | |
| 249 | - $page_product_id = get_queried_object_id(); | |
| 250 | - if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id ) { | |
| 251 | - $addTooltip = true; | |
| 252 | - } elseif ( $_product->get_id() === $page_product_id && TierPricingTablePlugin::isSimpleProductSupported( $_product ) ) { | |
| 253 | - $addTooltip = true; | |
| 254 | - } | |
| 255 | - if ( !$addTooltip ) { | |
| 256 | - return $price; | |
| 257 | - } | |
| 258 | - $pricingRule = PriceManager::getPricingRule( $_product->get_id() ); | |
| 259 | - if ( !empty( $pricingRule->getRules() ) ) { | |
| 260 | - return $price . $this->getContainer()->getFileManager()->renderTemplate( 'frontend/tooltip.php', array( | |
| 261 | - 'color' => $this->getContainer()->getSettings()->get( 'tooltip_color', '#96598A' ), | |
| 262 | - 'size' => $this->getContainer()->getSettings()->get( 'tooltip_size', 15 ) . 'px', | |
| 263 | - ) ); | |
| 264 | - } | |
| 265 | - } | |
| 266 | - return $price; | |
| 267 | - } | |
| 31 | + return $classes; | |
| 32 | + }, 10, 2 ); | |
| 268 | 33 | |
| 269 | - /** | |
| 270 | - * Fired when user chooses a variation. Renders tiered pricing table for the variation | |
| 271 | - * | |
| 272 | - * @throws Exception | |
| 273 | - * @global WP_Post $post . | |
| 274 | - */ | |
| 275 | - public function getVariationPricingTable() { | |
| 276 | - $product_id = ( isset( $_POST['variation_id'] ) ? sanitize_text_field( $_POST['variation_id'] ) : false ); | |
| 277 | - $nonce = ( isset( $_POST['nonce'] ) ? sanitize_key( $_POST['nonce'] ) : false ); | |
| 278 | - $displayContext = ( isset( $_POST['display_context'] ) ? sanitize_text_field( $_POST['display_context'] ) : 'product-page' ); | |
| 279 | - $renderSettings = apply_filters( | |
| 280 | - 'tiered_pricing_table/frontend/variation_render_settings', | |
| 281 | - array(), | |
| 282 | - $product_id, | |
| 283 | - $displayContext | |
| 284 | - ); | |
| 285 | - // Some cache plugins may cache the nonce, so we need to leave the ability to disable nonce verification. | |
| 286 | - // Checking nonce is not critical here. | |
| 287 | - $verifyNonce = apply_filters( | |
| 288 | - 'tiered_pricing_table/frontend/load_variation/verify_nonce', | |
| 289 | - false, | |
| 290 | - $nonce, | |
| 291 | - 'get_pricing_table' | |
| 292 | - ); | |
| 293 | - if ( $verifyNonce && !wp_verify_nonce( $nonce, 'get_pricing_table' ) ) { | |
| 294 | - return; | |
| 295 | - } | |
| 296 | - $product = wc_get_product( $product_id ); | |
| 297 | - if ( $product ) { | |
| 298 | - $parentProduct = wc_get_product( $product->get_parent_id() ); | |
| 299 | - if ( $product ) { | |
| 300 | - PricingTable::getInstance()->renderPricingTableHTML( $parentProduct, $product, $renderSettings ); | |
| 301 | - } | |
| 302 | - } | |
| 303 | - } | |
| 34 | + add_filter( 'woocommerce_available_variation', function ( $data, WC_Product $product ) { | |
| 35 | + $data['parent_id'] = $product->get_id(); | |
| 304 | 36 | |
| 37 | + return $data; | |
| 38 | + }, 10, 2 ); | |
| 39 | + | |
| 40 | + // Wrap price | |
| 41 | + add_action( 'woocommerce_get_price_html', array( $this, 'wrapPrice' ), 101, 2 ); | |
| 42 | + | |
| 43 | + // Render price table | |
| 44 | + add_action( GeneralSection::getPositionHook(), array( | |
| 45 | + $this, | |
| 46 | + 'renderPricingTableOnProductPage', | |
| 47 | + ), - 999 ); | |
| 48 | + | |
| 49 | + // Get table for variation | |
| 50 | + add_action( 'wc_ajax_get_pricing_table', array( $this, 'getVariationPricingTable' ), 10, 1 ); | |
| 51 | + | |
| 52 | + // Render tooltip if enabled | |
| 53 | + add_filter( 'woocommerce_get_price_html', array( $this, 'renderTooltip' ), 999, 2 ); | |
| 54 | + | |
| 55 | + add_filter( 'woocommerce_get_price_suffix', function ( $suffix, \WC_product $product, $price, $qty ) { | |
| 56 | + | |
| 57 | + // Allow 3rd-party to control this | |
| 58 | + if ( ! apply_filters( 'tiered_pricing_table/frontend/modify_price_suffix', true, $suffix, $product, $price, | |
| 59 | + $qty ) ) { | |
| 60 | + return $suffix; | |
| 61 | + } | |
| 62 | + | |
| 63 | + if ( empty( $suffix ) ) { | |
| 64 | + return $suffix; | |
| 65 | + } | |
| 66 | + | |
| 67 | + $html = ''; | |
| 68 | + | |
| 69 | + $suffix = get_option( 'woocommerce_price_display_suffix' ); | |
| 70 | + | |
| 71 | + if ( $suffix && wc_tax_enabled() && 'taxable' === $product->get_tax_status() ) { | |
| 72 | + | |
| 73 | + if ( '' === $price ) { | |
| 74 | + $price = $product->get_price(); | |
| 75 | + } | |
| 76 | + | |
| 77 | + $replacements = array( | |
| 78 | + '{price_including_tax}' => '<span class="tiered-pricing-dynamic-price__including_tax">' . wc_price( wc_get_price_including_tax( $product, | |
| 79 | + array( | |
| 80 | + 'qty' => $qty, | |
| 81 | + 'price' => $price, | |
| 82 | + ) ) ) . '</span>', | |
| 83 | + '{price_excluding_tax}' => '<span class="tiered-pricing-dynamic-price__excluding_tax">' . wc_price( wc_get_price_excluding_tax( $product, | |
| 84 | + array( | |
| 85 | + 'qty' => $qty, | |
| 86 | + 'price' => $price, | |
| 87 | + ) ) ) . '</span>', | |
| 88 | + ); | |
| 89 | + | |
| 90 | + $html = str_replace( array_keys( $replacements ), array_values( $replacements ), | |
| 91 | + ' <small class="woocommerce-price-suffix">' . wp_kses_post( $suffix ) . '</small>' ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + return $html; | |
| 95 | + | |
| 96 | + }, 10, 4 ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Wrap product price for managing it by JS | |
| 101 | + * | |
| 102 | + * @param mixed $priceHTML | |
| 103 | + * @param WC_Product $product | |
| 104 | + * | |
| 105 | + * @return string | |
| 106 | + */ | |
| 107 | + public function wrapPrice( $priceHTML, WC_Product $product ) { | |
| 108 | + | |
| 109 | + if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { | |
| 110 | + return $priceHTML; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Do not wrap price in quick edit and inline edit in admin | |
| 114 | + if ( isset( $_POST['action'] ) && in_array( $_POST['action'], [ | |
| 115 | + 'inline-save', | |
| 116 | + ], true ) ) { | |
| 117 | + return $priceHTML; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Allow 3rd-party to control this | |
| 121 | + if ( ! apply_filters( 'tiered_pricing_table/frontend/wrap_price', true, $product, $priceHTML ) ) { | |
| 122 | + return $priceHTML; | |
| 123 | + } | |
| 124 | + | |
| 125 | + $parentProductID = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id(); | |
| 126 | + $variationID = TierPricingTablePlugin::isVariationProductSupported( $product ) ? $product->get_id() : null; | |
| 127 | + $productPagePrice = get_queried_object_id() === $parentProductID && is_product(); | |
| 128 | + $priceDisplayContext = $productPagePrice ? 'product-page' : 'shop-loop'; | |
| 129 | + | |
| 130 | + $priceType = apply_filters( 'tiered_pricing_table/frontend/default_price_behaviour_type', 'dynamic', $product, | |
| 131 | + $priceHTML, $priceDisplayContext ); | |
| 132 | + | |
| 133 | + if ( $productPagePrice && $product->get_type() !== 'variation' ) { | |
| 134 | + // Do not format prices if tiered pricing formatting enabled on the product page. | |
| 135 | + if ( 'same_as_catalog' === ProductPagePriceSubsection::getFormatPriceType() ) { | |
| 136 | + $priceType = 'static'; | |
| 137 | + } | |
| 138 | + } | |
| 139 | + | |
| 140 | + $isVariable = in_array( $product->get_type(), TierPricingTablePlugin::getSupportedVariableProductTypes() ); | |
| 141 | + | |
| 142 | + $pricingRules = \TierPricingTable\PriceManager::getPricingRule( $product->get_id() ); | |
| 143 | + | |
| 144 | + // Set to no-rules if there are no pricing rules and the main dynamic price setting is disabled | |
| 145 | + $updatePrice = $this->getContainer()->getSettings()->get( 'update_price_on_product_page', 'yes' ) === 'yes'; | |
| 146 | + if ( ! $isVariable && empty( $pricingRules->getRules() ) && ! $updatePrice ) { | |
| 147 | + $priceType = 'no-rules'; | |
| 148 | + } | |
| 149 | + | |
| 150 | + $supportedTypes = array_merge( TierPricingTablePlugin::getSupportedSimpleProductTypes(), array( 'variation' ) ); | |
| 151 | + | |
| 152 | + $wrapVariableProductPrice = apply_filters( 'tiered_pricing_table/frontend/wrap_variable_price', true, | |
| 153 | + $product ); | |
| 154 | + | |
| 155 | + $hasRules = PricingTable::getInstance()->productHasPricingRules( $product ); | |
| 156 | + $showTotalPrice = $hasRules | |
| 157 | + ? $this->getContainer()->getSettings()->get( 'show_total_price', 'no' ) === 'yes' | |
| 158 | + : $this->getContainer()->getSettings()->get( 'show_total_price_non_tiered', 'no' ) === 'yes'; | |
| 159 | + | |
| 160 | + // Is "show total price" is enabled, we can wrap the variable product price, or it's forced by the hook | |
| 161 | + if ( $wrapVariableProductPrice || $showTotalPrice ) { | |
| 162 | + $supportedTypes = array_merge( $supportedTypes, | |
| 163 | + TierPricingTablePlugin::getSupportedVariableProductTypes() ); | |
| 164 | + } | |
| 165 | + | |
| 166 | + if ( in_array( $product->get_type(), $supportedTypes ) ) { | |
| 167 | + | |
| 168 | + ob_start(); | |
| 169 | + | |
| 170 | + ?> | |
| 171 | + <span class="tiered-pricing-dynamic-price-wrapper<?php echo $isVariable ? ' tiered-pricing-dynamic-price-wrapper--variable' : ''; ?>" | |
| 172 | + data-display-context="<?php echo esc_attr( $priceDisplayContext ); ?>" | |
| 173 | + data-price-type="<?php echo esc_attr( $priceType ); ?>" | |
| 174 | + data-product-id="<?php echo esc_attr( $product->get_id() ); ?>" | |
| 175 | + data-parent-id="<?php echo esc_attr( $product->get_parent_id() ? $product->get_parent_id() : $product->get_id() ); ?>"> | |
| 176 | + <?php | |
| 177 | + return ob_get_clean() . $priceHTML . '</span>'; | |
| 178 | + } | |
| 179 | + | |
| 180 | + return $priceHTML; | |
| 181 | + } | |
| 182 | + | |
| 183 | + /** | |
| 184 | + * Render a pricing table on the product page | |
| 185 | + */ | |
| 186 | + public function renderPricingTableOnProductPage() { | |
| 187 | + | |
| 188 | + global $post; | |
| 189 | + | |
| 190 | + if ( ! $post || ! is_product() ) { | |
| 191 | + return; | |
| 192 | + } | |
| 193 | + | |
| 194 | + $variationId = $this->getVariationIdFromURL( $post->ID ); | |
| 195 | + | |
| 196 | + PricingTable::getInstance()->renderPricingTable( $post->ID, $variationId ); | |
| 197 | + } | |
| 198 | + | |
| 199 | + public function getVariationIdFromURL( $productId ): ?int { | |
| 200 | + | |
| 201 | + $attributes = array(); | |
| 202 | + $attributeWordLen = strlen( 'attribute_' ); | |
| 203 | + | |
| 204 | + foreach ( $_REQUEST as $key => $value ) { | |
| 205 | + | |
| 206 | + if ( strlen( $key ) < $attributeWordLen ) { | |
| 207 | + continue; | |
| 208 | + } | |
| 209 | + | |
| 210 | + $string = substr( $key, 0, $attributeWordLen ); | |
| 211 | + | |
| 212 | + if ( strcasecmp( $string, 'attribute_' ) === 0 ) { | |
| 213 | + $attributes[ $key ] = $value; | |
| 214 | + } | |
| 215 | + } | |
| 216 | + | |
| 217 | + if ( empty( $attributes ) ) { | |
| 218 | + return null; | |
| 219 | + } | |
| 220 | + | |
| 221 | + $product = wc_get_product( $productId ); | |
| 222 | + | |
| 223 | + if ( ! $product ) { | |
| 224 | + return null; | |
| 225 | + } | |
| 226 | + | |
| 227 | + if ( ! TierPricingTablePlugin::isVariableProductSupported( $product ) ) { | |
| 228 | + return null; | |
| 229 | + } | |
| 230 | + | |
| 231 | + return ( new WC_Product_Data_Store_CPT() )->find_matching_product_variation( $product, $attributes ); | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Render tooltip near product price if selected display type is "tooltip" | |
| 236 | + * | |
| 237 | + * @param ?string $price | |
| 238 | + * @param WC_Product $_product | |
| 239 | + * | |
| 240 | + * @return string | |
| 241 | + */ | |
| 242 | + public function renderTooltip( ?string $price, WC_Product $_product ): ?string { | |
| 243 | + | |
| 244 | + // Do not render if not display | |
| 245 | + if ( ! GeneralSection::isAutomaticDisplayEnabled() ) { | |
| 246 | + return $price; | |
| 247 | + } | |
| 248 | + | |
| 249 | + $displayType = TieredPricingTab::getProductTemplate( $_product->get_parent_id() ? $_product->get_parent_id() : $_product->get_id() ); | |
| 250 | + $displayType = 'default' === $displayType ? $this->getContainer()->getSettings()->get( 'display_type', | |
| 251 | + 'table' ) : $displayType; | |
| 252 | + | |
| 253 | + // Do not display if display type is not the tooltip | |
| 254 | + if ( 'tooltip' !== $displayType ) { | |
| 255 | + return $price; | |
| 256 | + } | |
| 257 | + | |
| 258 | + if ( is_product() ) { | |
| 259 | + $addTooltip = false; | |
| 260 | + $page_product_id = get_queried_object_id(); | |
| 261 | + | |
| 262 | + if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id ) { | |
| 263 | + $addTooltip = true; | |
| 264 | + } elseif ( $_product->get_id() === $page_product_id && TierPricingTablePlugin::isSimpleProductSupported( $_product ) ) { | |
| 265 | + $addTooltip = true; | |
| 266 | + } | |
| 267 | + | |
| 268 | + if ( ! $addTooltip ) { | |
| 269 | + return $price; | |
| 270 | + } | |
| 271 | + | |
| 272 | + $pricingRule = PriceManager::getPricingRule( $_product->get_id() ); | |
| 273 | + | |
| 274 | + if ( ! empty( $pricingRule->getRules() ) ) { | |
| 275 | + | |
| 276 | + return $price . $this->getContainer()->getFileManager()->renderTemplate( 'frontend/tooltip.php', array( | |
| 277 | + 'color' => $this->getContainer()->getSettings()->get( 'tooltip_color', '#3858e9' ), | |
| 278 | + 'size' => $this->getContainer()->getSettings()->get( 'tooltip_size', 15 ) . 'px', | |
| 279 | + ) ); | |
| 280 | + } | |
| 281 | + | |
| 282 | + } | |
| 283 | + | |
| 284 | + return $price; | |
| 285 | + } | |
| 286 | + | |
| 287 | + /** | |
| 288 | + * Fired when user chooses a variation. Renders tiered pricing table for the variation | |
| 289 | + * | |
| 290 | + * @throws Exception | |
| 291 | + * @global WP_Post $post . | |
| 292 | + */ | |
| 293 | + public function getVariationPricingTable() { | |
| 294 | + | |
| 295 | + $product_id = isset( $_POST['variation_id'] ) ? sanitize_text_field( $_POST['variation_id'] ) : false; | |
| 296 | + $nonce = isset( $_POST['nonce'] ) ? sanitize_key( $_POST['nonce'] ) : false; | |
| 297 | + $displayContext = isset( $_POST['display_context'] ) ? sanitize_text_field( $_POST['display_context'] ) : 'product-page'; | |
| 298 | + | |
| 299 | + $renderSettings = apply_filters( 'tiered_pricing_table/frontend/variation_render_settings', array(), | |
| 300 | + $product_id, $displayContext ); | |
| 301 | + | |
| 302 | + // Some cache plugins may cache the nonce, so we need to leave the ability to disable nonce verification. | |
| 303 | + // Checking nonce is not critical here. | |
| 304 | + $verifyNonce = apply_filters( 'tiered_pricing_table/frontend/load_variation/verify_nonce', false, $nonce, | |
| 305 | + 'get_pricing_table' ); | |
| 306 | + | |
| 307 | + if ( $verifyNonce && ! wp_verify_nonce( $nonce, 'get_pricing_table' ) ) { | |
| 308 | + return; | |
| 309 | + } | |
| 310 | + | |
| 311 | + $product = wc_get_product( $product_id ); | |
| 312 | + | |
| 313 | + if ( $product ) { | |
| 314 | + $parentProduct = wc_get_product( $product->get_parent_id() ); | |
| 315 | + | |
| 316 | + if ( $parentProduct ) { | |
| 317 | + PricingTable::getInstance()->renderPricingTableHTML( $parentProduct, $product, $renderSettings ); | |
| 318 | + } | |
| 319 | + | |
| 320 | + } | |
| 321 | + | |
| 322 | + } | |
| 305 | 323 | } |