Admin
2 years ago
Assets
2 years ago
Pages
3 years ago
PostTypes
2 years ago
Shortcodes
2 years ago
Sitemap
2 years ago
Templates
2 years ago
Users
3 years ago
ActionsService.php
3 years ago
CompatibilityService.php
2 years ago
HealthService.php
2 years ago
LineItemStateService.php
2 years ago
PluginService.php
3 years ago
PluginServiceProvider.php
2 years ago
RecaptchaValidationService.php
2 years ago
StateService.php
2 years ago
ThemeService.php
2 years ago
ThemeServiceProvider.php
3 years ago
TranslationsServiceProvider.php
3 years ago
LineItemStateService.php
84 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\WordPress; |
| 4 | |
| 5 | /** |
| 6 | * LineItemStateService class |
| 7 | */ |
| 8 | class LineItemStateService { |
| 9 | /** |
| 10 | * Keeps track of already parsed blocks. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | protected static $parsed_blocks = []; |
| 15 | |
| 16 | /** |
| 17 | * Get existing line items from the checkout. |
| 18 | * |
| 19 | * @param string $store The store name. |
| 20 | * @param string $key The key to get. |
| 21 | */ |
| 22 | public function get( $store = 'checkout', $key = 'initialLineItems' ) { |
| 23 | $initial = sc_initial_state(); |
| 24 | return ! empty( $initial[ $store ][ $key ] ) ? $initial[ $store ][ $key ] : []; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Add line items to the checkout. |
| 29 | * |
| 30 | * @param array $line_items The line items to add. |
| 31 | * @param string $store The store name. |
| 32 | * @param string $key The key to get. |
| 33 | * |
| 34 | * @return array The new line items. |
| 35 | */ |
| 36 | public function merge( $line_items = [], $store = 'checkout', $key = 'initialLineItems' ) { |
| 37 | $initial = sc_initial_state(); |
| 38 | $initial_items = $initial[ $store ][ $key ] ?? []; |
| 39 | |
| 40 | // filter out any line items that were already added. |
| 41 | $merged_line_items = array_filter( |
| 42 | $line_items, |
| 43 | function ( $line_item ) use ( $initial_items ) { |
| 44 | // if the line item does not exist, add it. |
| 45 | return ! $this->lineItemExists( $line_item, $initial_items ); |
| 46 | } |
| 47 | ); |
| 48 | |
| 49 | return array_merge( $initial_items, $merged_line_items ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Does the line item exist? |
| 54 | * |
| 55 | * @param array $line_item The line item to check. |
| 56 | * |
| 57 | * @return boolean |
| 58 | */ |
| 59 | public function lineItemExists( $line_item, $line_items ) { |
| 60 | return array_reduce( |
| 61 | $line_items, |
| 62 | function ( $carry, $existing_line_item ) use ( $line_item ) { |
| 63 | $existing_price_id = $existing_line_item['price_id'] ?? $existing_line_item['price'] ?? null; |
| 64 | $existing_variant_id = $existing_line_item['variant_id'] ?? $existing_line_item['variant'] ?? null; |
| 65 | $price_id = $line_item['price_id'] ?? $line_item['price'] ?? null; |
| 66 | $variant_id = $line_item['variant_id'] ?? $line_item['variant'] ?? null; |
| 67 | |
| 68 | if ( (bool) $existing_variant_id && (bool) $variant_id && $existing_variant_id === $variant_id ) { |
| 69 | if ( $existing_price_id === $price_id ) { |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ( (bool) $existing_price_id && (bool) $price_id && $existing_price_id === $price_id ) { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return $carry; |
| 79 | }, |
| 80 | false |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 |