provider === 'role-based' && ! empty( $pricingRule->providerData['role'] ) ) {
$role = $pricingRule->providerData['role'];
$formId = get_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id", true );
if ( ! $formId ) {
$product = wc_get_product( $productId );
if ( $product && $product->is_type( 'variation' ) ) {
$parentId = $product->get_parent_id();
$formId = get_post_meta( $parentId, "_{$role}__tier_pricing_table_quote_form_id", true );
if ( $formId ) {
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId;
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $parentId,
"_{$role}__tier_pricing_table_quote_auto_open_quantity", true );
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $parentId,
"_{$role}__tier_pricing_table_quote_integrated_label_text", true );
return $pricingRule;
}
}
} else {
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId;
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $productId,
"_{$role}__tier_pricing_table_quote_auto_open_quantity", true );
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $productId,
"_{$role}__tier_pricing_table_quote_integrated_label_text", true );
return $pricingRule;
}
}
if ( $pricingRule->provider === 'global-rules' && ! empty( $pricingRule->providerData['rule_id'] ) ) {
$ruleId = $pricingRule->providerData['rule_id'];
$formId = get_post_meta( $ruleId, '_tier_pricing_table_quote_form_id', true );
if ( $formId ) {
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId;
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $ruleId,
'_tier_pricing_table_quote_auto_open_quantity', true );
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $ruleId,
'_tier_pricing_table_quote_integrated_label_text', true );
return $pricingRule;
}
}
$formId = get_post_meta( $productId, '_tier_pricing_table_quote_form_id', true );
if ( $formId ) {
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId;
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $productId,
'_tier_pricing_table_quote_auto_open_quantity', true );
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $productId,
'_tier_pricing_table_quote_integrated_label_text', true );
return $pricingRule;
}
$product = wc_get_product( $productId );
if ( $product && $product->is_type( 'variation' ) ) {
$parentId = $product->get_parent_id();
$formId = get_post_meta( $parentId, '_tier_pricing_table_quote_form_id', true );
if ( $formId ) {
$pricingRule->data['tier_pricing_table_quote_form_id'] = $formId;
$pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] = get_post_meta( $parentId,
'_tier_pricing_table_quote_auto_open_quantity', true );
$pricingRule->data['tier_pricing_table_quote_integrated_label_text'] = get_post_meta( $parentId,
'_tier_pricing_table_quote_integrated_label_text', true );
}
}
return $pricingRule;
}
public function renderFormSelect( $entityId, $role = null, $loop = null, $prefix = '' ) {
$place = $this->classifyPlace( $entityId, $role, $loop );
if ( 'user' === $place ) {
return;
}
$currentFormId = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_form_id",
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_form_id', true );
if ( 'global' === $place ) {
?>
renderSelectField( $currentFormId, $entityId, $role, $loop, $prefix );
} else {
?>
renderSelectField( $currentFormId, $entityId, $role, $loop, $prefix );
}
}
public function saveProductFormSelect( $productId ) {
$this->saveMeta( $productId );
}
public function saveVariationFormSelect( $variationId, $loop ) {
$formId = Form::getFieldValue( 'quote_form_id', null, $loop, '' );
if ( ! is_null( $formId ) ) {
if ( $formId !== '' ) {
update_post_meta( $variationId, '_tier_pricing_table_quote_form_id', sanitize_text_field( $formId ) );
} else {
delete_post_meta( $variationId, '_tier_pricing_table_quote_form_id' );
}
}
$autoOpen = Form::getFieldValue( 'quote_auto_open_quantity', null, $loop, '' );
if ( ! is_null( $autoOpen ) ) {
if ( $autoOpen !== '' ) {
update_post_meta( $variationId, '_tier_pricing_table_quote_auto_open_quantity',
sanitize_text_field( $autoOpen ) );
} else {
delete_post_meta( $variationId, '_tier_pricing_table_quote_auto_open_quantity' );
}
}
$label = Form::getFieldValue( 'quote_integrated_label_text', null, $loop, '' );
if ( ! is_null( $label ) ) {
if ( $label !== '' ) {
update_post_meta( $variationId, '_tier_pricing_table_quote_integrated_label_text',
sanitize_text_field( $label ) );
} else {
delete_post_meta( $variationId, '_tier_pricing_table_quote_integrated_label_text' );
}
}
}
public function saveGlobalFormSelect( $pricingRule, $ruleId ) {
$this->saveMeta( $ruleId );
}
public function saveRoleBasedMeta( $productId, $data, $role, $loop = null ) {
$formId = Form::getFieldValue( 'quote_form_id', $role, $loop, '', $data );
if ( ! is_null( $formId ) ) {
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id",
sanitize_text_field( $formId ) );
} else {
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_form_id" );
}
$autoOpen = Form::getFieldValue( 'quote_auto_open_quantity', $role, $loop, '', $data );
if ( ! is_null( $autoOpen ) ) {
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_auto_open_quantity",
sanitize_text_field( $autoOpen ) );
} else {
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_auto_open_quantity" );
}
$label = Form::getFieldValue( 'quote_integrated_label_text', $role, $loop, '', $data );
if ( ! is_null( $label ) ) {
update_post_meta( $productId, "_{$role}__tier_pricing_table_quote_integrated_label_text",
sanitize_text_field( $label ) );
} else {
delete_post_meta( $productId, "_{$role}__tier_pricing_table_quote_integrated_label_text" );
}
}
private function renderSelectField( $currentFormId, $entityId, $role = null, $loop = null, $prefix = '' ) {
$forms = RequestQuoteForm::getAll();
if ( empty( $forms ) ) {
return; // No forms created yet
}
$options = array(
'' => __( 'None', 'tier-pricing-table' ),
);
$formsData = array();
foreach ( $forms as $form ) {
$options[ $form->getId() ] = $form->getName();
$formsData[ $form->getId() ] = array(
'display_position' => $form->getDisplayPosition(),
);
}
$idSuffix = $role ? '_' . $role : '';
$idSuffix .= ! is_null( $loop ) ? '_' . $loop : '';
$formIdFieldId = '_tiered_pricing_quote_form_id' . $idSuffix;
$nameFormId = Form::getFieldName( 'quote_form_id', $role, $loop, $prefix );
$nameLabel = Form::getFieldName( 'quote_integrated_label_text', $role, $loop, $prefix );
$nameAutoOpen = Form::getFieldName( 'quote_auto_open_quantity', $role, $loop, $prefix );
$labelVal = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_integrated_label_text",
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_integrated_label_text', true );
$autoOpenVal = $role ? get_post_meta( $entityId, "_{$role}__tier_pricing_table_quote_auto_open_quantity",
true ) : get_post_meta( $entityId, '_tier_pricing_table_quote_auto_open_quantity', true );
woocommerce_wp_select( array(
'id' => $formIdFieldId,
'name' => $nameFormId,
'value' => $currentFormId,
'options' => $options,
'label' => __( 'Request a Quote Form', 'tier-pricing-table' ),
'class' => 'select short tpt-quote-form-select',
) );
$wrapperStyle = ( ! empty( $currentFormId ) ) ? '' : 'display:none;';
echo '';
$labelStyle = 'display:none;';
if ( ! empty( $currentFormId ) && isset( $formsData[ $currentFormId ] ) && $formsData[ $currentFormId ]['display_position'] === 'integrated' ) {
$labelStyle = '';
}
echo '
';
woocommerce_wp_text_input( array(
'id' => '_tier_pricing_table_quote_integrated_label_text' . $idSuffix,
'name' => $nameLabel,
'value' => $labelVal,
'placeholder' => __( 'Leave blank to use the form default', 'tier-pricing-table' ),
'label' => __( 'Quantity Label', 'tier-pricing-table' ),
'description' => __( 'Override the quantity label.', 'tier-pricing-table' ),
'desc_tip' => false,
) );
echo '
';
woocommerce_wp_text_input( array(
'id' => '_tier_pricing_table_quote_auto_open_quantity' . $idSuffix,
'name' => $nameAutoOpen,
'value' => $autoOpenVal,
'type' => 'number',
'placeholder' => __( 'Leave blank to use the form default', 'tier-pricing-table' ),
'custom_attributes' => array( 'min' => '1' ),
'label' => __( 'Quantity Trigger', 'tier-pricing-table' ),
'description' => __( 'Override the global auto-open trigger. Automatically open the quote form if a customer selects this quantity or higher.',
'tier-pricing-table' ),
'desc_tip' => false,
) );
echo '
';
?>