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 |