PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 3.0.3
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v3.0.3
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / packages / blocks / Blocks / AddToCartButton / Block.php
surecart / packages / blocks / Blocks / AddToCartButton Last commit date
Block.php 1 year ago block.json 1 year ago
Block.php
197 lines
1 <?php
2
3 namespace SureCartBlocks\Blocks\AddToCartButton;
4
5 use SureCart\Models\Blocks\ProductPageBlock;
6 use SureCart\Models\Price;
7
8 /**
9 * AddToCart Button Block.
10 */
11 class Block extends \SureCartBlocks\Blocks\BuyButton\Block {
12 /**
13 * Render the block
14 *
15 * @param array $attributes Block attributes.
16 * @param string $content Post content.
17 *
18 * @return string
19 */
20 public function render( $attributes, $content = '' ) {
21 // need a price id.
22 if ( empty( $attributes['price_id'] ) ) {
23 return '';
24 }
25
26 $price = Price::with( [ 'product', 'product.variants' ] )->find( $attributes['price_id'] );
27 if ( empty( $price->id ) ) {
28 return '';
29 }
30
31 // get the product.
32 $product = $price->product;
33
34 // setup the postdata.
35 global $post;
36 $post = $product->post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited,
37 setup_postdata( $post );
38
39 $variants = ! empty( $attributes['variant_id'] ) ? array_filter(
40 $product->variants->data ?? [],
41 function ( $variant ) use ( $attributes ) {
42 return $variant->id == $attributes['variant_id'];
43 }
44 ) : null;
45 $variant = $variants ? array_shift( $variants ) : null;
46
47 $form = \SureCart::forms()->getDefault();
48 if ( empty( $form->ID ) ) {
49 return '';
50 }
51
52 // Use backgroundColor and textColor if exist.
53 $styles = '';
54 if ( ! empty( $attributes['backgroundColor'] ) ) {
55 $styles .= "background-color: {$attributes['backgroundColor']}; ";
56 }
57 if ( ! empty( $attributes['textColor'] ) ) {
58 $styles .= "color: {$attributes['textColor']}; ";
59 }
60
61 $class = 'sc-button wp-element-button wp-block-button__link sc-button__link';
62
63 // Slide-out is disabled, go directly to checkout.
64 if ( (bool) get_option( 'sc_slide_out_cart_disabled', false ) ) {
65 return \SureCart::block()->render(
66 'blocks/buy-button',
67 [
68 'type' => $attributes['type'] ?? 'primary',
69 'size' => $attributes['size'] ?? 'medium',
70 'style' => $styles,
71 'class' => $class,
72 'href' => $this->href(
73 [
74 [
75 'id' => $price->id,
76 'variant_id' => $attributes['variant_id'] ?? null,
77 'quantity' => 1,
78 ],
79 ]
80 ),
81 'label' => $attributes['button_text'] ?? __( 'Buy Now', 'surecart' ),
82 ]
83 );
84 }
85
86 $controller = new ProductPageBlock();
87
88 $price_attributes = [
89 'id',
90 'archived',
91 'amount',
92 'display_amount',
93 'scratch_amount',
94 'scratch_display_amount',
95 'ad_hoc',
96 'is_zero_decimal',
97 'currency_symbol',
98 'converted_ad_hoc_min_amount',
99 'converted_ad_hoc_max_amount',
100 'setup_fee_text',
101 'interval_text',
102 'short_interval_text',
103 'interval_count_text',
104 'payments_text',
105 'trial_text',
106 ];
107
108 $context = $controller->context(
109 [
110 'selectedPrice' => $price ? $price->only( $price_attributes ) : null,
111 'prices' => array_map( fn( $price ) => $price->only( $price_attributes ), [ $price ] ),
112 'variantValues' => $variant ? array_filter(
113 [
114 'option_1' => $variant->option_1 ?? null,
115 'option_2' => $variant->option_2 ?? null,
116 'option_3' => $variant->option_3 ?? null,
117 ]
118 ) : [],
119 ]
120 );
121
122 ob_start(); ?>
123 <style>
124 .sc-form, .sc-form-wrapper {
125 display: inline-block;
126 }
127 </style>
128 <div
129 class="sc-form-wrapper"
130 data-wp-interactive='{ "namespace": "surecart/product-page" }'
131 <?php echo wp_kses_data( wp_interactivity_data_wp_context( $context ) ); ?>
132 >
133 <form class="sc-form" data-wp-on--submit="callbacks.handleSubmit">
134 <?php if ( ! empty( $price->ad_hoc ) ) : ?>
135 <div class="sc-form-group">
136 <label for="sc-product-custom-amount" class="sc-form-label">
137 <?php echo wp_kses_post( $attributes['ad_hoc_label'] ?? esc_html_e( 'Amount', 'surecart' ) ); ?>
138 </label>
139 <div class="sc-input-group">
140 <span class="sc-input-group-text" id="basic-addon1" data-wp-text="context.selectedPrice.currency_symbol"></span>
141
142 <input
143 class="sc-form-control"
144 id="sc-product-custom-amount"
145 type="number"
146 step="0.01"
147 required
148 placeholder="<?php echo esc_attr( $attributes['placeholder'] ?? '' ); ?>"
149 data-wp-bind--min="context.selectedPrice.converted_ad_hoc_min_amount"
150 data-wp-bind--max="context.selectedPrice.converted_ad_hoc_max_amount"
151 data-wp-bind--value="context.adHocAmount"
152 data-wp-on--input="callbacks.setAdHocAmount"
153 />
154 </div>
155 <?php if ( ! empty( $attributes['help'] ) ) : ?>
156 <div class="sc-help-text">
157 <?php echo wp_kses_post( $attributes['help'] ); ?>
158 </div>
159 <?php endif; ?>
160 </div>
161 <?php endif; ?>
162
163 <div
164 <?php
165 echo wp_kses_data(
166 get_block_wrapper_attributes(
167 array(
168 'class' => 'wp-block-button',
169 )
170 )
171 );
172 ?>
173 >
174 <button
175 type="submit"
176 class="<?php echo esc_attr( $class ); ?>"
177 data-wp-class--sc-button__link--busy="context.busy"
178 style="<?php echo esc_attr( $styles ); ?>"
179 >
180 <span class="sc-spinner" aria-hidden="true" data-wp-bind--hidden="!context.busy" hidden></span>
181 <span class="sc-button__link-text">
182 <?php
183 // pass content for shortcode usage.
184 echo wp_kses_post( ! empty( $content ) ? $content : $attributes['button_text'] );
185 ?>
186 </span>
187 </button>
188 </div>
189 </form>
190 </div>
191 <?php
192 $output = ob_get_clean();
193 wp_reset_postdata();
194 return $output;
195 }
196 }
197