Add additional fee to order in checkout ui extension

I am trying to achieve this:

  1. If the user enters a specific region in the shipment address then show an alert. I managed to do this successfully using the zip code property in useShipmentAddress.
  2. One more thing I want to do is to add an additional fee in the order summary if the user is from a specific region. How can I do this? Can I use the graphql admin API to update the order or what is the best way to achieve this?

Honetly can’t remember why, but I ended up creating a $0 product that was automatically added the to checkout based off some custom rules – in your case the region – via Checkout UI extensions, and then a separate Function that calculates the free price and then uses CartTransform to set the $0-product price.

If the fee is static you obviously wont need the Function. My case just had a dynamic fee.

Not sure if you can use this method.

hey @curzey thanks for taking the time to reply. Yes, it is a valid approach and I also considered it first but the client specifically requires that the fee should not show as an extra item but as an additional fee or an extra shipment fee with the title “Shipment Surcharge”. I was looking into the graphql API and there is an additionalFee property on order I was just wondering whether you can update it by updating the order or not

I’m not sure if that is possible. But will follow to see if someone has the solution.

were you able to find a solution for this @saimarshadd ?

I’m not sure you’ll be allowed to do this with the new rules but I’ll link them for you to see.

Hey @curzey Can we hide this product on cart page and display it only on checkout??

If I remember correct I added/removed the “free product” based off if a payment method was chosen. So it was actually removed when leaving the checkout.

Otherwise you could always just not render the line item in the cart. Skip it with liquid or hide it with CSS.

Thanks for replying @curzey . Is there any tutorial for beginners to implement this? i want the price to be 0.3% of the total cart amount. I created the app and added the extension but it just doesn’t show on checkout.

I’m not sure.

Can you explain further how far you are? Maybe show some code.

run.js

js

CopyEdit

// @ts-check

/**
 * @typedef {import("../generated/api").RunInput} RunInput
 * @typedef {import("../generated/api").FunctionRunResult} FunctionRunResult
 */

const NO_CHANGES = { operations: [] };

const ADMIN_FEE_VARIANT_GID = "gid://shopify/ProductVariant/42158587150401"; // Replace with your actual variant GID

/**
 * @param {RunInput} input
 * @returns {FunctionRunResult}
 */
export function run(input) {
  const lines = input.cart.lines;

  const alreadyAdded = lines.some(
    (line) => line.merchandise.id === ADMIN_FEE_VARIANT_GID
  );
  if (alreadyAdded) return NO_CHANGES;

  let subtotal = 0;
  for (const line of lines) {
    const item = line.merchandise;
    if (item.__typename === "ProductVariant") {
      const price = parseFloat(item.price.amount);
      subtotal += price * line.quantity;
    }
  }

  const feeAmount = subtotal * 0.003;
  const quantity = Math.max(1, Math.ceil(feeAmount));

  return {
    operations: [
      {
        addCartLine: {
          merchandiseId: ADMIN_FEE_VARIANT_GID,
          quantity,
        },
      },
    ],
  };
}

run.graphql

graphql

CopyEdit

query RunInput {
  cart {
    lines {
      quantity
      merchandise {
        ... on ProductVariant {
          id
          price {
            amount
          }
        }
      }
    }
  }
}

:white_check_mark: Extension config (shopify.extension.toml)

toml

CopyEdit

api_version = "2025-01"

[[extensions]]
name = "Admin Fee Cart Transform"
handle = "admin-fee-transform"
type = "cart_transform"
description = "Adds a 0.3% admin fee"

[[extensions.targeting]]
target = "purchase.cart-transform.run"
input_query = "src/run.graphql"
export = "run"

[extensions.build]
command = ""
path = "dist/function.wasm"

:hammer_and_wrench: Tools & Stack

  • Shopify CLI v3.79
  • JavaScript
  • Cart Transform function (Shopify Function)
  • Created hidden product for the fee and published to Online Store

:cross_mark: What’s not working:

  • The extension compiles and deploys successfully.
  • It shows up under Extensions in Shopify Partners dashboard.
  • But the app doesn’t appear in the Checkout Customizer and the fee doesn’t show in checkout.
  • shopify app dev preview shows no runs.```

Hi Christon,

Do you actually add the product to the cart? You can’t run a cartTransform changing the price of a line_item thats not in your cart.

Other than that, your functions seems to look right.

I think it’s more correct to use CartLineCost rather than the original variant price.

query RunInput {
  cart {
    lines {
      id
      cost {
        totalAmount {
          amount
          currencyCode
        }
      }
    }
  }
}

Also, you mention that it does not show up in the customizer. That is intended, as this is a Function, not a UI Extension.

It will show up at https://admin.shopify.com/store/STORE_NAME/settings/checkout when the Function is created. Read more about that here: Add a customized bundle function