PluginProbe
Tiered Pricing Table for WooCommerce / 6.1.0
Tiered Pricing Table for WooCommerce v6.1.0
8.0.2 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 All 91 releases
tier-pricing-table / src / Admin / ProductPage / TieredPricingTab.php

TieredPricingTab.php in Tiered Pricing Table for WooCommerce 6.1.0, at src/Admin/ProductPage/TieredPricingTab.php

261 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace TierPricingTable\Admin\ProductPage;
4
5 use TierPricingTable\Core\ServiceContainerTrait;
6 use TierPricingTable\PriceManager;
7 use TierPricingTable\Forms\MinimumOrderQuantityForm;
8 use TierPricingTable\Forms\TieredPricingRulesForm;
9 use TierPricingTable\TierPricingTablePlugin;
10 /**
11 * Class ProductManager
12 *
13 * @package TierPricingTable\Admin\Product
14 */
15 class TieredPricingTab {
16 use ServiceContainerTrait;
17 public function __construct() {
18 // Tiered Pricing Product Tab
19 add_filter(
20 'woocommerce_product_data_tabs',
21 array($this, 'register'),
22 99,
23 1
24 );
25 add_action( 'woocommerce_product_data_panels', array($this, 'render') );
26 add_action( 'woocommerce_process_product_meta', array($this, 'save') );
27 }
28
29 /**
30 * Add tiered pricing tab to woocommerce product tabs
31 *
32 * @param array $productTabs
33 *
34 * @return array
35 */
36 public function register( $productTabs ) : array {
37 $productTabs['tiered-pricing-tab'] = array(
38 'label' => __( 'Tiered Pricing', 'tier-pricing-table' ),
39 'target' => 'tiered-pricing-data',
40 'class' => (function () {
41 $types = array_merge( TierPricingTablePlugin::getSupportedSimpleProductTypes(), TierPricingTablePlugin::getSupportedVariableProductTypes() );
42 return array_map( function ( $type ) {
43 return 'show_if_' . $type;
44 }, $types );
45 })(),
46 );
47 return $productTabs;
48 }
49
50 /**
51 * Render content for the tiered pricing tab
52 */
53 public function render() {
54 global $post;
55 ?>
56 <div id="tiered-pricing-data" class="panel woocommerce_options_panel">
57
58 <?php
59 if ( !tpt_fs()->can_use_premium_code() ) {
60 $this->renderUpgradeNotice();
61 }
62 if ( tpt_fs()->can_use_premium_code() && !tpt_fs()->is_premium() ) {
63 $this->getContainer()->getFileManager()->includeTemplate( 'admin/banners/free-version-used-premium-available.php', array(
64 'is_product' => true,
65 ) );
66 }
67 do_action( 'tiered_pricing_table/admin/pricing_tab_begin', $post->ID );
68 ?>
69
70 <div class="hidden show_if_variable options_group">
71 <?php
72 $type = PriceManager::getPricingType( $post->ID, 'fixed', 'edit' );
73 $percentageRules = PriceManager::getPercentagePriceRules( $post->ID, 'edit' );
74 $fixedRules = PriceManager::getFixedPriceRules( $post->ID, 'edit' );
75 TieredPricingRulesForm::render(
76 $post->ID,
77 null,
78 null,
79 $type,
80 $percentageRules,
81 $fixedRules,
82 '_variable'
83 );
84 ?>
85 </div>
86
87 <div class="options_group">
88 <?php
89 $min = PriceManager::getProductQtyMin( $post->ID, 'edit' );
90 MinimumOrderQuantityForm::render( null, null, $min );
91 do_action( 'tiered_pricing_table/admin/after_minimum_order_quantity_field', $post->ID, null );
92 ?>
93 </div>
94
95 <?php
96 do_action( 'tiered_pricing_table/admin/before_advance_product_options', $post->ID );
97 ?>
98
99 <div class="tiered_pricing_tab_product_advance_options">
100 <div class="tiered_pricing_tab_product_advance_options__header">
101 <h4>
102 <span class="dashicons dashicons-admin-settings"></span>
103 <?php
104 esc_html_e( 'Additional options', 'tier-pricing-table' );
105 ?>
106 </h4>
107 <div>
108 <span class="tiered_pricing_arrow_down">â–¼</span>
109 <span class="tiered_pricing_arrow_up">â–²</span>
110 </div>
111 </div>
112
113 <div class="tiered_pricing_tab_product_advance_options__content">
114
115 <?php
116 $availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts();
117 $availableTemplates = array_merge( array(
118 'default' => __( 'Default' ),
119 ), $availableTemplates );
120 woocommerce_wp_select( array(
121 'id' => '_tiered_pricing_template',
122 'value' => self::getProductTemplate( $post->ID ),
123 'options' => $availableTemplates,
124 'label' => __( 'Display as (layout)', 'tier-pricing-table' ),
125 'description' => __( 'Specify the layout of the tiered pricing for the product. Leave the default to use global settings.', 'tier-pricing-table' ),
126 'default' => 'default',
127 'desc_tip' => true,
128 ) );
129 ?>
130
131 <p class="form-field">
132 <label>
133 <?php
134 esc_html_e( 'Unit label', 'tier-pricing-table' );
135 ?>
136 </label>
137
138 <?php
139 $productBaseUnitName = self::getProductBaseUnitName( $post->ID );
140 ?>
141
142 <input type="text"
143 value="<?php
144 echo esc_attr( $productBaseUnitName['singular'] );
145 ?>"
146 placeholder="<?php
147 esc_attr_e( 'Singular (e.g. piece)', 'tier-pricing-table' );
148 ?>"
149 style="width: 24%; margin-right: 20px;"
150 name="_tiered_pricing_base_unit_name[singular]">
151
152 <input type="text"
153 value="<?php
154 echo esc_attr( $productBaseUnitName['plural'] );
155 ?>"
156 placeholder="<?php
157 esc_attr_e( 'Plural (e.g. pieces)', 'tier-pricing-table' );
158 ?>"
159 style="width: 24%"
160 name="_tiered_pricing_base_unit_name[plural]">
161 </p>
162
163 <div style="padding: 0 20px 10px 162px; margin-bottom: 10px">
164 <?php
165 esc_html_e( 'For example: pieces, boxes, bottles, packs, etc. It will be shown next to quantities. Leave empty to use global settings.', 'tier-pricing-table' );
166 ?>
167 </div>
168
169 <?php
170 do_action( 'tiered_pricing_table/admin/advance_product_options', $post->ID );
171 ?>
172 </div>
173 </div>
174
175 <?php
176 do_action( 'tiered_pricing_table/admin/pricing_tab_end', $post->ID );
177 ?>
178 </div>
179 <?php
180 }
181
182 protected function renderUpgradeNotice() {
183 ?>
184 <div
185 style="display:flex; align-items:center; justify-content: space-between; background: #fafafa; padding: 10px; margin: 0 0 20px;border-bottom: 1px solid #eee;">
186 <div>
187 <span style="color: red;">
188 🚀
189 <?php
190 esc_html_e( 'Upgrade your plan to unlock all great features.', 'tier-pricing-table' );
191 ?>
192
193 </span>
194 </div>
195
196 <div>
197 <a target="_blank" class="button button-primary"
198 href="<?php
199 echo esc_attr( tpt_fs()->get_upgrade_url() );
200 ?>">
201 <?php
202 esc_html_e( 'Upgrade', 'tier-pricing-table' );
203 ?>
204 </a>
205 </div>
206
207 </div>
208 <?php
209 }
210
211 /**
212 * Save tiered pricing tab data
213 *
214 * @param $productId
215 */
216 public function save( $productId ) {
217 if ( wp_verify_nonce( true, true ) ) {
218 // as phpcs comments at Woo is not available, we have to do such a trash
219 $woo = 'Woo, please add ignoring comments to your phpcs checker';
220 }
221 $template = ( isset( $_POST['_tiered_pricing_template'] ) ? sanitize_text_field( $_POST['_tiered_pricing_template'] ) : 'default' );
222 self::updateProductTemplate( $productId, sanitize_text_field( $template ) );
223 $baseUnitName = ( isset( $_POST['_tiered_pricing_base_unit_name'] ) ? array_map( 'sanitize_text_field', $_POST['_tiered_pricing_base_unit_name'] ) : array() );
224 self::updateProductBaseUnitName( $productId, $baseUnitName );
225 }
226
227 public static function getProductTemplate( $productId ) {
228 $template = get_post_meta( $productId, '_tiered_pricing_template', true );
229 $availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts();
230 return ( array_key_exists( $template, $availableTemplates ) ? $template : 'default' );
231 }
232
233 public static function updateProductTemplate( $productId, $template ) {
234 $availableTemplates = TierPricingTablePlugin::getAvailablePricingLayouts();
235 $template = ( array_key_exists( $template, $availableTemplates ) ? $template : 'default' );
236 if ( 'default' !== $template ) {
237 update_post_meta( $productId, '_tiered_pricing_template', $template );
238 } else {
239 delete_post_meta( $productId, '_tiered_pricing_template' );
240 }
241 }
242
243 public static function getProductBaseUnitName( $productId ) : array {
244 $_baseUnitName = (array) get_post_meta( $productId, '_tiered_pricing_base_unit_name', true );
245 $baseUnitName['singular'] = $_baseUnitName['singular'] ?? null;
246 $baseUnitName['plural'] = $_baseUnitName['plural'] ?? null;
247 return $baseUnitName;
248 }
249
250 public static function updateProductBaseUnitName( $productId, array $unitNames ) {
251 $baseUnitName['singular'] = $unitNames['singular'] ?? null;
252 $baseUnitName['plural'] = $unitNames['plural'] ?? null;
253 if ( empty( $baseUnitName['singular'] ) && empty( $baseUnitName['plural'] ) ) {
254 delete_post_meta( $productId, '_tiered_pricing_base_unit_name' );
255 } else {
256 update_post_meta( $productId, '_tiered_pricing_base_unit_name', $baseUnitName );
257 }
258 }
259
260 }
261