HelperService.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\WP\HelperService; |
| 9 | |
| 10 | use AmeliaBooking\Infrastructure\WP\Integrations\WooCommerce\WooCommerceService; |
| 11 | |
| 12 | /** |
| 13 | * Class HelperService |
| 14 | * |
| 15 | * @package AmeliaBooking\Infrastructure\WP\HelperService |
| 16 | */ |
| 17 | class HelperService |
| 18 | { |
| 19 | public static $jsVars = []; |
| 20 | |
| 21 | /** |
| 22 | * Helper method to add PHP vars to JS vars |
| 23 | * |
| 24 | * @param $varName |
| 25 | * @param $phpVar |
| 26 | */ |
| 27 | public static function exportJSVar($varName, $phpVar) |
| 28 | { |
| 29 | self::$jsVars[$varName] = $phpVar; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Helper method to print PHP vars to JS vars |
| 34 | */ |
| 35 | public static function printJSVars() |
| 36 | { |
| 37 | if (!empty(self::$jsVars)) { |
| 38 | $jsBlock = '<script type="text/javascript">'; |
| 39 | foreach (self::$jsVars as $varName => $jsVar) { |
| 40 | $jsBlock .= "var {$varName} = " . json_encode($jsVar) . ';'; |
| 41 | } |
| 42 | $jsBlock .= '</script>'; |
| 43 | echo $jsBlock; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param int $orderId |
| 49 | * |
| 50 | * @return string|null |
| 51 | */ |
| 52 | public static function getWooCommerceOrderUrl($orderId) |
| 53 | { |
| 54 | return get_edit_post_link($orderId, ''); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param int $orderId |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | public static function getWooCommerceOrderItemAmountValues($orderId) |
| 63 | { |
| 64 | $order = wc_get_order($orderId); |
| 65 | |
| 66 | $prices = []; |
| 67 | |
| 68 | if ($order) { |
| 69 | foreach ($order->get_items() as $itemId => $orderItem) { |
| 70 | $data = wc_get_order_item_meta($itemId, WooCommerceService::AMELIA); |
| 71 | |
| 72 | if ($data && is_array($data)) { |
| 73 | $prices[$itemId] = [ |
| 74 | 'coupon' => (float)round($orderItem->get_subtotal() - $orderItem->get_total(), 2), |
| 75 | 'tax' => !isset($data['taxIncluded']) || !$data['taxIncluded'] ? |
| 76 | (float)$orderItem->get_total_tax() : 0, |
| 77 | ]; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return $prices; |
| 83 | } |
| 84 | } |
| 85 |