← All changes
|
app/Services/ShortCodeParser/Parsers/ItemParser.php
+125
-0
1.3.20
→
1.6.5
View file →
| @@ -47,9 +47,134 @@ | ||
| 47 | 47 | case 'payment_info': |
| 48 | 48 | return wp_kses_post($item['payment_info'] ?? ''); |
| 49 | 49 | case 'payment_type': |
| 50 | 50 | return esc_html($item['payment_type'] ?? ''); |
| 51 | + case 'package_name': | |
| 52 | + return esc_html($this->getFromOtherInfo('package_name')); | |
| 53 | + case 'package_type': | |
| 54 | + return esc_html($this->getPackageType()); | |
| 55 | + case 'dimensions': | |
| 56 | + return esc_html($this->getFormattedDimensions()); | |
| 57 | + case 'product_weight': | |
| 58 | + return esc_html($this->getFormattedProductWeight()); | |
| 59 | + case 'shipping_weight': | |
| 60 | + return esc_html($this->getFormattedShippingWeight()); | |
| 51 | 61 | default: |
| 52 | 62 | return Arr::get($item, $accessor, ''); |
| 53 | 63 | } |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * Get the item's other_info array. | |
| 68 | + */ | |
| 69 | + private function getOtherInfo() | |
| 70 | + { | |
| 71 | + $otherInfo = Arr::get($this->item, 'other_info', []); | |
| 72 | + return is_array($otherInfo) ? $otherInfo : []; | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Get a value from other_info. | |
| 77 | + */ | |
| 78 | + private function getFromOtherInfo($key, $default = '') | |
| 79 | + { | |
| 80 | + return Arr::get($this->getOtherInfo(), $key, $default); | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Get translated package type from other_info. | |
| 85 | + */ | |
| 86 | + private function getPackageType() | |
| 87 | + { | |
| 88 | + $type = $this->getFromOtherInfo('package_type', ''); | |
| 89 | + if (!$type) { | |
| 90 | + return ''; | |
| 91 | + } | |
| 92 | + | |
| 93 | + static $typeLabels = null; | |
| 94 | + if ($typeLabels === null) { | |
| 95 | + $typeLabels = [ | |
| 96 | + 'box' => __('Box', 'fluent-cart'), | |
| 97 | + 'envelope' => __('Envelope', 'fluent-cart'), | |
| 98 | + 'soft_package' => __('Soft Package', 'fluent-cart'), | |
| 99 | + ]; | |
| 100 | + } | |
| 101 | + | |
| 102 | + return isset($typeLabels[$type]) ? $typeLabels[$type] : $type; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Get formatted dimensions from other_info. | |
| 107 | + */ | |
| 108 | + private function getFormattedDimensions() | |
| 109 | + { | |
| 110 | + $otherInfo = $this->getOtherInfo(); | |
| 111 | + $length = Arr::get($otherInfo, 'package_length', ''); | |
| 112 | + $width = Arr::get($otherInfo, 'package_width', ''); | |
| 113 | + $height = Arr::get($otherInfo, 'package_height', ''); | |
| 114 | + $dimensionUnit = Arr::get($otherInfo, 'package_dimension_unit', 'cm'); | |
| 115 | + | |
| 116 | + $parts = array_filter([$length, $width, $height], function ($val) { | |
| 117 | + return $val !== '' && $val !== null && $val != 0; | |
| 118 | + }); | |
| 119 | + | |
| 120 | + return $parts ? implode(' × ', $parts) . ' ' . $dimensionUnit : ''; | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Get formatted product weight from other_info. | |
| 125 | + */ | |
| 126 | + private function getFormattedProductWeight() | |
| 127 | + { | |
| 128 | + $otherInfo = $this->getOtherInfo(); | |
| 129 | + $storeWeightUnit = $this->getStoreWeightUnit(); | |
| 130 | + $productWeight = floatval(Arr::get($otherInfo, 'weight', 0)); | |
| 131 | + $productWeightUnit = Arr::get($otherInfo, 'weight_unit', $storeWeightUnit); | |
| 132 | + | |
| 133 | + if (!$productWeight) { | |
| 134 | + return ''; | |
| 135 | + } | |
| 136 | + | |
| 137 | + $convertedWeight = Helper::convertWeight($productWeight, $productWeightUnit, $storeWeightUnit); | |
| 138 | + $formatted = rtrim(rtrim(number_format($convertedWeight, 2), '0'), '.'); | |
| 139 | + | |
| 140 | + return $formatted . ' ' . $storeWeightUnit; | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Get formatted shipping weight (product + package) from other_info. | |
| 145 | + */ | |
| 146 | + private function getFormattedShippingWeight() | |
| 147 | + { | |
| 148 | + $otherInfo = $this->getOtherInfo(); | |
| 149 | + $storeWeightUnit = $this->getStoreWeightUnit(); | |
| 150 | + | |
| 151 | + $productWeight = floatval(Arr::get($otherInfo, 'weight', 0)); | |
| 152 | + $productWeightUnit = Arr::get($otherInfo, 'weight_unit', $storeWeightUnit); | |
| 153 | + $convertedProductWeight = Helper::convertWeight($productWeight, $productWeightUnit, $storeWeightUnit); | |
| 154 | + | |
| 155 | + $packageWeight = floatval(Arr::get($otherInfo, 'package_weight', 0)); | |
| 156 | + $packageWeightUnit = Arr::get($otherInfo, 'package_weight_unit', $storeWeightUnit); | |
| 157 | + $convertedPackageWeight = Helper::convertWeight($packageWeight, $packageWeightUnit, $storeWeightUnit); | |
| 158 | + | |
| 159 | + $totalWeight = $convertedProductWeight + $convertedPackageWeight; | |
| 160 | + if (!$totalWeight) { | |
| 161 | + return ''; | |
| 162 | + } | |
| 163 | + | |
| 164 | + $formatted = rtrim(rtrim(number_format($totalWeight, 2), '0'), '.'); | |
| 165 | + | |
| 166 | + return $formatted . ' ' . $storeWeightUnit; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Get the store's weight unit, cached per request. | |
| 171 | + */ | |
| 172 | + private function getStoreWeightUnit() | |
| 173 | + { | |
| 174 | + static $unit = null; | |
| 175 | + if ($unit === null) { | |
| 176 | + $unit = Helper::shopConfig('weight_unit') ?: 'kg'; | |
| 177 | + } | |
| 178 | + return $unit; | |
| 54 | 179 | } |
| 55 | 180 | } |