Skip to content

Commit

Permalink
[Marketplace] Adds the ability to set metafields on cart items (#2704)
Browse files Browse the repository at this point in the history
* Adds the ability to set metafields to cart items

* fix typo
  • Loading branch information
mikemurray authored Aug 21, 2017
1 parent 54b989b commit 91d44f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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

0 comments on commit 91d44f5

Please sign in to comment.