2025-12-21 04:56:50 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Frontend display for tier and package pricing
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (!defined('ABSPATH')) {
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class WC_TPP_Frontend {
|
|
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
|
|
|
|
|
add_action('woocommerce_before_add_to_cart_button', array($this, 'display_pricing_table_before'), 20);
|
|
|
|
|
add_action('woocommerce_after_add_to_cart_button', array($this, 'display_pricing_table_after'), 10);
|
|
|
|
|
add_action('woocommerce_single_product_summary', array($this, 'display_pricing_table_after_price'), 15);
|
2025-12-21 15:54:04 +01:00
|
|
|
add_action('woocommerce_before_add_to_cart_quantity', array($this, 'maybe_hide_quantity_input'));
|
2025-12-21 04:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function enqueue_scripts() {
|
|
|
|
|
if (is_product()) {
|
|
|
|
|
wp_enqueue_style('wc-tpp-frontend', WC_TPP_PLUGIN_URL . 'assets/css/frontend.css', array(), WC_TPP_VERSION);
|
|
|
|
|
wp_enqueue_script('wc-tpp-frontend', WC_TPP_PLUGIN_URL . 'assets/js/frontend.js', array('jquery'), WC_TPP_VERSION, true);
|
|
|
|
|
|
|
|
|
|
// Localize script with currency settings
|
|
|
|
|
wp_localize_script('wc-tpp-frontend', 'wcTppData', array(
|
|
|
|
|
'currency_symbol' => esc_js(get_woocommerce_currency_symbol()),
|
|
|
|
|
'currency_position' => esc_js(get_option('woocommerce_currency_pos', 'left')),
|
|
|
|
|
'price_decimals' => absint(wc_get_price_decimals()),
|
|
|
|
|
'price_decimal_separator' => esc_js(wc_get_price_decimal_separator()),
|
|
|
|
|
'price_thousand_separator' => esc_js(wc_get_price_thousand_separator())
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function display_pricing_table_before() {
|
|
|
|
|
if (get_option('wc_tpp_display_position') === 'before_add_to_cart') {
|
|
|
|
|
$this->display_pricing_table();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function display_pricing_table_after() {
|
|
|
|
|
if (get_option('wc_tpp_display_position') === 'after_add_to_cart') {
|
|
|
|
|
$this->display_pricing_table();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function display_pricing_table_after_price() {
|
|
|
|
|
if (get_option('wc_tpp_display_position') === 'after_price') {
|
|
|
|
|
$this->display_pricing_table();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 15:54:04 +01:00
|
|
|
public function maybe_hide_quantity_input() {
|
|
|
|
|
global $product;
|
|
|
|
|
|
|
|
|
|
if (!$product || !is_a($product, 'WC_Product')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product_id = $product->get_id();
|
|
|
|
|
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
|
|
|
|
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
|
|
|
|
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
|
|
|
|
|
|
|
|
|
// Hide quantity input if restriction is enabled and packages exist
|
|
|
|
|
if (($global_restrict || $product_restrict) && !empty($packages)) {
|
|
|
|
|
echo '<style>.quantity { display: none !important; }</style>';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 04:56:50 +01:00
|
|
|
public function display_pricing_table() {
|
|
|
|
|
global $product;
|
|
|
|
|
|
|
|
|
|
if (!$product || !is_a($product, 'WC_Product') || get_option('wc_tpp_display_table') !== 'yes') {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$product_id = $product->get_id();
|
|
|
|
|
$tiers = get_post_meta($product_id, '_wc_tpp_tiers', true);
|
|
|
|
|
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
2025-12-21 15:54:04 +01:00
|
|
|
$global_restrict = get_option('wc_tpp_restrict_package_quantities', 'no') === 'yes';
|
|
|
|
|
$product_restrict = get_post_meta($product_id, '_wc_tpp_restrict_to_packages', true) === 'yes';
|
2025-12-21 04:56:50 +01:00
|
|
|
|
|
|
|
|
if (empty($tiers) && empty($packages)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WC_TPP_Template_Loader::get_instance()->display('frontend/pricing-table.twig', array(
|
|
|
|
|
'product' => $product,
|
|
|
|
|
'tiers' => $tiers,
|
2025-12-21 15:54:04 +01:00
|
|
|
'packages' => $packages,
|
|
|
|
|
'restrict_to_packages' => $global_restrict || $product_restrict
|
2025-12-21 04:56:50 +01:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function get_tier_price($product_id, $quantity) {
|
|
|
|
|
$tiers = get_post_meta($product_id, '_wc_tpp_tiers', true);
|
|
|
|
|
|
|
|
|
|
if (empty($tiers) || !is_array($tiers)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$applicable_price = null;
|
|
|
|
|
foreach ($tiers as $tier) {
|
|
|
|
|
if ($quantity >= $tier['min_qty']) {
|
|
|
|
|
$applicable_price = $tier['price'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $applicable_price;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function get_package_price($product_id, $quantity) {
|
|
|
|
|
$packages = get_post_meta($product_id, '_wc_tpp_packages', true);
|
|
|
|
|
|
|
|
|
|
if (empty($packages) || !is_array($packages)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($packages as $package) {
|
|
|
|
|
if ($quantity == $package['qty']) {
|
|
|
|
|
return $package['price'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new WC_TPP_Frontend();
|