Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Marketplace] Adds the ability to set metafields on cart items #2704

Merged
merged 3 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/collections/schemas/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Payment } from "./payments";
import { Product, ProductVariant } from "./products";
import { Shipment, ShippingParcel } from "./shipping";
import { Workflow } from "./workflow";
import { Metafield } from "./metafield";

/**
* CartItem Schema
Expand Down Expand Up @@ -35,6 +36,10 @@ export const CartItem = new SimpleSchema({
variants: {
type: ProductVariant
},
metafields: {
type: [Metafield],
optional: true
},
title: {
type: String,
label: "CartItem Title"
Expand Down
26 changes: 24 additions & 2 deletions server/methods/core/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,21 @@ Meteor.methods({
* @param {String} productId - productId to add to Cart
* @param {String} variantId - product variant _id
* @param {Number} [itemQty] - qty to add to cart
* @param {Object} [additionalOptions] - object containing additional options and fields for cart item
* @return {Number|Object} Mongo insert response
*/
"cart/addToCart": function (productId, variantId, itemQty) {
"cart/addToCart": function (productId, variantId, itemQty, additionalOptions) {
check(productId, String);
check(variantId, String);
check(itemQty, Match.Optional(Number));
check(additionalOptions, Match.Optional(Object));

// Copy additionalOptions into an options object to use througout the method
const options = {
overwriteExistingMetafields: false, // Allows updating of metafields on quantity change
metafields: undefined, // Array of MetaFields to set on the CartItem
...additionalOptions || {}
};

const cart = Collections.Cart.findOne({ userId: this.userId });
if (!cart) {
Expand Down Expand Up @@ -365,14 +374,26 @@ Meteor.methods({
.some(item => item.variants._id === variantId);

if (cartVariantExists) {
let modifier = {};

// Allows for updating metafields on an existing item when the quantity also changes
if (options.overwriteExistingMetafields) {
modifier = {
$set: {
"items.$.metafields": options.metafields
}
};
}

return Collections.Cart.update({
"_id": cart._id,
"items.product._id": productId,
"items.variants._id": variantId
}, {
$inc: {
"items.$.quantity": quantity
}
},
...modifier
}, function (error, result) {
if (error) {
Logger.warn("error adding to cart",
Expand Down Expand Up @@ -406,6 +427,7 @@ Meteor.methods({
quantity: quantity,
product: product,
variants: variant,
metafields: options.metafields,
title: product.title,
type: product.type,
parcel: product.parcel || null
Expand Down