Skip to content

Commit cdf78c7

Browse files
committed
introducing custom attributes for cart and line items
1 parent 42af33b commit cdf78c7

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

Diff for: src/components/cart.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,14 @@ export default class Cart extends Component {
420420
* @param {Object} variant - variant object.
421421
* @param {Number} [quantity=1] - quantity to be added.
422422
*/
423-
addVariantToCart(variant, quantity = 1, openCart = true) {
423+
addVariantToCart(variant, quantity = 1, customAttributes = [], openCart = true) {
424424
if (quantity <= 0) {
425425
return null;
426426
}
427427
if (openCart) {
428428
this.open();
429429
}
430-
const lineItem = {variantId: variant.id, quantity};
430+
const lineItem = {variantId: variant.id, quantity, customAttributes};
431431
if (this.model) {
432432
return this.props.client.checkout.addLineItems(this.model.id, [lineItem]).then((checkout) => {
433433
this.model = checkout;
@@ -444,6 +444,7 @@ export default class Cart extends Component {
444444
lineItems: [
445445
lineItem,
446446
],
447+
customAttributes: this.options.customAttributes || [],
447448
};
448449
return this.props.client.checkout.create(input).then((checkout) => {
449450
localStorage.setItem(this.localStorageCheckoutKey, checkout.id);

Diff for: src/components/product.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export default class Product extends Component {
8585
this.modalProduct = Boolean(config.modalProduct);
8686
this.updater = new ProductUpdater(this);
8787
this.view = new ProductView(this);
88+
this.customAttributes = config.customAttributes || [];
8889
}
8990

9091
/**
@@ -248,6 +249,7 @@ export default class Product extends Component {
248249
buttonDisabled: !this.buttonEnabled,
249250
selectedVariant: this.selectedVariant,
250251
selectedQuantity: this.selectedQuantity,
252+
customAttributes: this.customAttributes,
251253
buttonText: this.buttonText,
252254
imgStyle: this.imgStyle,
253255
quantityClass: this.quantityClass,
@@ -651,7 +653,7 @@ export default class Product extends Component {
651653
} else if (this.options.buttonDestination === 'cart') {
652654
this.props.closeModal();
653655
this._userEvent('addVariantToCart');
654-
this.props.tracker.trackMethod(this.cart.addVariantToCart.bind(this), 'Update Cart', this.selectedVariantTrackingInfo)(this.selectedVariant, this.selectedQuantity);
656+
this.props.tracker.trackMethod(this.cart.addVariantToCart.bind(this), 'Update Cart', this.selectedVariantTrackingInfo)(this.selectedVariant, this.selectedQuantity, this.customAttributes);
655657
if (!this.modalProduct) {
656658
this.props.setActiveEl(target);
657659
}
@@ -677,6 +679,7 @@ export default class Product extends Component {
677679
{
678680
variantId: this.selectedVariant.id,
679681
quantity: this.selectedQuantity,
682+
customAttributes: this.customAttributes,
680683
},
681684
],
682685
};

Diff for: test/unit/cart/cart.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,11 @@ describe('Cart class', () => {
694694
const modelId = 135;
695695
const variantId = 1111;
696696
const quantity = 2;
697+
const customAttributes = [];
697698
const variant = {
698699
id: variantId,
699700
};
700-
const lineItem = {variantId, quantity};
701+
const lineItem = {variantId, quantity, customAttributes};
701702
let cartOpenStub;
702703
let setFocusStub;
703704
let addLineItemsStub;
@@ -741,13 +742,13 @@ describe('Cart class', () => {
741742
});
742743

743744
it('calls open on cart if openCart parameter is true', () => {
744-
return cart.addVariantToCart(variant, quantity, true).then(() => {
745+
return cart.addVariantToCart(variant, quantity, [], true).then(() => {
745746
assert.calledOnce(cartOpenStub);
746747
});
747748
});
748749

749750
it('does not call open on cart if openCart parameter is false', () => {
750-
return cart.addVariantToCart(variant, quantity, false).then(() => {
751+
return cart.addVariantToCart(variant, quantity, [], false).then(() => {
751752
assert.notCalled(cartOpenStub);
752753
});
753754
});
@@ -758,7 +759,7 @@ describe('Cart class', () => {
758759
};
759760

760761
return cart.addVariantToCart(variant).then(() => {
761-
assert.calledWith(addLineItemsStub, modelId, [{variantId, quantity: 1}]);
762+
assert.calledWith(addLineItemsStub, modelId, [{variantId, quantity: 1, customAttributes: []}]);
762763
});
763764
});
764765

@@ -779,13 +780,13 @@ describe('Cart class', () => {
779780
});
780781

781782
it('does not call setFocus if the openCart parameter is true', () => {
782-
return cart.addVariantToCart(variant, quantity, true).then(() => {
783+
return cart.addVariantToCart(variant, quantity, [], true).then(() => {
783784
assert.notCalled(setFocusStub);
784785
});
785786
});
786787

787788
it('calls setFocus if the openCart parameter is false', () => {
788-
return cart.addVariantToCart(variant, quantity, false).then(() => {
789+
return cart.addVariantToCart(variant, quantity, [], false).then(() => {
789790
assert.calledOnce(setFocusStub);
790791
});
791792
});
@@ -805,19 +806,19 @@ describe('Cart class', () => {
805806
it('creates a checkout with line item and returns the updated checkout if cart model is null', () => {
806807
return cart.addVariantToCart(variant, quantity).then((checkout) => {
807808
assert.calledOnce(checkoutCreateStub);
808-
assert.calledWith(checkoutCreateStub, {lineItems: [lineItem]});
809+
assert.calledWith(checkoutCreateStub, {lineItems: [lineItem], customAttributes: []});
809810
assert.deepEqual(checkout, mockCheckout);
810811
});
811812
});
812813

813814
it('does not call setFocus if the openCart parameter is true', () => {
814-
return cart.addVariantToCart(variant, quantity, true).then(() => {
815+
return cart.addVariantToCart(variant, quantity, [], true).then(() => {
815816
assert.notCalled(setFocusStub);
816817
});
817818
});
818819

819820
it('calls setFocus if openCart parameter is false', () => {
820-
return cart.addVariantToCart(variant, quantity, false).then(() => {
821+
return cart.addVariantToCart(variant, quantity, [], false).then(() => {
821822
assert.calledOnce(setFocusStub);
822823
});
823824
});

Diff for: test/unit/product/product-component.js

+1
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@ describe('Product Component class', () => {
676676
assert.calledWith(createCheckoutStub, {lineItems: [{
677677
variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMjM0NQ==',
678678
quantity: selectedQuantity,
679+
customAttributes: [],
679680
}]});
680681
});
681682
});

0 commit comments

Comments
 (0)