Block.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\AddToCartButton; |
| 4 | |
| 5 | use SureCart\Models\Form; |
| 6 | use SureCart\Models\Price; |
| 7 | /** |
| 8 | * Logout Button Block. |
| 9 | */ |
| 10 | class Block extends \SureCartBlocks\Blocks\BuyButton\Block { |
| 11 | /** |
| 12 | * Render the block |
| 13 | * |
| 14 | * @param array $attributes Block attributes. |
| 15 | * @param string $content Post content. |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | public function render( $attributes, $content = '' ) { |
| 20 | // need a price id. |
| 21 | if ( empty( $attributes['price_id'] ) ) { |
| 22 | return ''; |
| 23 | } |
| 24 | |
| 25 | $price = Price::find( $attributes['price_id'] ); |
| 26 | if ( empty( $price->id ) ) { |
| 27 | return ''; |
| 28 | } |
| 29 | |
| 30 | // need a form for checkout. |
| 31 | $form = \SureCart::forms()->getDefault(); |
| 32 | if ( empty( $form->ID ) ) { |
| 33 | return ''; |
| 34 | } |
| 35 | |
| 36 | // Use backgroundColor and textColor if exist. |
| 37 | $styles = ''; |
| 38 | if ( ! empty( $attributes['backgroundColor'] ) ) { |
| 39 | $styles .= '--sc-color-primary-500: ' . $attributes['backgroundColor'] . '; '; |
| 40 | $styles .= '--sc-focus-ring-color-primary: ' . $attributes['backgroundColor'] . '; '; |
| 41 | $styles .= '--sc-input-border-color-focus: ' . $attributes['backgroundColor'] . '; '; |
| 42 | } |
| 43 | if ( ! empty( $attributes['textColor'] ) ) { |
| 44 | $styles .= '--sc-color-primary-text: ' . $attributes['textColor'] . '; '; |
| 45 | } |
| 46 | |
| 47 | // Slide-out is disabled, go directly to checkout. |
| 48 | if ( (bool) get_option( 'sc_slide_out_cart_disabled', false ) ) { |
| 49 | return \SureCart::blocks()->render( |
| 50 | 'blocks/buy-button', |
| 51 | [ |
| 52 | 'type' => $attributes['type'] ?? 'primary', |
| 53 | 'size' => $attributes['size'] ?? 'medium', |
| 54 | 'style' => $styles, |
| 55 | 'href' => $this->href( |
| 56 | [ |
| 57 | [ |
| 58 | 'id' => $price->id, |
| 59 | 'quantity' => 1, |
| 60 | ], |
| 61 | ] |
| 62 | ), |
| 63 | 'label' => $attributes['button_text'] ?? __( 'Buy Now', 'surecart' ), |
| 64 | ] |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | ob_start(); ?> |
| 69 | |
| 70 | <sc-cart-form |
| 71 | price-id="<?php echo esc_attr( $attributes['price_id'] ); ?>" |
| 72 | form-id="<?php echo esc_attr( $form->ID ); ?>" |
| 73 | mode="<?php echo esc_attr( Form::getMode( $form->ID ) ); ?>" |
| 74 | <?php if ( ! empty( $styles ) ) { ?> |
| 75 | style="<?php echo esc_attr( $styles ); ?>" |
| 76 | <?php } ?>> |
| 77 | |
| 78 | <?php if ( $price->ad_hoc ) : ?> |
| 79 | <sc-price-input |
| 80 | currency-code="<?php echo esc_attr( $price->currency ); ?>" |
| 81 | label="<?php echo esc_attr( ! empty( $attributes['ad_hoc_label'] ) ? $attributes['ad_hoc_label'] : __( 'Amount', 'surecart' ) ); ?>" |
| 82 | min="<?php echo (int) $price->ad_hoc_min_amount; ?>" |
| 83 | max="<?php echo (int) $price->ad_hoc_max_amount; ?>" |
| 84 | placeholder="<?php echo esc_attr( $attributes['placeholder'] ?? '' ); ?>" |
| 85 | required |
| 86 | help="<?php echo esc_attr( $attributes['help'] ?? '' ); ?>" |
| 87 | name="price" |
| 88 | ></sc-price-input> |
| 89 | <?php endif; ?> |
| 90 | |
| 91 | <sc-cart-form-submit |
| 92 | type="<?php echo esc_attr( ! empty( $attributes['type'] ) ? $attributes['type'] : 'primary' ); ?>" |
| 93 | size="<?php echo esc_attr( ! empty( $attributes['size'] ) ? $attributes['size'] : 'medium' ); ?>" |
| 94 | > |
| 95 | <?php echo wp_kses_post( $attributes['button_text'] ) ?? esc_html__( 'Add To Cart', 'surecart' ); ?> |
| 96 | </sc-cart-form-submit> |
| 97 | </sc-cart-form> |
| 98 | |
| 99 | <?php |
| 100 | return wp_kses_post( ob_get_clean() ); |
| 101 | } |
| 102 | } |
| 103 |