PluginProbe
Tiered Pricing Table for WooCommerce / 8.0.2
Tiered Pricing Table for WooCommerce v8.0.2
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 / AddToCartAdapter.php

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

147 lines 4.5 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\Addons\RequestAQuote\Models\RequestQuoteForm;
5 use TierPricingTable\Core\ServiceContainer;
6 use TierPricingTable\PriceManager;
7 use TierPricingTable\PricingRule;
8
9 class AddToCartAdapter extends AbstractLayoutAdapter {
10
11 protected bool $hasRendered = false;
12
13 public function registerHooks() {
14 // Hook into the single product page add to cart area
15 add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'renderButtonNextToCart' ), -10 );
16
17 // Fallback for non-purchasable or out-of-stock products
18 add_action( 'woocommerce_single_product_summary', array( $this, 'renderCustomCartForm' ), 31 );
19
20 // Hook to hide the add to cart button via CSS if configured
21 add_action( 'wp_head', array( $this, 'maybeHideAddToCartButton' ) );
22 }
23
24 public function renderButtonNextToCart() {
25 global $product;
26
27 if ( ! is_a( $product, 'WC_Product' ) ) {
28 return;
29 }
30
31 $pricingRule = PriceManager::getPricingRule( $product->get_id() );
32 $form = $this->getValidFormForNextToCartPosition( $pricingRule );
33
34 if ( ! $form ) {
35 return;
36 }
37
38 $this->hasRendered = true;
39
40 QuoteFormDisplay::addModalToRender( $form, $product->get_id() );
41
42 $autoOpenQty = $form->getAutoOpenQuantity();
43 $classes = 'button alt wp-element-button tpt-request-quote-trigger tpt-quote-next-to-cart';
44 $idAttr = 'tpt-raq-link-' . esc_attr( $product->get_id() );
45 $style = 'margin-left: 10px;';
46
47 echo wp_kses_post( ServiceContainer::getInstance()->getFileManager()->renderTemplate( 'frontend/integrated/quote-button.php',
48 array(
49 'form' => $form,
50 'productId' => $product->get_id(),
51 'classes' => $classes,
52 'idAttr' => $idAttr,
53 'autoOpenQty' => $autoOpenQty,
54 'style' => $style,
55 'content' => esc_html( $form->getPromptText() ),
56 ), plugin_dir_path( dirname( __DIR__ ) ) . 'views/' ) );
57 }
58
59 public function renderCustomCartForm() {
60 if ( $this->hasRendered ) {
61 return; // Standard button was already rendered
62 }
63
64 global $product;
65 if ( ! is_a( $product, 'WC_Product' ) ) {
66 return;
67 }
68
69 $pricingRule = PriceManager::getPricingRule( $product->get_id() );
70 $form = $this->getValidFormForNextToCartPosition( $pricingRule );
71
72 if ( ! $form || ! $form->getShowForNonPurchasable() ) {
73 return;
74 }
75
76 $this->hasRendered = true; // Mark as rendered so we don't render again if called elsewhere
77
78 QuoteFormDisplay::addModalToRender( $form, $product->get_id() );
79
80 // Ensure standard woocommerce scripts know it's a form if needed, though mostly it's just for styling
81 echo '<form class="cart tpt-custom-quote-cart" method="post" enctype="multipart/form-data" style="margin-top: 15px;">';
82
83 $autoOpenQty = $form->getAutoOpenQuantity();
84 $classes = 'button alt wp-element-button tpt-request-quote-trigger tpt-quote-next-to-cart';
85 $idAttr = 'tpt-raq-link-' . esc_attr( $product->get_id() );
86
87 echo wp_kses_post( ServiceContainer::getInstance()->getFileManager()->renderTemplate( 'frontend/integrated/quote-button.php',
88 array(
89 'form' => $form,
90 'productId' => $product->get_id(),
91 'classes' => $classes,
92 'idAttr' => $idAttr,
93 'autoOpenQty' => $autoOpenQty,
94 'style' => '',
95 'content' => esc_html( $form->getPromptText() ),
96 ), plugin_dir_path( dirname( __DIR__ ) ) . 'views/' ) );
97
98 echo '</form>';
99 }
100
101 public function maybeHideAddToCartButton() {
102 if ( ! is_product() ) {
103 return;
104 }
105
106 $productId = get_the_ID();
107 if ( ! $productId ) {
108 return;
109 }
110
111 $pricingRule = PriceManager::getPricingRule( $productId );
112 $form = $this->getValidFormForNextToCartPosition( $pricingRule );
113
114 if ( $form && $form->getHideAddToCart() ) {
115 echo '<style>.single_add_to_cart_button { display: none !important; }</style>';
116 }
117 }
118
119 /**
120 * Check if the given pricing rule has a valid form configured for the next_to_add_to_cart position.
121 *
122 * @param PricingRule $pricingRule
123 *
124 * @return RequestQuoteForm|false
125 */
126 protected function getValidFormForNextToCartPosition( $pricingRule ) {
127 $formId = $this->getAssignedFormId( $pricingRule, null );
128 if ( ! $formId ) {
129 return false;
130 }
131
132 $form = $this->getForm( $formId );
133 if ( ! $form ) {
134 return false;
135 }
136
137 if ( 'next_to_add_to_cart' !== $form->getDisplayPosition() ) {
138 return false;
139 }
140
141 $overrides = $this->getEntityOverrides( $pricingRule, null );
142 $form->setOverrides( $overrides );
143
144 return $form;
145 }
146 }
147