Block.php
59 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\ProductDonationPrices; |
| 4 | |
| 5 | use SureCart\Models\Product; |
| 6 | use SureCartBlocks\Blocks\BaseBlock; |
| 7 | |
| 8 | /** |
| 9 | * Product Title Block |
| 10 | */ |
| 11 | class Block extends BaseBlock { |
| 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 | $product = Product::with( [ 'prices' ] )->find( $this->block->context['surecart/product-donation/product_id'] ); |
| 22 | if ( is_wp_error( $product ) ) { |
| 23 | return $product->get_error_message(); |
| 24 | } |
| 25 | |
| 26 | // must have a minimum of 2 prices to show choices. |
| 27 | if ( count( $product->active_ad_hoc_prices ?? [] ) < 2 |
| 28 | ) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | $wrapper_attributes = get_block_wrapper_attributes( |
| 33 | [ |
| 34 | 'style' => implode( |
| 35 | ' ', |
| 36 | [ |
| 37 | 'border: none;', |
| 38 | esc_attr( $this->getVars( $attributes, '--sc-choice' ) ), |
| 39 | '--columns:' . intval( $attributes['columns'] ) . ';', |
| 40 | ! empty( $attributes['style']['spacing']['blockGap'] ) ? '--sc-choices-gap:' . $this->getSpacingPresetCssVar( $attributes['style']['spacing']['blockGap'] ) . ';' : '', |
| 41 | ] |
| 42 | ), |
| 43 | ] |
| 44 | ); |
| 45 | |
| 46 | return wp_sprintf( |
| 47 | '<div %s> |
| 48 | <sc-choices label="%s" required="%s"> |
| 49 | %s |
| 50 | </sc-choices> |
| 51 | </div>', |
| 52 | $wrapper_attributes, |
| 53 | esc_attr( $attributes['label'] ), |
| 54 | $this->block->context['surecart/product-donation/required'] ? 'true' : 'false', |
| 55 | filter_block_content( $content ) |
| 56 | ); |
| 57 | } |
| 58 | } |
| 59 |