fileManager = $fileManager; $this->settings = $settings; $this->licence = $licence; $this->initManagers(); if ( $this->settings->get( 'display' ) == 'yes' ) { // Render price table add_action( $this->settings->get( 'position_hook' ), [ $this, 'displayPricingTable' ], -999 ); } // Wrap price add_action( 'woocommerce_get_price_html', [ $this, 'wrapPrice' ], 10, 2 ); // Get table for variation add_action( 'wc_ajax_get_price_table', [ $this, 'getPriceTableVariation' ], 10, 1 ); // Enqueue frontend assets add_action( 'wp_enqueue_scripts', [ $this, 'enqueueAssets' ], 10, 1 ); // Render tooltip near product price if selected display type is "tooltip" if ( $this->settings->get( 'display' ) === 'yes' && $this->settings->get( 'display_type' ) === 'tooltip' ) { add_filter( 'woocommerce_get_price_html', [ $this, 'renderTooltip' ], 10, 2 ); } } /** * */ protected function initManagers() { $this->cartPriceManager = new CartPriceManager( $this->settings ); } /** * Display table at frontend */ public function displayPricingTable() { global $post ; if ( !$post ) { return; } $product = wc_get_product( $post->ID ); if ( $product ) { if ( $product->is_type( 'simple' ) ) { $this->renderPricingTable( $product->get_id() ); } elseif ( $product->is_type( 'variable' ) ) { echo '
' ; } } } /** * Wrap product price for managing it by JS * * @param string $price_html * @param WC_Product $product * * @return string */ public function wrapPrice( $price_html, $product ) { if ( is_single() && ($product->is_type( 'simple' ) || $product->is_type( 'variation' )) ) { return '' . $price_html . ''; } return $price_html; } /** * Render tooltip near product price if selected display type is "tooltip" * * @param string $price * @param WC_Product $_product * * @return string */ public function renderTooltip( $price, $_product ) { if ( is_product() ) { $page_product_id = get_queried_object_id(); if ( $_product->is_type( 'variation' ) && $_product->get_parent_id() === $page_product_id || is_product() && $_product->is_type( 'simple' ) && $page_product_id === $_product->get_id() ) { $rules = PriceManager::getPriceRules( $_product->get_id() ); if ( !empty($rules) ) { return $price . $this->fileManager->renderTemplate( 'frontend/tooltip.php', [ 'color' => $this->settings->get( 'tooltip_color' ), 'size' => $this->settings->get( 'tooltip_size' ) . 'px', ] ); } } } return $price; } /** * Enqueue assets at simple product and variation product page. * * @global WP_Post $post . */ public function enqueueAssets() { global $post ; if ( is_product() ) { $product = wc_get_product( $post->ID ); if ( $product->is_type( 'variable' ) || $product->is_type( 'simple' ) ) { wp_enqueue_script( 'jquery' ); wp_enqueue_script( 'jquery-ui-tooltip' ); wp_enqueue_script( 'tier-pricing-table-front-js', $this->fileManager->locateAsset( 'frontend/product-tier-pricing-table.js' ), 'jquery', TierPricingTablePlugin::VERSION ); wp_enqueue_style( 'tier-pricing-table-front-css', $this->fileManager->locateAsset( 'frontend/main.css' ), null, TierPricingTablePlugin::VERSION ); wp_localize_script( 'tier-pricing-table-front-js', 'tieredPricingTable', [ 'product_type' => $product->get_type(), 'settings' => $this->settings->getAll(), ] ); } } } /** * Fired when user choose some variation. Render price rules table for it if it exists * @global WP_Post $post . */ public function getPriceTableVariation() { $product_id = ( isset( $_POST['variation_id'] ) ? $_POST['variation_id'] : false ); $product = wc_get_product( $product_id ); if ( $product && $product->is_type( 'variation' ) ) { $this->renderPricingTable( $product->get_parent_id(), $product->get_id() ); } } /** * Main function for rendering pricing table for product * * @param int $product_id * @param int $variation_id */ public function renderPricingTable( $product_id, $variation_id = null ) { $product = wc_get_product( $product_id ); $product_id = ( !is_null( $variation_id ) ? $variation_id : $product->get_id() ); // Exit if product is not valid if ( !$product || !($product->is_type( 'simple' ) || $product->is_type( 'variable' )) ) { return; } $rules = PriceManager::getPriceRules( $product_id ); $real_price = ( !is_null( $variation_id ) ? wc_get_product( $variation_id )->get_price() : $product->get_price() ); if ( !empty($rules) ) { $template = ( PriceManager::getPricingType( $product_id ) === 'percentage' ? 'price-table-percentage.php' : 'price-table-fixed.php' ); $this->fileManager->includeTemplate( 'frontend/' . $template, [ 'price_rules' => $rules, 'real_price' => $real_price, 'product_id' => $product_id, 'settings' => $this->settings->getAll(), ] ); } } }