Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kieckhafer
Copy link
Member

@kieckhafer kieckhafer commented Nov 26, 2018

Impact: minor
Type: feature

There is a matching PR in the starterkit to go along with this PR. It might be beneficial to test them both at the same time, as the starterkit PR requires the changes made in this PR: reactioncommerce/example-storefront#449

Issue

Shops should be able to apply surcharges based on shipping location and attributes of items inside an order (price, brand, type of item, etc). This is a similar feature, and could be seen as an extension of the work done for shipping restrictions: #4821

Solution

  • Create both universal and per fulfillment method ways to add surcharges to an order based on the order destination, and / or attributes attached to items in an order. This is done by creating a Surcharges collection, and applying the surcharges to an order if criteria matches.

Breaking changes

None. These are add-on features. If no surcharges are found, nothing is different.

Testing

By default, surcharges can be tested against tags, and any product attribute listed here, any any user info such as shipping address.

Creating / modifying surcharges

Use the following GQL queries / mutations to test creating, deleting, updating and getting a single surcharge, and getting all surcharges.

Get all surcharges for a shop

query {
  getSurcharges(shopId: "shopId-GOES-HERE") {
    edges {
      node {
        _id
        shopId
        message
        methodIds
        type
        amount {
          amount
          displayAmount
        }
        message(language: "LANGUAGE-CODE")
      }
    }
  }
}

Get a single surcharge

Use the Get all surcharges for a shop query to get the ID of a surcharge, then preform the following:

query {
  getSurcharge(shopId: "shopId-GOES-HERE", surchargeId: "surchargeId-GOES-HERE") {
    _id
    shopId
    message
    methodIds
    type
    amount {
      amount
      displayAmount
    }
    message(language: "LANGUAGE-CODE")
  }
}

Get a single surcharge in a non-default language (be sure to have added a surcharge message with a second language)

Use the Get all surcharges for a shop query to get the ID of a surcharge, then preform the following:

query {
  getSurcharge(shopId: "shopId-GOES-HERE", surchargeId: "surchargeId-GOES-HERE") {
    _id
    shopId
    message
    methodIds
    type
    amount {
      amount
      displayAmount
    }
    message(language: "LANGUAGE-CODE")
  }
}

Create a surcharge

mutation createSurcharge($input: CreateSurchargeInput!) {
  createSurcharge(input: $input) {
    clientMutationId
    surcharge {
      _id
      amount {
        amount
        displayAmount
      }
      attributes {
        property
        value
        propertyType
        operator
      }
      destination {
        region
      }
      message
      methodIds
    	type
    }
  }
}


Variables:
{
  "input": {
    "shopId": "shopId-GOES-HERE",
    "surcharge": {
      "amount": "amount": 999.99,
      "messagesByLanguage": [
        {
        	"content": "This is a message in english",
        	"language": "en"
        },{ 
          "content": "Este es un mensaje en espanol",
          "language": "es"
        }
      ],
      "methodIds": ["a","b"],
      "type": "surcharge",
      "attributes": [
        { "property": "productVendor", "value": "reaction", "propertyType": "string", "operator": "eq" },
        { "property": "variantTitle", "value": "Option 1 - Red Dwarf", "propertyType": "string", "operator": "eq" }
      ],
      "destination": { "region": ["CO", "NY"] }
    }
  }
}

Update a surcharge

mutation updateSurcharge($input: UpdateSurchargeInput!) {
  updateSurcharge(input: $input) {
    clientMutationId
    surcharge {
      amount {
        amount
        displayAmount
      }
      attributes {
        property
        value
        propertyType
        operator
      }
      destination {
        region
      }
      message
      methodIds
    	type
    }
  }
}


Variables:
{
  "input": {
    "shopId": "shopId-GOES-HERE",
    "surchargeId": "surchargeId-GOES-HERE"
    "surcharge": {
      "amount": 888.99,
       "messagesByLanguage": [
        {
        	"content": "This is an updated message in english",
        	"language": "en"
        },{ 
          "content": "Este es un nuevo mensaje en espanol",
          "language": "es"
        }
      ]
      "methodIds": ["a","b"],
      "type": "surcharge",
      "attributes": [
        { "property": "productVendor", "value": "reaction", "propertyType": "string", "operator": "eq" },
        { "property": "variantTitle", "value": "Option 1 - Red Dwarf", "propertyType": "string", "operator": "eq" }
      ],
      "destination": { "region": ["NJ", "TX"] }
    }
  }
}

Delete a surcharge

mutation deleteSurcharge($input: DeleteSurchargeInput!) {
  deleteSurcharge(input: $input) {
    clientMutationId
    surcharge {
      _id
      amount {
        amount
        displayAmount
      }
      message
      methodIds
      type
    }
  }
}


Variables:
{
  "input": {
    "shopId": "shopId-GOES-HERE",
    "surchargeId": "surchargeId-GOES-HERE"
  }
}

Applying surcharges to checkout

  1. Use the above instructions to create a few surcharges that will apply to different scenarios. For example:
    • Create one that will apply with destination: { region: ["CA"] }, and then use a California address for your shipping address.
    • Create one that will apply with attributes: [ { "property": "productVendor", "value": "Surcharge", "propertyType": "string", "operator": "eq" },], and then add a product to your cart that has a vendor of "Surcharge"
  1. Add items to cart, and select a shipping method during checkout
  2. See that appropriate surcharges are applied, and display in step 4 (Review) in checkout
  3. Make sure you can complete checkout with surcharges applied

Note that a surcharge must match ALL attributes AND destinations for it to apply. If you want to test just a destination, leave the attributes array empty. If you want to test just an attribute, leave the destination object empty. If you want to test both, then provide both, but note that they must both match (i.e. the product must have title value as Basic Reaction Product AND it must be shipped to CA)

@impactmass impactmass changed the base branch from release-2.0.0-rc.7 to release-2.0.0-rc.8 November 30, 2018 16:12
@aldeed aldeed changed the base branch from release-2.0.0-rc.8 to develop December 14, 2018 20:06
Signed-off-by: Erik Kieckhafer <[email protected]>
Signed-off-by: Erik Kieckhafer <[email protected]>
@kieckhafer kieckhafer dismissed aldeed’s stale review February 8, 2019 18:58

@aldeed all comments addressed. The only one I did not take action on was the indexing, I'm not sure it's needed. Maybe you see a place it is? Also, I added a new field to the Order schema, as a recent change was causing an issue.

@aldeed
Copy link
Contributor

aldeed commented Feb 13, 2019

@kieckhafer That change looks good, but a few more things I see now (so close!):

  • I forgot that we had long ago decided that we should not prefix GraphQL queries with get. So getSurcharges should be surcharges and getSurcharge should be surchargeById. The restriction queries actually have the same issue, but I’m not sure how we feel about changing them now. Have they been included in a release yet?
  • The default sort is by createdAt, but I noticed that neither createdAt nor updatedAt are being set. Add createdAt: new Date() to the insert and updatedAt: new Date() to the $set in the update.
  • I’m seeing that methodIds is not being respected. I created a surcharge for a single method ID, but it shows up in storefront checkout totals regardless of which fulfillment method I choose.

@kieckhafer
Copy link
Member Author

kieckhafer commented Feb 13, 2019

@aldeed

  • I forgot that we had long ago decided that we should not prefix GraphQL queries with get. So getSurcharges should be surcharges and getSurcharge should be surchargeById. The restriction queries actually have the same issue, but I’m not sure how we feel about changing them now. Have they been included in a release yet?

Updated. Restrictions were merged in ~November, they are in a release.

  • The default sort is by createdAt, but I noticed that neither createdAt nor updatedAt are being set. Add createdAt: new Date() to the insert and updatedAt: new Date() to the $set in the update.

Added

  • I’m seeing that methodIds is not being respected. I created a surcharge for a single method ID, but it shows up in storefront checkout totals regardless of which fulfillment method I choose.

Fixed: 72abacf

@aldeed aldeed merged commit aca7393 into develop Feb 13, 2019
@aldeed aldeed deleted the feat-kieckhafer-surcharges branch February 13, 2019 22:27
@jeffcorpuz jeffcorpuz mentioned this pull request Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants