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
tier-pricing-table / src / Addons / TierLabels / Admin / TierLabelsAdmin.php

TierLabelsAdmin.php in Tiered Pricing Table for WooCommerce trunk, at src/Addons/TierLabels/Admin/TierLabelsAdmin.php

226 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\TierLabels\Admin;
2
3 use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule;
4 use TierPricingTable\Addons\RoleBasedPricing\RoleBasedPricingRule;
5 use TierPricingTable\Addons\TierLabels\TierLabelsManager;
6 use TierPricingTable\Forms\Form;
7 use TierPricingTable\PricingRule;
8
9 class TierLabelsAdmin {
10
11 /**
12 * @var TierLabelsManager
13 */
14 private $manager;
15
16 public function __construct( TierLabelsManager $manager ) {
17 $this->manager = $manager;
18
19 add_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs', [ $this, 'renderInputField' ], 999,
20 7 );
21 add_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/after_pricing_type',
22 [ $this, 'renderFirstRowField' ], 10, 4 );
23 add_action( 'tiered_pricing_table/admin/components/tiered_pricing_rules_form/get_from_request',
24 [ $this, 'saveField' ], 10, 6 );
25
26 add_filter( 'tiered_pricing_table/price/pricing_rule', array( $this, 'addLabelDataToPricingRule' ), 1, 2 );
27
28 add_filter( 'tiered_pricing_table/role_based_pricing/after_adjusting_pricing_rule',
29 function ( PricingRule $pricingRule, RoleBasedPricingRule $roleBasedPricingRule ) {
30 return $this->addLabelDataToPricingRule( $pricingRule, $pricingRule->getProductId(),
31 $roleBasedPricingRule->getRole() );
32 }, 2, 3 );
33
34 add_filter( 'tiered_pricing_table/global_pricing/after_adjusting_pricing_rule', function (
35 PricingRule $pricingRule,
36 GlobalPricingRule $globalPricingRule
37 ) {
38 return $this->addLabelDataToPricingRule( $pricingRule, $globalPricingRule->getId(), null, false );
39 }, 3, 3 );
40
41 add_filter( 'tiered_pricing_table/admin/tiered_pricing_rules_form/inputs_width', function ( $defaultWidth ) {
42
43 $labels = $this->manager->getLabels();
44
45 if ( empty( $labels ) ) {
46 return $defaultWidth;
47 }
48
49 return min( 100, $defaultWidth + 20 );
50 } );
51 }
52
53 public function renderInputField(
54 $entityId,
55 $amount,
56 $role,
57 $loop,
58 $custom_prefix,
59 $type
60 ) {
61 $labels = $this->manager->getLabels();
62
63 if ( empty( $labels ) ) {
64 return;
65 }
66
67 $value = $this->getValueForAmount( $amount, $entityId, $role, $type );
68 $name = Form::getFieldName( 'tier_label_' . $type, $role, $loop, $custom_prefix );
69
70 ?>
71 <select name="<?php echo esc_attr( $name ); ?>[]" style="width: 35%;">
72 <option value=""><?php esc_html_e( 'No label', 'tier-pricing-table' ); ?></option>
73 <?php foreach ( $labels as $label ) : ?>
74 <option value="<?php echo esc_attr( $label->getId() ); ?>" <?php selected( $value,
75 $label->getId() ); ?>>
76 <?php echo esc_html( $label->getTitle() ); ?>
77 </option>
78 <?php endforeach; ?>
79 </select>
80 <?php
81 }
82
83 public function renderFirstRowField(
84 $entityId,
85 $role,
86 $loop,
87 $custom_prefix
88 ) {
89 $labels = $this->manager->getLabels();
90
91 if ( empty( $labels ) ) {
92 return;
93 }
94
95 $value = $this->getFirstRowValue( $entityId, 'fixed', $role );
96
97 $options = [ '' => __( 'No label', 'tier-pricing-table' ) ];
98 foreach ( $labels as $label ) {
99 $options[ $label->getId() ] = $label->getTitle();
100 }
101
102 woocommerce_wp_select( array(
103 'id' => Form::getFieldName( 'tier_label_first_row', $role, $loop, $custom_prefix ),
104 'label' => __( 'Tier label (regular price)', 'tier-pricing-table' ),
105 'value' => $value,
106 'options' => $options,
107 'wrapper_class' => is_null( $loop ) ? 'tiered-pricing-form-block' : 'tiered-pricing-form-variation-block',
108 ) );
109 }
110
111 public function saveField(
112 $entityId,
113 $role,
114 $loop,
115 $customPrefix,
116 $data,
117 $request
118 ) {
119 $_value = array(
120 'fixed' => array(),
121 'percentage' => array(),
122 );
123
124 foreach ( array( 'fixed', 'percentage' ) as $pricingType ) {
125
126 $value = Form::getFieldValue( 'tier_label_' . $pricingType, $role, $loop, $customPrefix, $request );
127
128 $value = is_array( $value ) ? $value : array();
129 $amounts = 'fixed' === $pricingType ? $data['fixed_quantities'] : $data['percentage_quantities'];
130
131 foreach ( $value as $key => $itemValue ) {
132 if ( ! empty( $amounts[ $key ] ) ) {
133 $_value[ $pricingType ][ $amounts[ $key ] ] = sanitize_text_field( $itemValue );
134 }
135 }
136
137 $firstRowValue = Form::getFieldValue( 'tier_label_first_row', $role, $loop, $customPrefix, $request );
138
139 if ( ! Form::isEmpty( $firstRowValue ) ) {
140 $_value[ $pricingType ]['first_row'] = sanitize_text_field( $firstRowValue );
141 }
142 }
143
144 update_post_meta( $entityId, $this->getMetaKey( $role ), $_value );
145 }
146
147 public function getValue( $entityId, $type, $role = null ): array {
148 $value = (array) get_post_meta( $entityId, $this->getMetaKey( $role ), true );
149
150 return ! empty( $value[ $type ] ) ? (array) $value[ $type ] : array();
151 }
152
153 public function getValueForAmount( $amount, $entityId, $role, $type ) {
154 if ( ! $amount ) {
155 return '';
156 }
157
158 $amount = intval( $amount );
159
160 if ( $amount < 2 ) {
161 return '';
162 }
163
164 $value = $this->getValue( $entityId, $type, $role );
165
166 if ( array_key_exists( $amount, $value ) ) {
167 return $value[ $amount ];
168 }
169
170 return '';
171 }
172
173 public function getFirstRowValue( $entityId, $type, $role ) {
174 $value = $this->getValue( $entityId, $type, $role );
175
176 if ( array_key_exists( 'first_row', $value ) ) {
177 return $value['first_row'];
178 }
179
180 return '';
181 }
182
183 public function addLabelDataToPricingRule(
184 PricingRule $pricingRule,
185 $entityId,
186 $role = null,
187 $isProductRule = true
188 ): PricingRule {
189
190 $percentageValues = $this->getValue( $entityId, 'percentage', $role );
191 $fixedValues = $this->getValue( $entityId, 'fixed', $role );
192
193 if ( empty( $percentageValues ) || empty( $fixedValues ) ) {
194 if ( $isProductRule ) {
195 $product = wc_get_product( $entityId );
196 if ( $product && $product->get_parent_id() ) {
197 $percentageValues = $percentageValues ?: $this->getValue( $product->get_parent_id(), 'percentage',
198 $role );
199 $fixedValues = $fixedValues ?: $this->getValue( $product->get_parent_id(), 'fixed', $role );
200 }
201 }
202 }
203
204 $percentageValues = array_filter( $percentageValues, function ( $value ) {
205 return ! is_null( $this->manager->getLabel( $value ) );
206 } );
207
208 $fixedValues = array_filter( $fixedValues, function ( $value ) {
209 return ! is_null( $this->manager->getLabel( $value ) );
210 } );
211
212 $firstRowValue = $this->getFirstRowValue( $entityId, 'fixed', $role );
213
214 $pricingRule->data['tier_labels']['percentage'] = $percentageValues;
215 $pricingRule->data['tier_labels']['fixed'] = $fixedValues;
216
217 return $pricingRule;
218 }
219
220 public function getMetaKey( $role = null ): string {
221 $rolePrefix = $role ? '_' . $role : '';
222
223 return $rolePrefix . '_tiered_price_tier_labels';
224 }
225 }
226