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

Default to using shop country code or GeoIP result for new addresses #2515

Merged
merged 2 commits into from
Jul 2, 2017
Merged
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
56 changes: 41 additions & 15 deletions client/modules/accounts/templates/addressBook/add/add.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,67 @@
import { $ } from "meteor/jquery";
import { Session } from "meteor/session";
import { Meteor } from "meteor/meteor";
import { HTTP } from "meteor/http";
import { ReactiveVar } from "meteor/reactive-var";
import { AutoForm } from "meteor/aldeed:autoform";
import { Template } from "meteor/templating";
import { i18next } from "/client/api";
import { Reaction, i18next } from "/client/api";
import * as Collections from "/lib/collections";

Template.addressBookAdd.onCreated(function () {
this.currentCountry = new ReactiveVar(null);
// hit Reaction's GeoIP server and try to determine the user's country
HTTP.get("https://geo.getreaction.io/json/", (err, res) => {
if (!err) {
this.currentCountry.set(res.data.country_code);
}
});
});

Template.addressBookAdd.helpers({
thisAddress: function () {
thisAddress() {
const thisAddress = {};
// admin should receive his account

// admin should receive their account
const account = Collections.Accounts.findOne({
userId: Meteor.userId()
});
if (account) {
if (account.profile) {
if (account.profile.name) {
thisAddress.fullName = account.profile.name;
}
// if this will be the first address we set defaults here and not display
// them inside form
if (account.profile.addressBook) {
if (account.profile.addressBook.length === 0) {
thisAddress.isShippingDefault = true;
thisAddress.isBillingDefault = true;
}

if (account && account.profile) {
if (account.profile.name) {
thisAddress.fullName = account.profile.name;
}
// if this will be the first address we set defaults here and not display
// them inside form
if (account.profile.addressBook) {
if (account.profile.addressBook.length === 0) {
thisAddress.isShippingDefault = true;
thisAddress.isBillingDefault = true;
}
}
}

const shop = Collections.Shops.findOne(Reaction.getShopId());

// Set default country code based on shop's shipping address
if (shop && Array.isArray(shop.addressBook) && shop.addressBook.length > 0) {
const defaultAddress = shop.addressBook.find(address => address.isShippingDefault);
const defaultCountryCode = defaultAddress.country;
if (defaultCountryCode) {
thisAddress.country = defaultCountryCode;
}
}

if (Session.get("address")) {
thisAddress.postal = Session.get("address").zipcode;
thisAddress.country = Session.get("address").countryCode;
thisAddress.city = Session.get("address").city;
thisAddress.region = Session.get("address").state;
}

// update the reactive country code from the GeoIP lookup (if found)
thisAddress.country = Template.instance().currentCountry.get() || thisAddress.country;

return thisAddress;
},

Expand Down