| 1 |
<?php namespace TierPricingTable\Addons\RequestAQuote\Frontend\Adapters; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\RequestAQuote\Frontend\QuoteFormDisplay; |
| 4 |
use TierPricingTable\Core\ServiceContainer; |
| 5 |
use TierPricingTable\PricingRule; |
| 6 |
use TierPricingTable\Addons\RequestAQuote\Models\RequestQuoteForm; |
| 7 |
|
| 8 |
abstract class AbstractLayoutAdapter { |
| 9 |
|
| 10 |
/** |
| 11 |
* Register the necessary hooks for this adapter. |
| 12 |
*/ |
| 13 |
abstract public function registerHooks(); |
| 14 |
|
| 15 |
|
| 16 |
public function renderFormPrompt( PricingRule $pricingRule, $productId = null ) { |
| 17 |
if ( ! is_scalar( $productId ) ) { |
| 18 |
$productId = null; |
| 19 |
} |
| 20 |
// Try to find the associated form ID for this pricing rule. |
| 21 |
// For now, we will assume the form ID is attached to the pricing rule object or product meta |
| 22 |
$formId = $this->getAssignedFormId( $pricingRule, $productId ); |
| 23 |
|
| 24 |
if ( ! $formId ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$form = $this->getForm( $formId ); |
| 29 |
|
| 30 |
if ( ! $form ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
$displayPosition = $form->getDisplayPosition(); |
| 35 |
|
| 36 |
// This method only handles "after" position |
| 37 |
if ( 'after' !== $displayPosition ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$overrides = $this->getEntityOverrides( $pricingRule, $productId ); |
| 42 |
$form->setOverrides( $overrides ); |
| 43 |
|
| 44 |
ServiceContainer::getInstance()->getFileManager()->includeTemplate( 'frontend/integrated/prompt.php', array( |
| 45 |
'form' => $form, |
| 46 |
'productId' => $pricingRule->getProductId(), |
| 47 |
'buttonHtml' => $this->getQuoteButtonHtml( $form, $pricingRule->getProductId(), '', '' ), |
| 48 |
), plugin_dir_path( dirname( __DIR__ ) ) . 'views/' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Renders the Request a Quote trigger button. |
| 53 |
* |
| 54 |
* @param RequestQuoteForm $form |
| 55 |
* @param int $productId |
| 56 |
* @param string $additionalClasses |
| 57 |
* @param string $style |
| 58 |
* @param string $idPrefix |
| 59 |
* @param bool $isHidden If true, the button is rendered visually hidden (useful for wrappers with onclick). |
| 60 |
*/ |
| 61 |
protected function getQuoteButtonHtml( |
| 62 |
$form, |
| 63 |
$productId, |
| 64 |
string $additionalClasses = 'button alt wp-element-button', |
| 65 |
string $style = 'padding: 5px 10px; margin: 0;', |
| 66 |
string $idPrefix = 'tpt-raq-link', |
| 67 |
bool $isHidden = false |
| 68 |
): string { |
| 69 |
// One modal per product family: variations share their parent's modal. Variation tables can be |
| 70 |
// loaded by AJAX after the page (and its wp_footer) has been rendered, so a modal keyed by the |
| 71 |
// variation ID would never reach the page. The JS resolves a variation's trigger to the parent |
| 72 |
// modal and writes the selected variation into the modal's product field. |
| 73 |
$parentId = QuoteFormDisplay::getModalProductId( $productId ); |
| 74 |
|
| 75 |
QuoteFormDisplay::addModalToRender( $form, $parentId ); |
| 76 |
$autoOpenQty = $form->getAutoOpenQuantity(); |
| 77 |
|
| 78 |
if ( $isHidden ) { |
| 79 |
$style = 'display:none;'; |
| 80 |
$content = ''; |
| 81 |
} else { |
| 82 |
$content = esc_html( $form->getPromptText() ); |
| 83 |
} |
| 84 |
|
| 85 |
$classes = trim( 'tpt-request-quote-trigger ' . $additionalClasses ); |
| 86 |
$idAttr = $idPrefix . '-' . esc_attr( $productId ); |
| 87 |
|
| 88 |
return ServiceContainer::getInstance()->getFileManager()->renderTemplate( 'frontend/integrated/quote-button.php', |
| 89 |
array( |
| 90 |
'form' => $form, |
| 91 |
'productId' => $productId, |
| 92 |
'parentId' => $parentId, |
| 93 |
'classes' => $classes, |
| 94 |
'idAttr' => $idAttr, |
| 95 |
'autoOpenQty' => $autoOpenQty, |
| 96 |
'style' => $style, |
| 97 |
'content' => $content, |
| 98 |
), plugin_dir_path( dirname( __DIR__ ) ) . 'views/' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if the given pricing rule has a valid form configured for the integrated position. |
| 103 |
* |
| 104 |
* @param PricingRule $pricingRule |
| 105 |
* |
| 106 |
* @return RequestQuoteForm|false |
| 107 |
*/ |
| 108 |
protected function getValidFormForIntegratedPosition( PricingRule $pricingRule ) { |
| 109 |
$formId = $this->getAssignedFormId( $pricingRule, null ); |
| 110 |
if ( ! $formId ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
$form = $this->getForm( $formId ); |
| 115 |
if ( ! $form ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if ( 'integrated' !== $form->getDisplayPosition() ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$overrides = $this->getEntityOverrides( $pricingRule, null ); |
| 124 |
$form->setOverrides( $overrides ); |
| 125 |
|
| 126 |
return $form; |
| 127 |
} |
| 128 |
|
| 129 |
protected function getAssignedFormId( PricingRule $pricingRule ) { |
| 130 |
return $pricingRule->data['tier_pricing_table_quote_form_id'] ?? null; |
| 131 |
} |
| 132 |
|
| 133 |
protected function getEntityOverrides( PricingRule $pricingRule, $productId = null ) { |
| 134 |
return array( |
| 135 |
'auto_open_quantity' => $pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] ?? '', |
| 136 |
'integrated_label_text' => $pricingRule->data['tier_pricing_table_quote_integrated_label_text'] ?? '', |
| 137 |
); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Fetch a form by its ID. |
| 142 |
* |
| 143 |
* @param string $formId |
| 144 |
* |
| 145 |
* @return RequestQuoteForm|null |
| 146 |
*/ |
| 147 |
protected function getForm( $formId ) { |
| 148 |
if ( ! tpt_fs()->can_use_premium_code__premium_only() && ! current_user_can( 'manage_options' ) ) { |
| 149 |
return null; |
| 150 |
} |
| 151 |
return RequestQuoteForm::get( $formId ); |
| 152 |
} |
| 153 |
} |
| 154 |
|