PluginProbe
Tiered Pricing Table for WooCommerce / trunk
Tiered Pricing Table for WooCommerce vtrunk
7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 All 90 releases
← All changes | src/Frontend/Frontend.php +39 -149 1.1trunk View file →
@@ -1,12 +1,9 @@
1 1 <?php namespace TierPricingTable\Frontend;
2 2
3 -use TierPricingTable\Admin\Settings;
4 -use Premmerce\SDK\V2\FileManager\FileManager;
5 -use TierPricingTable\PriceManager;
3 +use TierPricingTable\Core\ServiceContainerTrait;
4 +use TierPricingTable\TierPricingTablePlugin;
6 5 use WP_Post;
7 -use WC_Cart;
8 -use WC_Product;
9 6
10 7 /**
11 8 * Class Frontend
12 9 *
@@ -11,148 +8,41 @@
11 8 * Class Frontend
12 9 *
13 10 * @package TierPricingTable\Frontend
14 11 */
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 -
38 - if($this->settings->get('display') == 'yes'){
39 - // Render price table
40 - add_action($this->settings->get('position_hook'), 'render_price_quantity_table', -999);
41 - }
42 -
43 - // Get table for variation
44 - add_action('wc_ajax_get_price_table', [$this, 'getPriceTableVariation']);
45 -
46 - add_action('wp_enqueue_scripts', [$this, 'enqueueAssets']);
47 -
48 - add_action('woocommerce_before_calculate_totals', [$this, 'calculateTotals']);
49 -
50 - if($this->settings->get('display') == 'yes' && $this->settings->get('display_type') == 'tooltip'){
51 - add_filter('woocommerce_get_price_html', [$this, 'renderTooltip'], 1, 2);
52 - }
53 - }
54 -
55 - /**
56 - * @param string $price
57 - * @param WC_Product $instance
58 - * @return string
59 - */
60 - public function renderTooltip($price, $instance)
61 - {
62 - $current_url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
63 - $current_product_id = url_to_postid($current_url);
64 -
65 - if(get_post_meta($instance->get_id(), '_price_rules', true)){
66 - if($instance->is_type('variation') && $instance->get_parent_id() === $current_product_id
67 - || (is_product() && $instance->is_type('simple') && $current_product_id == $instance->get_id())){
68 - return $price . $this->fileManager->renderTemplate('frontend/tooltip.php',
69 - [
70 - 'color' => $this->settings->get('tooltip_color'),
71 - 'size' => $this->settings->get('tooltip_size') . 'px',
72 - ]
73 - );
74 - }
75 - }
76 -
77 - return $price;
78 - }
79 - /**
80 - * Change price by quantity rules
81 - *
82 - * @param WC_Cart $cart
83 - */
84 - public function calculateTotals($cart)
85 - {
86 - foreach ($cart->cart_contents as $key => $product_item) {
87 - $product_id = !empty($product_item['variation_id']) ? $product_item['variation_id'] : $product_item['product_id'];
88 - $new_price = PriceManager::getPriceByRules($product_item['quantity'], $product_id);
89 -
90 - if ($new_price !== false) {
91 - $product_item['data']->set_price($new_price);
92 - }
93 - }
94 - }
95 -
96 - /**
97 - * Enqueue assets at simple product and variation product page.
98 - * @global WP_Post $post .
99 - */
100 - public function enqueueAssets()
101 - {
102 - global $post;
103 -
104 - if (is_product()) {
105 - $product = wc_get_product($post->ID);
106 -
107 - if ($product->is_type('variable') || $product->is_type('simple')) {
108 - wp_enqueue_script('jquery');
109 - wp_enqueue_script('jquery-ui-tooltip');
110 - wp_enqueue_script('tier-pricing-table-front-js',
111 - $this->fileManager->locateAsset('frontend/product-tier-pricing-table.js'), 'jquery');
112 - wp_enqueue_style('tier-pricing-table-front-css', $this->fileManager->locateAsset('frontend/main.css'));
113 - wp_localize_script('tier-pricing-table-front-js', 'tierPricingTable', ['product_type' => $product->get_type(), 'settings' => $this->settings->getAll()]);
114 - }
115 - }
116 - }
117 -
118 - /**
119 - * Fired when user choose some variation. Render price rules table for it if it exists
120 - * @global WP_Post $post .
121 - */
122 - public function getPriceTableVariation()
123 - {
124 - $variation_id = isset($_POST['product_id']) ? $_POST['product_id'] : false;
125 - if ($variation_id) {
126 - $this->_renderPriceTable($variation_id);
127 - }
128 - }
129 -
130 - /**
131 - * @param int $variation_id
132 - */
133 - public function _renderPriceTable($variation_id = null)
134 - {
135 - global $post;
136 -
137 - $settings = $this->settings->getAll();
138 -
139 - $product = wc_get_product($post->ID);
140 - $product_id = !is_null($variation_id) ? $variation_id : $product->get_id();
141 -
142 - if ($product && !$product->is_type('simple') && !$product->is_type('variable')) {
143 - return;
144 - }
145 -
146 - $rules = PriceManager::getPriceRules($product_id);
147 - $real_price = !is_null($variation_id) ? wc_get_product($variation_id)->get_price() : $product->get_price();
148 -
149 - if (!empty($rules)) {
150 - $this->fileManager->includeTemplate('frontend/price-table.php', [
151 - 'price_rules' => $rules,
152 - 'real_price' => $real_price,
153 - 'product_id' => $product_id,
154 - 'settings' => $settings
155 - ]);
156 - }
157 - }
158 -}
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 +}