@@ -30,33 +30,45 @@ class CartLineItem {
3030 String ? variationOptions;
3131 List <ws_product.Category >? categories;
3232 bool ? onSale;
33+ String ? salePrice;
34+ String ? regularPrice;
3335 String ? stockStatus;
34- Object ? metaData = {};
36+ List <Map <String ?, dynamic >> metaData = [];
37+ List <Map <String ?, dynamic >> appMetaData = [];
3538
3639 CartLineItem (
3740 {this .name,
38- this .productId,
39- this .variationId,
40- this .isManagedStock,
41- this .stockQuantity,
42- this .quantity = 1 ,
43- this .stockStatus,
44- this .shippingClassId,
45- this .taxStatus,
46- this .taxClass,
47- this .categories,
48- this .shippingIsTaxable,
49- this .variationOptions,
50- this .imageSrc,
51- this .subtotal,
52- this .onSale,
53- this .total,
54- this .metaData});
41+ this .productId,
42+ this .variationId,
43+ this .isManagedStock,
44+ this .stockQuantity,
45+ this .quantity = 1 ,
46+ this .stockStatus,
47+ this .shippingClassId,
48+ this .taxStatus,
49+ this .taxClass,
50+ this .categories,
51+ this .shippingIsTaxable,
52+ this .variationOptions,
53+ this .imageSrc,
54+ this .subtotal,
55+ this .onSale,
56+ this .salePrice,
57+ this .regularPrice,
58+ this .total,
59+ this .metaData = const []});
5560
5661 String getCartTotal () {
5762 return (quantity * parseWcPrice (subtotal)).toStringAsFixed (2 );
5863 }
5964
65+ String getPrice ({bool formatToCurrency = false }) {
66+ if (formatToCurrency) {
67+ return formatStringCurrency (total: (parseWcPrice (subtotal)).toStringAsFixed (2 ));
68+ }
69+ return (parseWcPrice (subtotal)).toStringAsFixed (2 );
70+ }
71+
6072 CartLineItem .fromProduct (
6173 {int ? quantityAmount, required ws_product.Product product}) {
6274 name = product.name;
@@ -69,18 +81,24 @@ class CartLineItem {
6981 categories = product.categories;
7082 isManagedStock = product.manageStock;
7183 stockQuantity = product.stockQuantity;
84+ salePrice = product.salePrice;
85+ onSale = product.onSale;
86+ regularPrice = product.regularPrice;
7287 shippingIsTaxable = product.shippingTaxable;
88+ List <Map <String ?, String ?>> data = product.metaData.map ((e) => {e.key: e.value}).toList ();
89+ metaData.addAll (data);
7390 imageSrc = product.images.isEmpty
7491 ? getEnv ("PRODUCT_PLACEHOLDER_IMAGE" )
7592 : product.images.first.src;
7693 total = product.price;
94+ appMetaData = product.metaData.map ((e) => {e.key: e.value}).toList ();
7795 }
7896
7997 CartLineItem .fromProductVariation (
8098 {int ? quantityAmount,
81- required List <String > options,
82- required ws_product.Product product,
83- required ProductVariation productVariation}) {
99+ required List <String > options,
100+ required ws_product.Product product,
101+ required ProductVariation productVariation}) {
84102 String ? imageSrc = getEnv ("PRODUCT_PLACEHOLDER_IMAGE" );
85103 if (product.images.isNotEmpty) {
86104 imageSrc = product.images.first.src;
@@ -99,38 +117,49 @@ class CartLineItem {
99117 isManagedStock = productVariation.manageStock;
100118 categories = product.categories;
101119 taxClass = productVariation.taxClass;
120+ onSale = productVariation.onSale;
102121 this .imageSrc = imageSrc;
122+ salePrice = productVariation.salePrice;
123+ List <Map <String ?, String ?>> data = product.metaData.map ((e) => {e.key: e.value}).toList ();
124+ metaData.addAll (data);
125+ regularPrice = productVariation.regularPrice;
103126 shippingIsTaxable = product.shippingTaxable;
104127 variationOptions = options.join ("; " );
105128 total = productVariation.price;
129+ appMetaData = product.metaData.map ((e) => {"key" : e.key, "value" : e.value}).toList ();
130+ }
131+
132+ CartLineItem .fromJson (Map <String , dynamic > json) {
133+ name = json['name' ];
134+ productId = json['product_id' ];
135+ variationId = json['variation_id' ];
136+ quantity = json['quantity' ];
137+ shippingClassId = json['shipping_class_id' ].toString ();
138+ taxStatus = json['tax_status' ];
139+ stockQuantity = json['stock_quantity' ];
140+ isManagedStock = (json['is_managed_stock' ] != null &&
141+ json['is_managed_stock' ] is bool ) ? json['is_managed_stock' ] : false ;
142+ shippingIsTaxable = json['shipping_is_taxable' ];
143+ subtotal = json['subtotal' ];
144+ total = json['total' ];
145+ taxClass = json['tax_class' ];
146+ stockStatus = json['stock_status' ];
147+ imageSrc = json['image_src' ];
148+ salePrice = json['sale_price' ];
149+ regularPrice = json['regular_price' ];
150+ categories = json['categories' ] == null ? null : List .of (json['categories' ] as List ).map ((e) => ws_product.Category .fromJson (e)).toList ();
151+ onSale = json['on_sale' ];
152+ variationOptions = json['variation_options' ];
153+ metaData = [];
154+ if (json['meta_data' ] != null ) {
155+ metaData = List .from (json['meta_data' ]).cast <Map <String , dynamic >>();
156+ }
157+ appMetaData = [];
158+ if (json['app_meta_data' ] != null ) {
159+ appMetaData = List .from (json['app_meta_data' ]).cast <Map <String , dynamic >>();
160+ }
106161 }
107162
108- CartLineItem .fromJson (Map <String , dynamic > json)
109- : name = json['name' ],
110- productId = json['product_id' ],
111- variationId = json['variation_id' ],
112- quantity = json['quantity' ],
113- shippingClassId = json['shipping_class_id' ].toString (),
114- taxStatus = json['tax_status' ],
115- stockQuantity = json['stock_quantity' ],
116- isManagedStock = (json['is_managed_stock' ] != null &&
117- json['is_managed_stock' ] is bool )
118- ? json['is_managed_stock' ]
119- : false ,
120- shippingIsTaxable = json['shipping_is_taxable' ],
121- subtotal = json['subtotal' ],
122- total = json['total' ],
123- taxClass = json['tax_class' ],
124- stockStatus = json['stock_status' ],
125- imageSrc = json['image_src' ],
126- categories = json['categories' ] == null
127- ? null
128- : List .of (json['categories' ] as List )
129- .map ((e) => ws_product.Category .fromJson (e))
130- .toList (),
131- onSale = json['on_sale' ],
132- variationOptions = json['variation_options' ],
133- metaData = json['metaData' ];
134163
135164 Map <String , dynamic > toJson () => {
136165 'name' : name,
@@ -153,5 +182,6 @@ class CartLineItem {
153182 'on_sale' : onSale,
154183 'total' : total,
155184 'meta_data' : metaData,
185+ 'app_meta_data' : appMetaData,
156186 };
157187}
0 commit comments