@@ -68,8 +68,58 @@ class OrderItemsService {
6868 }
6969 }
7070
71+ getVariantFromProduct ( product , variantId ) {
72+ if ( product . variants && product . variants . length > 0 ) {
73+ return product . variants . find ( variant => variant . id . toString ( ) === variantId . toString ( ) ) ;
74+ } else {
75+ return null ;
76+ }
77+ }
78+
79+ getOptionFromProduct ( product , optionId ) {
80+ if ( product . options && product . options . length > 0 ) {
81+ return product . options . find ( item => item . id . toString ( ) === optionId . toString ( ) ) ;
82+ } else {
83+ return null ;
84+ }
85+ }
86+
87+ getOptionValueFromProduct ( product , optionId , valueId ) {
88+ const option = this . getOptionFromProduct ( product , optionId ) ;
89+ if ( option && option . values && option . values . length > 0 ) {
90+ return option . values . find ( item => item . id . toString ( ) === valueId . toString ( ) ) ;
91+ } else {
92+ return null ;
93+ }
94+ }
95+
96+ getOptionNameFromProduct ( product , optionId ) {
97+ const option = this . getOptionFromProduct ( product , optionId ) ;
98+ return option ? option . name : null ;
99+ }
100+
101+ getOptionValueNameFromProduct ( product , optionId , valueId ) {
102+ const value = this . getOptionValueFromProduct ( product , optionId , valueId )
103+ return value ? value . name : null ;
104+ }
105+
106+ getVariantNameFromProduct ( product , variantId ) {
107+ const variant = this . getVariantFromProduct ( product , variantId ) ;
108+ if ( variant ) {
109+ let optionNames = [ ] ;
110+ for ( const option of variant . options ) {
111+ const optionName = this . getOptionNameFromProduct ( product , option . option_id ) ;
112+ const optionValueName = this . getOptionValueNameFromProduct ( product , option . option_id , option . value_id ) ;
113+ optionNames . push ( `${ optionName } : ${ optionValueName } ` ) ;
114+ }
115+ return optionNames . join ( ', ' ) ;
116+ }
117+
118+ return null ;
119+ }
120+
71121 calculateAndUpdateItem ( order_id , item_id ) {
72- // TODO: variant_name, tax_total, discount_total
122+ // TODO: tax_total, discount_total
73123
74124 let orderObjectID = new ObjectID ( order_id ) ;
75125 let itemObjectID = new ObjectID ( item_id ) ;
@@ -79,16 +129,39 @@ class OrderItemsService {
79129 let item = order . items . find ( i => i . id . toString ( ) === item_id . toString ( ) ) ;
80130 if ( item ) {
81131 return ProductsService . getSingleProduct ( item . product_id ) . then ( product => {
82- let newItemData = {
83- 'items.$.sku' : product . sku ,
84- 'items.$.name' : product . name ,
85- 'items.$.variant_name' : '' ,
86- 'items.$.price' : product . price ,
87- 'items.$.tax_class' : product . tax_class ,
88- 'items.$.tax_total' : 0 ,
89- 'items.$.weight' : product . weight || 0 ,
90- 'items.$.discount_total' : 0 ,
91- 'items.$.price_total' : product . price * item . quantity
132+ let newItemData = null ;
133+ if ( item . variant_id ) {
134+ const variant = this . getVariantFromProduct ( product , item . variant_id ) ;
135+ const variantName = this . getVariantNameFromProduct ( product , item . variant_id ) ;
136+
137+ if ( ! variant ) {
138+ // variant not exists
139+ return null ;
140+ }
141+
142+ newItemData = {
143+ 'items.$.sku' : variant . sku ,
144+ 'items.$.name' : product . name ,
145+ 'items.$.variant_name' : variantName ,
146+ 'items.$.price' : variant . price ,
147+ 'items.$.tax_class' : product . tax_class ,
148+ 'items.$.tax_total' : 0 ,
149+ 'items.$.weight' : variant . weight || 0 ,
150+ 'items.$.discount_total' : 0 ,
151+ 'items.$.price_total' : variant . price * item . quantity
152+ }
153+ } else {
154+ newItemData = {
155+ 'items.$.sku' : product . sku ,
156+ 'items.$.name' : product . name ,
157+ 'items.$.variant_name' : '' ,
158+ 'items.$.price' : product . price ,
159+ 'items.$.tax_class' : product . tax_class ,
160+ 'items.$.tax_total' : 0 ,
161+ 'items.$.weight' : product . weight || 0 ,
162+ 'items.$.discount_total' : 0 ,
163+ 'items.$.price_total' : product . price * item . quantity
164+ }
92165 }
93166
94167 return mongo . db . collection ( 'orders' ) . updateOne ( {
0 commit comments