| 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 |
} |
| 157 |
} |