Skip to content

Commit

Permalink
Merge pull request #2034 from reactioncommerce/joykare-inventory-trac…
Browse files Browse the repository at this point in the history
…king-1928

"Deny when out of stock" behavior improvements  1928
  • Loading branch information
Aaron Judd authored Mar 29, 2017
2 parents bc16da9 + 634975e commit fad6051
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { composeWithTracker } from "/lib/api/compose";
import { Meteor } from "meteor/meteor";
import { ReactionProduct } from "/lib/api";
import { Reaction, i18next, Logger } from "/client/api";
import { Tags, Media } from "/lib/collections";
import { Tags, Media, Cart } from "/lib/collections";
import { Loading } from "/imports/plugins/core/ui/client/components";
import { ProductDetail, ProductNotFound } from "../components";
import { SocialContainer, VariantListContainer } from "./";
Expand All @@ -30,6 +30,9 @@ class ProductDetailContainer extends Component {
handleAddToCart = () => {
let productId;
let quantity;
let maxQuantity;
let totalQuantity;
let storedQuantity = 0;
const currentVariant = ReactionProduct.selectedVariant();
const currentProduct = ReactionProduct.selectedProduct();

Expand All @@ -56,12 +59,51 @@ class ProductDetailContainer extends Component {
return [];
}

if (this.props.storedCart && this.props.storedCart.items) {
this.props.storedCart.items.forEach((item) => {
if (item.variants._id === currentVariant._id) {
storedQuantity = item.quantity;
}
});
}

quantity = parseInt(this.state.cartQuantity, 10);
totalQuantity = quantity + storedQuantity;
maxQuantity = currentVariant.inventoryQuantity;

if (quantity < 1) {
quantity = 1;
}

if (currentVariant.inventoryPolicy && quantity > maxQuantity && storedQuantity < maxQuantity) {
Alerts.inline("Your product quantity has been adjusted to the max quantity in stock", "warning", {
placement: "productDetail",
i18nKey: "admin.inventoryAlerts.adjustedQuantity",
autoHide: 10000
});
quantity = maxQuantity - storedQuantity;
totalQuantity = maxQuantity;
}

if (currentVariant.inventoryPolicy && totalQuantity > maxQuantity && storedQuantity < maxQuantity && quantity < maxQuantity) {
Alerts.inline("Your product quantity has been adjusted to the max quantity in stock", "warning", {
placement: "productDetail",
i18nKey: "admin.inventoryAlerts.adjustedQuantity",
autoHide: 10000
});
quantity = maxQuantity - storedQuantity;
totalQuantity = maxQuantity;
}

if (currentVariant.inventoryPolicy && totalQuantity > maxQuantity) {
Alerts.inline("Sorry, this item is out of stock!", "error", {
placement: "productDetail",
i18nKey: "productDetail.outOfStock",
autoHide: 10000
});
return [];
}

if (!currentProduct.isVisible) {
Alerts.inline("Publish product before adding to cart.", "error", {
placement: "productDetail",
Expand Down Expand Up @@ -182,7 +224,8 @@ class ProductDetailContainer extends Component {

ProductDetailContainer.propTypes = {
media: PropTypes.arrayOf(PropTypes.object),
product: PropTypes.object
product: PropTypes.object,
storedCart: PropTypes.object
};

function composer(props, onData) {
Expand All @@ -198,7 +241,7 @@ function composer(props, onData) {
productSub = Meteor.subscribe("Product", productId);
}

if (productSub && productSub.ready() && tagSub.ready()) {
if (productSub && productSub.ready() && tagSub.ready() && Reaction.Subscriptions.Cart.ready()) {
// Get the product
const product = ReactionProduct.setProduct(productId, variantId);

Expand Down Expand Up @@ -282,6 +325,8 @@ function composer(props, onData) {

const topVariants = ReactionProduct.getTopVariants();

const storedCart = Cart.findOne();

onData(null, {
variants: topVariants,
layout: product.template || "productDetailSimple",
Expand All @@ -291,7 +336,8 @@ function composer(props, onData) {
media: mediaArray,
editable,
viewAs: viewProductAs,
hasAdminPermission: Reaction.hasPermission(["createProduct"])
hasAdminPermission: Reaction.hasPermission(["createProduct"]),
storedCart
});
} else {
// onData must be called with composeWithTracker, or else the loading icon will show forever.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"admin": {
"settings": {
"productDetailsLabel": "Product Details"
},
"inventoryAlerts": {
"adjustedQuantity": "Your product quantity has been adjusted to the max quantity in stock"
}
}
}
Expand Down

0 comments on commit fad6051

Please sign in to comment.