PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.7
Tiered Pricing Table for WooCommerce v7.1.7
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 / Addons / RequestAQuote / Frontend / Adapters / AbstractLayoutAdapter.php

AbstractLayoutAdapter.php in Tiered Pricing Table for WooCommerce 7.1.7, at src/Addons/RequestAQuote/Frontend/Adapters/AbstractLayoutAdapter.php

147 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 QuoteFormDisplay::addModalToRender( $form, $productId );
70 $autoOpenQty = $form->getAutoOpenQuantity();
71
72 if ( $isHidden ) {
73 $style = 'display:none;';
74 $content = '';
75 } else {
76 $content = esc_html( $form->getPromptText() );
77 }
78
79 $classes = trim( 'tpt-request-quote-trigger ' . $additionalClasses );
80 $idAttr = $idPrefix . '-' . esc_attr( $productId );
81
82 return ServiceContainer::getInstance()->getFileManager()->renderTemplate( 'frontend/integrated/quote-button.php',
83 array(
84 'form' => $form,
85 'productId' => $productId,
86 'classes' => $classes,
87 'idAttr' => $idAttr,
88 'autoOpenQty' => $autoOpenQty,
89 'style' => $style,
90 'content' => $content,
91 ), plugin_dir_path( dirname( __DIR__ ) ) . 'views/' );
92 }
93
94 /**
95 * Check if the given pricing rule has a valid form configured for the integrated position.
96 *
97 * @param PricingRule $pricingRule
98 *
99 * @return RequestQuoteForm|false
100 */
101 protected function getValidFormForIntegratedPosition( PricingRule $pricingRule ) {
102 $formId = $this->getAssignedFormId( $pricingRule, null );
103 if ( ! $formId ) {
104 return false;
105 }
106
107 $form = $this->getForm( $formId );
108 if ( ! $form ) {
109 return false;
110 }
111
112 if ( 'integrated' !== $form->getDisplayPosition() ) {
113 return false;
114 }
115
116 $overrides = $this->getEntityOverrides( $pricingRule, null );
117 $form->setOverrides( $overrides );
118
119 return $form;
120 }
121
122 protected function getAssignedFormId( PricingRule $pricingRule ) {
123 return $pricingRule->data['tier_pricing_table_quote_form_id'] ?? null;
124 }
125
126 protected function getEntityOverrides( PricingRule $pricingRule, $productId = null ) {
127 return array(
128 'auto_open_quantity' => $pricingRule->data['tier_pricing_table_quote_auto_open_quantity'] ?? '',
129 'integrated_label_text' => $pricingRule->data['tier_pricing_table_quote_integrated_label_text'] ?? '',
130 );
131 }
132
133 /**
134 * Fetch a form by its ID.
135 *
136 * @param string $formId
137 *
138 * @return RequestQuoteForm|null
139 */
140 protected function getForm( $formId ) {
141 if ( ! tpt_fs()->can_use_premium_code__premium_only() && ! current_user_can( 'manage_options' ) ) {
142 return null;
143 }
144 return RequestQuoteForm::get( $formId );
145 }
146 }
147