| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\Integration; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT; |
| 4 |
use TierPricingTable\Addons\RequestAQuote\Models\RequestQuoteForm; |
| 5 |
use TierPricingTable\Forms\Form; |
| 6 |
use TierPricingTable\PricingRule; |
| 7 |
|
| 8 |
class PricingRulesIntegration { |
| 9 |
|
| 10 |
public function __construct() { |
| 11 |
// Unified Render for Quote Form Selector |
| 12 |
add_action( 'tiered_pricing_table/admin/tiered_pricing_rules_form/form_end', array( $this, 'renderFormSelect' ), |
| 13 |
10, 4 ); |
| 14 |
|
| 15 |
// Product Level Integration (Saving) |
| 16 |
add_action( 'woocommerce_process_product_meta', array( $this, 'saveProductFormSelect' ) ); |
| 17 |
|
| 18 |
// Variation Level Integration (Saving) |
| 19 |
add_action( 'woocommerce_save_product_variation', array( $this, 'saveVariationFormSelect' ), 10, 2 ); |
| 20 |
|
| 21 |
// Global Rules Integration (Saving) |
| 22 |
add_action( 'tiered_pricing_table/global_pricing/before_updating', array( $this, 'saveGlobalFormSelect' ), 10, |
| 23 |
2 ); |
| 24 |
|
| 25 |
// Role-based Integration (Saving) |
| 26 |
add_action( 'tiered_pricing_table/role_based_rules/save_role_based_rules', array( $this, 'saveRoleBasedMeta' ), |
| 27 |
10, 4 ); |
| 28 |
|
| 29 |
// Inject to PricingRule |
| 30 |
add_filter( 'tiered_pricing_table/price/pricing_rule', array( $this, 'addRequestAQuoteToPricingRule' ), 99, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
public function addRequestAQuoteToPricingRule( PricingRule $pricingRule, $productId ): PricingRule { |
| 34 |
|
| 35 |
if ( $pricingRule->provider === 'role-based' && ! empty( $pricingRule->providerData['role'] ) ) { |
| 36 |
$role = $pricingRule->providerData['role']; |
| 37 |
$formId = get_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id", true ); |
| 38 |
|
| 39 |
if ( ! $formId ) { |
| 40 |
$product = wc_get_product( $productId ); |
| 41 |
if ( $product && $product->is_type( 'variation' ) ) { |
| 42 |
$parentId = $product->get_parent_id(); |
| 43 |
$formId = get_post_meta( $parentId, "_{$role}__tier_pricing_table_quote_form_id", true ); |
| 44 |
if ( $formId ) { |
| 45 |
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId; |
| 46 |
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $parentId, |
| 47 |
"_{$role}__tier_pricing_table_quote_auto_open_quantity", true ); |
| 48 |
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $parentId, |
| 49 |
"_{$role}__tier_pricing_table_quote_integrated_label_text", true ); |
| 50 |
|
| 51 |
return $pricingRule; |
| 52 |
} |
| 53 |
} |
| 54 |
} else { |
| 55 |
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId; |
| 56 |
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $productId, |
| 57 |
"_{$role}__tier_pricing_table_quote_auto_open_quantity", true ); |
| 58 |
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $productId, |
| 59 |
"_{$role}__tier_pricing_table_quote_integrated_label_text", true ); |
| 60 |
|
| 61 |
return $pricingRule; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if ( $pricingRule->provider === 'global-rules' && ! empty( $pricingRule->providerData['rule_id'] ) ) { |
| 66 |
$ruleId = $pricingRule->providerData['rule_id']; |
| 67 |
$formId = get_post_meta( $ruleId, '_tier_pricing_table_quote_form_id', true ); |
| 68 |
if ( $formId ) { |
| 69 |
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId; |
| 70 |
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $ruleId, |
| 71 |
'_tier_pricing_table_quote_auto_open_quantity', true ); |
| 72 |
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $ruleId, |
| 73 |
'_tier_pricing_table_quote_integrated_label_text', true ); |
| 74 |
|
| 75 |
return $pricingRule; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$formId = get_post_meta( $productId, '_tier_pricing_table_quote_form_id', true ); |
| 80 |
if ( $formId ) { |
| 81 |
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId; |
| 82 |
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $productId, |
| 83 |
'_tier_pricing_table_quote_auto_open_quantity', true ); |
| 84 |
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $productId, |
| 85 |
'_tier_pricing_table_quote_integrated_label_text', true ); |
| 86 |
|
| 87 |
return $pricingRule; |
| 88 |
} |
| 89 |
|
| 90 |
$product = wc_get_product( $productId ); |
| 91 |
if ( $product && $product->is_type( 'variation' ) ) { |
| 92 |
$parentId = $product->get_parent_id(); |
| 93 |
$formId = get_post_meta( $parentId, '_tier_pricing_table_quote_form_id', true ); |
| 94 |
if ( $formId ) { |
| 95 |
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId; |
| 96 |
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $parentId, |
| 97 |
'_tier_pricing_table_quote_auto_open_quantity', true ); |
| 98 |
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $parentId, |
| 99 |
'_tier_pricing_table_quote_integrated_label_text', true ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $pricingRule; |
| 104 |
} |
| 105 |
|
| 106 |
public function renderFormSelect( $entityId, $role = null, $loop = null, $prefix = '' ) { |
| 107 |
|
| 108 |
$place = $this->classifyPlace( $entityId, $role, $loop ); |
| 109 |
|
| 110 |
if ( 'user' === $place ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$currentFormId = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_form_id", |
| 115 |
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_form_id', true ); |
| 116 |
|
| 117 |
if ( 'global' === $place ) { |
| 118 |
?> |
| 119 |
<style> |
| 120 |
|
| 121 |
select[name^="tiered_pricing_quote_"] { |
| 122 |
width: 75% !important; |
| 123 |
} |
| 124 |
</style> |
| 125 |
<div class="tpt-global-pricing-title"> |
| 126 |
<?php esc_html_e( 'Request a Quote', 'tiered-pricing-table' ); ?> |
| 127 |
</div> |
| 128 |
<?php |
| 129 |
$this->renderSelectField( $currentFormId, $entityId, $role, $loop, $prefix ); |
| 130 |
} else { |
| 131 |
?> |
| 132 |
<hr style="border:none; border-top: 1px solid #ededed;"> |
| 133 |
<?php |
| 134 |
$this->renderSelectField( $currentFormId, $entityId, $role, $loop, $prefix ); |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
public function saveProductFormSelect( $productId ) { |
| 140 |
$this->saveMeta( $productId ); |
| 141 |
} |
| 142 |
|
| 143 |
public function saveVariationFormSelect( $variationId, $loop ) { |
| 144 |
$formId = Form::getFieldValue( 'quote_form_id', null, $loop, '' ); |
| 145 |
if ( ! is_null( $formId ) ) { |
| 146 |
if ( $formId !== '' ) { |
| 147 |
update_post_meta( $variationId, '_tier_pricing_table_quote_form_id', sanitize_text_field( $formId ) ); |
| 148 |
} else { |
| 149 |
delete_post_meta( $variationId, '_tier_pricing_table_quote_form_id' ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$autoOpen = Form::getFieldValue( 'quote_auto_open_quantity', null, $loop, '' ); |
| 154 |
if ( ! is_null( $autoOpen ) ) { |
| 155 |
if ( $autoOpen !== '' ) { |
| 156 |
update_post_meta( $variationId, '_tier_pricing_table_quote_auto_open_quantity', |
| 157 |
sanitize_text_field( $autoOpen ) ); |
| 158 |
} else { |
| 159 |
delete_post_meta( $variationId, '_tier_pricing_table_quote_auto_open_quantity' ); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$label = Form::getFieldValue( 'quote_integrated_label_text', null, $loop, '' ); |
| 164 |
if ( ! is_null( $label ) ) { |
| 165 |
if ( $label !== '' ) { |
| 166 |
update_post_meta( $variationId, '_tier_pricing_table_quote_integrated_label_text', |
| 167 |
sanitize_text_field( $label ) ); |
| 168 |
} else { |
| 169 |
delete_post_meta( $variationId, '_tier_pricing_table_quote_integrated_label_text' ); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
public function saveGlobalFormSelect( $pricingRule, $ruleId ) { |
| 175 |
$this->saveMeta( $ruleId ); |
| 176 |
} |
| 177 |
|
| 178 |
public function saveRoleBasedMeta( $productId, $data, $role, $loop = null ) { |
| 179 |
|
| 180 |
$formId = Form::getFieldValue( 'quote_form_id', $role, $loop, '', $data ); |
| 181 |
if ( ! is_null( $formId ) ) { |
| 182 |
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id", |
| 183 |
sanitize_text_field( $formId ) ); |
| 184 |
} else { |
| 185 |
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id" ); |
| 186 |
} |
| 187 |
|
| 188 |
$autoOpen = Form::getFieldValue( 'quote_auto_open_quantity', $role, $loop, '', $data ); |
| 189 |
if ( ! is_null( $autoOpen ) ) { |
| 190 |
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_auto_open_quantity", |
| 191 |
sanitize_text_field( $autoOpen ) ); |
| 192 |
} else { |
| 193 |
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_auto_open_quantity" ); |
| 194 |
} |
| 195 |
|
| 196 |
$label = Form::getFieldValue( 'quote_integrated_label_text', $role, $loop, '', $data ); |
| 197 |
if ( ! is_null( $label ) ) { |
| 198 |
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_integrated_label_text", |
| 199 |
sanitize_text_field( $label ) ); |
| 200 |
} else { |
| 201 |
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_integrated_label_text" ); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
private function renderSelectField( $currentFormId, $entityId, $role = null, $loop = null, $prefix = '' ) { |
| 206 |
$forms = RequestQuoteForm::getAll(); |
| 207 |
|
| 208 |
if ( empty( $forms ) ) { |
| 209 |
return; // No forms created yet |
| 210 |
} |
| 211 |
|
| 212 |
$options = array( |
| 213 |
'' => __( 'None', 'tier-pricing-table' ), |
| 214 |
); |
| 215 |
|
| 216 |
$formsData = array(); |
| 217 |
|
| 218 |
foreach ( $forms as $form ) { |
| 219 |
$options[ $form->getId() ] = $form->getName(); |
| 220 |
$formsData[ $form->getId() ] = array( |
| 221 |
'display_position' => $form->getDisplayPosition(), |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
$idSuffix = $role ? '_' . $role : ''; |
| 226 |
$idSuffix .= ! is_null( $loop ) ? '_' . $loop : ''; |
| 227 |
|
| 228 |
$formIdFieldId = '_tiered_pricing_quote_form_id' . $idSuffix; |
| 229 |
|
| 230 |
$nameFormId = Form::getFieldName( 'quote_form_id', $role, $loop, $prefix ); |
| 231 |
$nameLabel = Form::getFieldName( 'quote_integrated_label_text', $role, $loop, $prefix ); |
| 232 |
$nameAutoOpen = Form::getFieldName( 'quote_auto_open_quantity', $role, $loop, $prefix ); |
| 233 |
|
| 234 |
$labelVal = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_integrated_label_text", |
| 235 |
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_integrated_label_text', true ); |
| 236 |
$autoOpenVal = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_auto_open_quantity", |
| 237 |
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_auto_open_quantity', true ); |
| 238 |
|
| 239 |
woocommerce_wp_select( array( |
| 240 |
'id' => $formIdFieldId, |
| 241 |
'name' => $nameFormId, |
| 242 |
'value' => $currentFormId, |
| 243 |
'options' => $options, |
| 244 |
'label' => __( 'Request a Quote Form', 'tier-pricing-table' ), |
| 245 |
'class' => 'select short tpt-quote-form-select', |
| 246 |
) ); |
| 247 |
|
| 248 |
$wrapperStyle = ( ! empty( $currentFormId ) ) ? '' : 'display:none;'; |
| 249 |
echo '<div id="tpt-quote-entity-settings-wrapper' . esc_attr( $idSuffix ) . '" class="tpt-quote-settings-wrapper" style="' . $wrapperStyle . '">'; |
| 250 |
|
| 251 |
$labelStyle = 'display:none;'; |
| 252 |
if ( ! empty( $currentFormId ) && isset( $formsData[ $currentFormId ] ) && $formsData[ $currentFormId ]['display_position'] === 'integrated' ) { |
| 253 |
$labelStyle = ''; |
| 254 |
} |
| 255 |
echo '<div id="tpt-quote-integrated-label-wrapper' . esc_attr( $idSuffix ) . '" class="tpt-quote-label-wrapper" style="' . $labelStyle . '">'; |
| 256 |
woocommerce_wp_text_input( array( |
| 257 |
'id' => '_tier_pricing_table_quote_integrated_label_text' . $idSuffix, |
| 258 |
'name' => $nameLabel, |
| 259 |
'value' => $labelVal, |
| 260 |
'placeholder' => __( 'Leave blank to use the form default', 'tier-pricing-table' ), |
| 261 |
'label' => __( 'Quantity Label', 'tier-pricing-table' ), |
| 262 |
'description' => __( 'Override the quantity label.', 'tier-pricing-table' ), |
| 263 |
'desc_tip' => false, |
| 264 |
) ); |
| 265 |
echo '</div>'; |
| 266 |
|
| 267 |
woocommerce_wp_text_input( array( |
| 268 |
'id' => '_tier_pricing_table_quote_auto_open_quantity' . $idSuffix, |
| 269 |
'name' => $nameAutoOpen, |
| 270 |
'value' => $autoOpenVal, |
| 271 |
'type' => 'number', |
| 272 |
'placeholder' => __( 'Leave blank to use the form default', 'tier-pricing-table' ), |
| 273 |
'custom_attributes' => array( 'min' => '1' ), |
| 274 |
'label' => __( 'Quantity Trigger', 'tier-pricing-table' ), |
| 275 |
'description' => __( 'Override the global auto-open trigger. Automatically open the quote form if a customer selects this quantity or higher.', |
| 276 |
'tier-pricing-table' ), |
| 277 |
'desc_tip' => false, |
| 278 |
) ); |
| 279 |
echo '</div>'; |
| 280 |
|
| 281 |
?> |
| 282 |
<script> |
| 283 |
jQuery(document).ready(function ($) { |
| 284 |
if (typeof window.tptQuoteFormsData === 'undefined') { |
| 285 |
window.tptQuoteFormsData = <?php echo json_encode( $formsData ); ?>; |
| 286 |
|
| 287 |
$(document).on('change', '.tpt-quote-form-select', function () { |
| 288 |
var val = $(this).val(); |
| 289 |
var $selectP = $(this).closest('p.form-field'); |
| 290 |
var $wrapper = $selectP.nextAll('.tpt-quote-settings-wrapper').first(); |
| 291 |
var $labelWrapper = $wrapper.find('.tpt-quote-label-wrapper'); |
| 292 |
|
| 293 |
if (val && window.tptQuoteFormsData[val]) { |
| 294 |
$wrapper.show(); |
| 295 |
if (window.tptQuoteFormsData[val].display_position === 'integrated') { |
| 296 |
$labelWrapper.show(); |
| 297 |
} else { |
| 298 |
$labelWrapper.hide(); |
| 299 |
} |
| 300 |
} else { |
| 301 |
$wrapper.hide(); |
| 302 |
} |
| 303 |
}); |
| 304 |
} |
| 305 |
|
| 306 |
// Trigger change on init for the current element specifically |
| 307 |
$('#<?php echo esc_js( $formIdFieldId ); ?>').trigger('change'); |
| 308 |
}); |
| 309 |
</script> |
| 310 |
<?php |
| 311 |
} |
| 312 |
|
| 313 |
private function saveMeta( $postId ) { |
| 314 |
$prefix = ''; |
| 315 |
if ( isset( $_POST['product-type'] ) && 'variable' === $_POST['product-type'] ) { |
| 316 |
$prefix = '_variable'; |
| 317 |
} |
| 318 |
|
| 319 |
$formId = Form::getFieldValue( 'quote_form_id', null, null, $prefix ); |
| 320 |
if ( ! is_null( $formId ) ) { |
| 321 |
if ( $formId !== '' ) { |
| 322 |
update_post_meta( $postId, '_tier_pricing_table_quote_form_id', sanitize_text_field( $formId ) ); |
| 323 |
|
| 324 |
// Save overrides |
| 325 |
$autoOpen = Form::getFieldValue( 'quote_auto_open_quantity', null, null, $prefix ); |
| 326 |
if ( ! is_null( $autoOpen ) ) { |
| 327 |
update_post_meta( $postId, '_tier_pricing_table_quote_auto_open_quantity', |
| 328 |
sanitize_text_field( $autoOpen ) ); |
| 329 |
} |
| 330 |
|
| 331 |
$label = Form::getFieldValue( 'quote_integrated_label_text', null, null, $prefix ); |
| 332 |
if ( ! is_null( $label ) ) { |
| 333 |
update_post_meta( $postId, '_tier_pricing_table_quote_integrated_label_text', |
| 334 |
sanitize_text_field( $label ) ); |
| 335 |
} |
| 336 |
} else { |
| 337 |
delete_post_meta( $postId, '_tier_pricing_table_quote_form_id' ); |
| 338 |
delete_post_meta( $postId, '_tier_pricing_table_quote_auto_open_quantity' ); |
| 339 |
delete_post_meta( $postId, '_tier_pricing_table_quote_integrated_label_text' ); |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
protected function classifyPlace( $entityId, $role, $loop ) { |
| 345 |
if ( $role ) { |
| 346 |
if ( strpos( $role, 'user_' ) === 0 ) { |
| 347 |
return 'user'; |
| 348 |
} |
| 349 |
|
| 350 |
return 'role'; |
| 351 |
} elseif ( get_post_type( $entityId ) == GlobalTieredPricingCPT::SLUG ) { |
| 352 |
return 'global'; |
| 353 |
} elseif ( $loop ) { |
| 354 |
return 'loop'; |
| 355 |
} else { |
| 356 |
return 'product'; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|