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