Block.php
60 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\Product\Price; |
| 4 | |
| 5 | use SureCartBlocks\Blocks\Product\ProductBlock; |
| 6 | use SureCart\Support\Currency; |
| 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 | if ( empty( $attributes['id'] ) && empty( get_query_var( 'sc_upsell_id' ) ) ) { |
| 29 | return \SureCart::block() |
| 30 | ->productSelectedPriceMigration( $attributes, $this->block ) |
| 31 | ->render(); |
| 32 | } |
| 33 | |
| 34 | $product = $this->getProductAndSetInitialState( $attributes['id'] ?? '' ); |
| 35 | if ( empty( $product ) || empty( $product->active_prices ) ) { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | $attributes = get_block_wrapper_attributes( |
| 40 | [ |
| 41 | 'sale-text' => esc_attr( $attributes['sale_text'] ?? '' ), |
| 42 | 'class' => esc_attr( $this->getClasses( $attributes ) . ' product-price surecart-block' ), |
| 43 | 'style' => esc_attr( $this->getStyles( $attributes ) . ' --sc-product-price-alignment:' . esc_attr( $attributes['alignment'] ?? 'left' ) . '; text-align:' . esc_attr( $attributes['alignment'] ?? 'left' ) . ';' ), |
| 44 | 'product-id' => esc_attr( $product->id ), |
| 45 | ] |
| 46 | ); |
| 47 | |
| 48 | // first price. |
| 49 | $first_price = $product->active_prices[0]; |
| 50 | |
| 51 | return wp_sprintf( |
| 52 | '<sc-product-price %1$s> |
| 53 | %2$s |
| 54 | </sc-product-price>', |
| 55 | $attributes, |
| 56 | esc_html( Currency::format( $first_price->amount, $first_price->currency ) ) |
| 57 | ); |
| 58 | } |
| 59 | } |
| 60 |