| @@ -1,48 +1,242 @@ | ||
| 1 | -<?php namespace TierPricingTable\Frontend; | |
| 1 | +<?php | |
| 2 | 2 | |
| 3 | -use TierPricingTable\Core\ServiceContainerTrait; | |
| 4 | -use TierPricingTable\TierPricingTablePlugin; | |
| 5 | -use WP_Post; | |
| 3 | +namespace TierPricingTable\Frontend; | |
| 6 | 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 ; | |
| 7 | 13 | /** |
| 8 | 14 | * Class Frontend |
| 9 | 15 | * |
| 10 | 16 | * @package TierPricingTable\Frontend |
| 11 | 17 | */ |
| 12 | -class Frontend { | |
| 13 | - | |
| 14 | - use ServiceContainerTrait; | |
| 15 | - | |
| 16 | - public function __construct() { | |
| 17 | - | |
| 18 | - new PricingTableShortcode(); | |
| 19 | - | |
| 20 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueueAssets' ), 10, 1 ); | |
| 21 | - } | |
| 22 | - | |
| 23 | - public function enqueueAssets() { | |
| 24 | - wp_enqueue_script( 'tiered-pricing-table-front-js', | |
| 25 | - $this->getContainer()->getFileManager()->locateJSAsset( 'frontend/product-tiered-pricing-table' ), | |
| 26 | - array( 'jquery' ), TierPricingTablePlugin::VERSION ); | |
| 27 | - | |
| 28 | - wp_enqueue_style( 'tiered-pricing-table-front-css', | |
| 29 | - $this->getContainer()->getFileManager()->locateCSSAsset( 'frontend/main.css' ), null, | |
| 30 | - TierPricingTablePlugin::VERSION ); | |
| 31 | - | |
| 32 | - wp_localize_script( 'tiered-pricing-table-front-js', 'tieredPricingGlobalData', [ | |
| 33 | - 'version' => TierPricingTablePlugin::VERSION, | |
| 34 | - 'loadVariationTieredPricingNonce' => wp_create_nonce( 'get_pricing_table' ), | |
| 35 | - 'isPremium' => ! tpt_fs()->can_use_premium_code() ? 'no' : 'yes', | |
| 36 | - 'currencyOptions' => [ | |
| 37 | - 'currency_symbol' => get_woocommerce_currency_symbol(), | |
| 38 | - 'decimal_separator' => wc_get_price_decimal_separator(), | |
| 39 | - 'thousand_separator' => wc_get_price_thousand_separator(), | |
| 40 | - 'decimals' => wc_get_price_decimals(), | |
| 41 | - 'price_format' => get_woocommerce_price_format(), | |
| 42 | - 'trim_zeros' => apply_filters( 'woocommerce_price_trim_zeros', false ), | |
| 43 | - ], | |
| 44 | - 'supportedVariableProductTypes' => TierPricingTablePlugin::getSupportedVariableProductTypes(), | |
| 45 | - 'supportedSimpleProductTypes' => TierPricingTablePlugin::getSupportedSimpleProductTypes(), | |
| 46 | - ] ); | |
| 47 | - } | |
| 48 | -} | |
| 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 | +} | |