Skip to content

Commit bad5808

Browse files
committed
Added path to cart items
1 parent 118665e commit bad5808

10 files changed

Lines changed: 294 additions & 81 deletions

File tree

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cezerin",
3-
"version": "0.13.0",
3+
"version": "0.14.0",
44
"description": "Cezerin allow you to create online store with full-stack JavaScript. https://cezerin.com",
55
"keywords": [
66
"cezerin",
@@ -67,7 +67,7 @@
6767
"json-loader": "^0.5.4",
6868
"jsonwebtoken": "^7.3.0",
6969
"lru-cache": "^4.0.2",
70-
"material-ui": "^0.17.3",
70+
"material-ui": "^0.17.4",
7171
"moment": "^2.18.1",
7272
"mongodb": "^2.2.25",
7373
"nodemailer": "^4.0.1",
@@ -91,7 +91,7 @@
9191
"slug": "^0.9.1",
9292
"theme": "file:themes/current",
9393
"ua-parser-js": "^0.7.12",
94-
"webpack": "^2.4.0",
94+
"webpack": "^2.4.1",
9595
"winston": "^2.3.1"
9696
},
9797
"devDependencies": {

src/api/server/services/orders/order_items.js

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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({

src/store/server/ajax.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,25 @@ const DEFAULT_CACHE_CONTROL = 'public, max-age=600';
88
const PRODUCTS_CACHE_CONTROL = 'public, max-age=60';
99
const PRODUCT_DETAILS_CACHE_CONTROL = 'public, max-age=60';
1010

11+
const getVariantFromProduct = (product, variantId) => {
12+
if(product.variants && product.variants.length > 0) {
13+
return product.variants.find(variant => variant.id.toString() === variantId.toString());
14+
} else {
15+
return null;
16+
}
17+
}
18+
1119
const fillCartItemWithProductData = (products, cartItem) => {
1220
const product = products.find(p => p.id === cartItem.product_id);
1321
if(product) {
1422
cartItem.image_url = product.images.length > 0 ? product.images[0].url : null;
15-
cartItem.stock_quantity = product.stock_quantity;
23+
cartItem.path = product.path;
24+
if(cartItem.variant_id && cartItem.variant_id.length > 0) {
25+
const variant = getVariantFromProduct(product, cartItem.variant_id);
26+
cartItem.stock_quantity = variant ? variant.stock_quantity : 0;
27+
} else {
28+
cartItem.stock_quantity = product.stock_quantity;
29+
}
1630
}
1731
return cartItem;
1832
}
@@ -21,7 +35,7 @@ const fillCartItems = (cartResponse) => {
2135
let cart = cartResponse.json;
2236
if(cart && cart.items && cart.items.length > 0) {
2337
const productIds = cart.items.map(item => item.product_id);
24-
return api.products.list({ ids: productIds, fields: 'images,enabled,stock_quantity' }).then(({status, json}) => {
38+
return api.products.list({ ids: productIds, fields: 'images,enabled,stock_quantity,variants,path' }).then(({status, json}) => {
2539
const newCartItem = cart.items.map(cartItem => fillCartItemWithProductData(json.data, cartItem))
2640
cartResponse.json.items = newCartItem;
2741
return cartResponse;

themes/current/assets/css/theme.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,16 @@ hr.separator {
163163
.product-new-price {
164164
color: #FE4365;
165165
}
166+
.product-option {
167+
margin-bottom: 15px;
168+
}
169+
.product-option-name {
170+
color: #777;
171+
}
172+
173+
.cart-option-name {
174+
color: #999;
175+
}
166176

167177
.categories-tree {
168178
}

themes/current/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
"sort_newest": "-date_created",
1111
"sort_price_low": "price",
1212
"sort_price_high": "-price",
13-
"infiniteScrolling": false
13+
"infiniteScrolling": false,
14+
"maxCartItemQty": 100
1415
}

themes/current/locales/en.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
"sortPriceLow":"Price low to high",
2727
"sortPriceHigh":"Price high to low",
2828
"title404": "Page not found",
29-
"loadMore": "Load more products",
29+
"loadMore": "Show more products",
3030
"text404": "The page you requested does not exist. Click here to continue shopping.",
3131
"search": "Search",
3232
"searchPlaceholder": "Search",
33-
"resultsFor": "results for"
33+
"resultsFor": "results for",
34+
"selectOption": "Select",
35+
"optionsRequired": "You need to choose options"
3436
}

themes/current/package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12-
"babel-cli": "^6.24.0",
13-
"babel-core": "^6.24.0",
12+
"babel-cli": "^6.24.1",
13+
"babel-core": "^6.24.1",
1414
"babel-loader": "^6.4.1",
15-
"babel-plugin-transform-class-properties": "^6.23.0",
16-
"babel-preset-latest": "^6.24.0",
17-
"babel-preset-react": "^6.23.0",
18-
"rc-slider": "^6.3.1",
19-
"react": "^15.4.2",
20-
"react-dom": "^15.4.2",
21-
"react-helmet": "^5.0.2",
15+
"babel-plugin-transform-class-properties": "^6.24.1",
16+
"babel-preset-latest": "^6.24.1",
17+
"babel-preset-react": "^6.24.1",
18+
"rc-slider": "^7.0.1",
19+
"react": "^15.5.4",
20+
"react-dom": "^15.5.4",
21+
"react-helmet": "^5.0.3",
2222
"react-image-gallery": "^0.7.15",
23-
"react-router": "^3.0.3",
24-
"react-waypoint": "^5.2.1"
23+
"react-router": "^3.0.5",
24+
"react-waypoint": "^5.3.1"
2525
}
2626
}

themes/current/src/components/cart.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ const CartItem = ({item, deleteCartItem, settings}) => {
1515
</div>
1616
</div>
1717
<div className="column">
18-
<a>{item.name}</a><br />
19-
<span>{text.qty}: {item.quantity}</span>
18+
<div><a>{item.name}</a></div>
19+
{item.variant_name.length > 0 &&
20+
<div className="cart-option-name">{item.variant_name}</div>
21+
}
22+
<div>{text.qty}: {item.quantity}</div>
2023
</div>
2124
<div className="column is-4 has-text-right">
2225
<div className="mini-cart-item-price">{helper.formatCurrency(item.price_total, settings)}</div>

themes/current/src/components/orderSummary.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import text from '../lib/text'
44
import config from '../lib/config'
55
import * as helper from '../lib/helper'
66

7-
const MAX_CART_ITEM_QTY = 10;
8-
97
const SummaryItem = ({settings, item, deleteCartItem, updateCartItemQuantiry}) => {
108
const thumbnail = helper.getThumbnailUrl(item.image_url, config.cart_thumbnail_width);
119
const qtyOptions = [];
12-
const maxQty = item.stock_quantity >= MAX_CART_ITEM_QTY ? MAX_CART_ITEM_QTY : item.stock_quantity;
10+
const maxQty = item.stock_quantity >= config.maxCartItemQty ? config.maxCartItemQty : item.stock_quantity;
1311

1412
for(let i = 0; i <= maxQty; i++){
1513
const optionText = i === 0 ? text.remove : i;
@@ -24,10 +22,11 @@ const SummaryItem = ({settings, item, deleteCartItem, updateCartItemQuantiry}) =
2422
</div>
2523
</div>
2624
<div className="column">
27-
<p>
28-
{item.name}
29-
</p>
30-
<div className="columns is-mobile is-gapless is-flex" style={{ alignItems: 'center' }}>
25+
<div>{item.name}</div>
26+
{item.variant_name.length > 0 &&
27+
<div className="cart-option-name">{item.variant_name}</div>
28+
}
29+
<div className="columns is-mobile is-gapless is-flex" style={{ alignItems: 'center', margin: '5px 0' }}>
3130
<div className="column is-2">
3231
{text.qty}:
3332
</div>

0 commit comments

Comments
 (0)