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