Block.php
53 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Product\VariantChoices; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\Product\ProductBlock; |
| 6 | use SureCartBlocks\Util\BlockStyleAttributes; |
| 7 | |
| 8 | /** |
| 9 | * Product Title Block |
| 10 | */ |
| 11 | class Block extends ProductBlock { |
| 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 | if ( empty( $attributes['id'] ) && empty( get_query_var( 'sc_upsell_id' ) ) ) { |
| 22 | return \SureCart::block() |
| 23 | ->productVariantsMigration( $attributes, $this->block ) |
| 24 | ->render(); |
| 25 | } |
| 26 | |
| 27 | // check for product. |
| 28 | $product = $this->getProductAndSetInitialState( $attributes['id'] ?? '' ); |
| 29 | if ( empty( $product ) || empty( $product->variant_options->data ) ) { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | [ 'styles' => $styles] = BlockStyleAttributes::getClassesAndStylesFromAttributes( $attributes, [ 'margin' ] ); |
| 34 | |
| 35 | $attributes = get_block_wrapper_attributes( |
| 36 | [ |
| 37 | 'style' => 'border: none; display: block; margin-bottom: var(--sc-form-row-spacing, 0.75em);' . esc_attr( $this->getVars( $attributes, '--sc-pill-option' ) ), |
| 38 | ] |
| 39 | ); |
| 40 | |
| 41 | ob_start(); ?> |
| 42 | |
| 43 | <div style="<?php echo esc_attr( $styles ); ?>"> |
| 44 | <?php foreach ( $product->variant_options->data as $key => $option ) : ?> |
| 45 | <sc-product-pills-variant-option product-id="<?php echo esc_attr( $product->id ); ?>" label="<?php echo esc_attr( $option->name ); ?>" option-number="<?php echo (int) $key + 1; ?>" <?php echo $attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>></sc-product-pills-variant-option> |
| 46 | <?php endforeach; ?> |
| 47 | </div> |
| 48 | |
| 49 | <?php |
| 50 | return ob_get_clean(); |
| 51 | } |
| 52 | } |
| 53 |