Block.php
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Product\Price; |
| 4 | |
| 5 | use SureCart\Support\Currency; |
| 6 | use SureCartBlocks\Blocks\Product\ProductBlock; |
| 7 | |
| 8 | /** |
| 9 | * Product Title Block |
| 10 | */ |
| 11 | class Block extends ProductBlock { |
| 12 | /** |
| 13 | * Keep track of the instance number of this block. |
| 14 | * |
| 15 | * @var integer |
| 16 | */ |
| 17 | public static $instance; |
| 18 | |
| 19 | /** |
| 20 | * Render the block |
| 21 | * |
| 22 | * @param array $attributes Block attributes. |
| 23 | * @param string $content Post content. |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | public function render( $attributes, $content ) { |
| 28 | $product = $this->getProductAndSetInitialState( $attributes['id'] ?? '' ); |
| 29 | if ( empty( $product ) || empty( $product->activePrices() ) ) { |
| 30 | return ''; |
| 31 | } |
| 32 | |
| 33 | $attributes = get_block_wrapper_attributes( |
| 34 | [ |
| 35 | 'sale-text' => esc_attr( $attributes['sale_text'] ?? '' ), |
| 36 | 'class' => esc_attr( $this->getClasses( $attributes ) . ' product-price surecart-block' ), |
| 37 | 'style' => esc_attr( $this->getStyles( $attributes ) . ' --sc-product-price-alignment:' . esc_attr( $attributes['alignment'] ?? 'left' ) . '; text-align:' . esc_attr( $attributes['alignment'] ?? 'left' ) . ';' ), |
| 38 | 'product-id' => esc_attr( $product->id ), |
| 39 | ] |
| 40 | ); |
| 41 | |
| 42 | // first price. |
| 43 | $first_price = $product->activePrices()[0]; |
| 44 | |
| 45 | return wp_sprintf( |
| 46 | '<sc-product-price %1$s> |
| 47 | %2$s |
| 48 | </sc-product-price>', |
| 49 | $attributes, |
| 50 | esc_html( Currency::format( $first_price->amount, $first_price->currency ) ) |
| 51 | ); |
| 52 | } |
| 53 | } |
| 54 |