Block.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCartBlocks\Blocks\ProductDonation; |
| 4 | |
| 5 | use SureCart\Models\Product; |
| 6 | use SureCartBlocks\Blocks\BaseBlock; |
| 7 | use SureCartBlocks\Util\BlockStyleAttributes; |
| 8 | |
| 9 | /** |
| 10 | * Product Title Block |
| 11 | */ |
| 12 | class Block extends BaseBlock { |
| 13 | /** |
| 14 | * Render the block |
| 15 | * |
| 16 | * @param array $attributes Block attributes. |
| 17 | * @param string $content Post content. |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public function render( $attributes, $content ) { |
| 22 | if ( empty( $attributes['product_id'] ) ) { |
| 23 | return ''; |
| 24 | } |
| 25 | |
| 26 | // get the product. |
| 27 | $product = Product::with( array( 'prices' ) )->find( $attributes['product_id'] ?? '' ); |
| 28 | if ( is_wp_error( $product ) ) { |
| 29 | return $product->get_error_message(); |
| 30 | } |
| 31 | |
| 32 | // no ad_hoc prices. |
| 33 | if ( ! count( $product->activeAdHocPrices() ) ) { |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | // get amounts from inner blocks. |
| 38 | $amounts = $this->getAmounts(); |
| 39 | if ( empty( $amounts ) ) { |
| 40 | return false; |
| 41 | } |
| 42 | |
| 43 | // set initial state. |
| 44 | sc_initial_state( |
| 45 | array( |
| 46 | 'checkout' => array( |
| 47 | 'initialLineItems' => sc_initial_line_items( $this->getInitialLineItems( $product, $amounts ) ), |
| 48 | ), |
| 49 | 'productDonation' => array( |
| 50 | $attributes['product_id'] => array( |
| 51 | 'product' => $product->toArray(), |
| 52 | 'amounts' => $amounts, |
| 53 | 'ad_hoc_amount' => null, |
| 54 | 'custom_amount' => null, |
| 55 | 'selectedPrice' => ( $product->active_prices || array() )[0] ?? null, |
| 56 | ), |
| 57 | ), |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | [ 'styles' => $styles, 'classes' => $classes ] = BlockStyleAttributes::getClassesAndStylesFromAttributes( $attributes ); |
| 62 | |
| 63 | if ( ! empty( $attributes['textColor'] ) ) { |
| 64 | $styles .= '--sc-input-label-color: ' . $attributes['textColor'] . '; '; |
| 65 | } |
| 66 | |
| 67 | $wrapper_attributes = get_block_wrapper_attributes( |
| 68 | array( |
| 69 | 'style' => esc_attr( $styles ), |
| 70 | 'class' => esc_attr( $classes ), |
| 71 | ) |
| 72 | ); |
| 73 | |
| 74 | return wp_sprintf( |
| 75 | '<div %s> |
| 76 | %s |
| 77 | </div>', |
| 78 | $wrapper_attributes, |
| 79 | filter_block_content( $content ) |
| 80 | ); |
| 81 | |
| 82 | return filter_block_content( $content ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get the initial line items. |
| 87 | * |
| 88 | * @return array |
| 89 | */ |
| 90 | public function getInitialLineItems( $product, $amounts ) { |
| 91 | if ( empty( $product->activeAdHocPrices()[0] ) ) { |
| 92 | return array(); |
| 93 | } |
| 94 | |
| 95 | return array( |
| 96 | array( |
| 97 | 'price' => $product->activeAdHocPrices()[0]->id, |
| 98 | 'ad_hoc_amount' => $product->activeAdHocPrices()[0]->amount, |
| 99 | 'quantity' => 1, |
| 100 | ), |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the amounts. |
| 106 | * |
| 107 | * @return array |
| 108 | */ |
| 109 | public function getAmounts() { |
| 110 | $amounts_block = array_filter( |
| 111 | $this->block->parsed_block['innerBlocks'], |
| 112 | function ( $block ) { |
| 113 | return 'surecart/product-donation-amounts' === $block['blockName']; |
| 114 | }, |
| 115 | ); |
| 116 | |
| 117 | if ( empty( $amounts_block[0]['innerBlocks'] ) ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | // get amounts from inner blocks. |
| 122 | return array_values( |
| 123 | array_map( |
| 124 | function ( $block ) { |
| 125 | return $block['attrs']['amount'] ?? 0; |
| 126 | }, |
| 127 | $amounts_block[0]['innerBlocks'] |
| 128 | ) |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 |