| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\ShortCodeParser\Parsers; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
class ItemParser extends BaseParser |
| 9 |
{ |
| 10 |
private $item; |
| 11 |
private $order; |
| 12 |
|
| 13 |
public function __construct($data) |
| 14 |
{ |
| 15 |
$this->item = Arr::get($data, 'item', []); |
| 16 |
$this->order = Arr::get($data, 'order'); |
| 17 |
parent::__construct($data); |
| 18 |
} |
| 19 |
|
| 20 |
public function parse($accessor = null, $code = null, $transformer = null): ?string |
| 21 |
{ |
| 22 |
$item = $this->item; |
| 23 |
|
| 24 |
switch ($accessor) { |
| 25 |
case 'sl': |
| 26 |
return (string) ($item['sl'] ?? ''); |
| 27 |
case 'name': |
| 28 |
return esc_html(!empty($item['post_title']) ? $item['post_title'] : ($item['title'] ?? '')); |
| 29 |
case 'variant': |
| 30 |
return esc_html($item['title'] ?? ''); |
| 31 |
case 'quantity': |
| 32 |
return esc_html($item['quantity'] ?? ''); |
| 33 |
case 'price': |
| 34 |
return isset($item['line_total']) ? (string) ($item['line_total'] / 100) : ''; |
| 35 |
case 'price_formatted': |
| 36 |
return $item['formatted_total'] ?? ''; |
| 37 |
case 'unit_price': |
| 38 |
return isset($item['unit_price']) ? (string) ($item['unit_price'] / 100) : ''; |
| 39 |
case 'unit_price_formatted': |
| 40 |
$unitPrice = isset($item['unit_price']) ? Helper::toDecimal($item['unit_price']) : ''; |
| 41 |
return esc_html($unitPrice); |
| 42 |
case 'subtotal': |
| 43 |
return isset($item['subtotal']) ? (string) ($item['subtotal'] / 100) : ''; |
| 44 |
case 'subtotal_formatted': |
| 45 |
$subtotal = isset($item['subtotal']) ? Helper::toDecimal($item['subtotal']) : ''; |
| 46 |
return esc_html($subtotal); |
| 47 |
case 'payment_info': |
| 48 |
return wp_kses_post($item['payment_info'] ?? ''); |
| 49 |
case 'payment_type': |
| 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()); |
| 61 |
default: |
| 62 |
return Arr::get($item, $accessor, ''); |
| 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; |
| 179 |
} |
| 180 |
} |
| 181 |
|