| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | use FluentCart\App\Models\Concerns\CanSearch; |
| 8 | 8 | use FluentCart\App\Models\Concerns\CanUpdateBatch; |
| 9 | 9 | use FluentCart\App\Models\WpModels\PostMeta; |
| 10 | 10 | use FluentCart\Framework\Database\Orm\Relations\HasOne; |
| 11 | +use FluentCart\Framework\Support\Arr; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * OrderItem Model - DB Model for Order Items |
| 14 | 15 | * |
| @@ -54,19 +55,19 @@ | ||
| 54 | 55 | 'payment_type', |
| 55 | 56 | 'created_at', |
| 56 | 57 | // 'shipping_charge' |
| 57 | 58 | ]; |
| 58 | - protected $appends = ['payment_info', 'setup_info', 'is_custom']; | |
| 59 | + protected $appends = ['payment_info', 'setup_info', 'is_custom', 'variation_display_title']; | |
| 59 | 60 | |
| 60 | 61 | protected $casts = [ |
| 61 | - 'unit_price' => 'double', | |
| 62 | - 'cost' => 'double', | |
| 63 | - 'subtotal' => 'double', | |
| 64 | - 'tax_amount' => 'double', | |
| 65 | - 'shipping_charge' => 'double', | |
| 66 | - 'discount_total' => 'double', | |
| 67 | - 'line_total' => 'double', | |
| 68 | - 'refund_total' => 'double', | |
| 62 | + 'unit_price' => 'double', | |
| 63 | + 'cost' => 'double', | |
| 64 | + 'subtotal' => 'double', | |
| 65 | + 'tax_amount' => 'double', | |
| 66 | + 'shipping_charge' => 'double', | |
| 67 | + 'discount_total' => 'double', | |
| 68 | + 'line_total' => 'double', | |
| 69 | + 'refund_total' => 'double', | |
| 69 | 70 | ]; |
| 70 | 71 | |
| 71 | 72 | protected static function booted() |
| 72 | 73 | { |
| @@ -76,11 +77,19 @@ | ||
| 76 | 77 | } |
| 77 | 78 | |
| 78 | 79 | protected function getFormattedTotalAttribute() |
| 79 | 80 | { |
| 80 | - return Helper::toDecimal($this->subtotal); | |
| 81 | + if ($this->line_total == 0 && $this->discount_total == 0) { | |
| 82 | + return Helper::toDecimal($this->subtotal); | |
| 83 | + } | |
| 84 | + return Helper::toDecimal($this->line_total); | |
| 81 | 85 | } |
| 82 | 86 | |
| 87 | + public function getCouponDiscountAttribute() | |
| 88 | + { | |
| 89 | + return (int)($this->line_meta['coupon_discount'] ?? 0); | |
| 90 | + } | |
| 91 | + | |
| 83 | 92 | public function setOtherInfoAttribute($value) |
| 84 | 93 | { |
| 85 | 94 | if (is_array($value) || is_object($value)) { |
| 86 | 95 | $this->attributes['other_info'] = json_encode($value); |
| @@ -226,6 +235,36 @@ | ||
| 226 | 235 | { |
| 227 | 236 | return $this->is_custom |
| 228 | 237 | ? ($this->other_info['view_url'] ?? '') |
| 229 | 238 | : ''; |
| 239 | + } | |
| 240 | + | |
| 241 | + public function getDisplayTitle() | |
| 242 | + { | |
| 243 | + if ($this->post_title == $this->title) { | |
| 244 | + return $this->title; | |
| 245 | + } | |
| 246 | + | |
| 247 | + return $this->post_title . ' - ' . $this->title; | |
| 248 | + | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 252 | + * Labeled attribute combination resolved from the frozen | |
| 253 | + * other_info['item_attributes'] snapshot, e.g. "Color: Red | Size: XS". | |
| 254 | + * Falls back to the stored variation title when no attribute snapshot | |
| 255 | + * resolves (custom/simple items, or attrs whose groups were removed). | |
| 256 | + * | |
| 257 | + * @return string | |
| 258 | + */ | |
| 259 | + public function getVariationDisplayTitleAttribute() | |
| 260 | + { | |
| 261 | + // Order items carry the item_attributes snapshot (written at order | |
| 262 | + // creation), so resolve directly — getDisplayAttributesString renders | |
| 263 | + // the labeled combination and falls back to the variation title. | |
| 264 | + return \FluentCart\App\Helpers\AttributeHelper::getDisplayAttributesString( | |
| 265 | + Arr::get($this->other_info, 'item_attributes', []), | |
| 266 | + $this, | |
| 267 | + 'order_item' | |
| 268 | + ); | |
| 230 | 269 | } |
| 231 | 270 | } |