Removing shipping services based on products in cart #35
-
Hi All, I would just like to share how to remove an available shipping service based on the products in the cart. There is no native feature in the Swell dashboard settings, so it has to be done in a webhook. It's not documented in Swell, so I'm posting here to save future users some time since it took me half a day to figure this out. In my specific example, we have two flat rate shipping options - Here's the setup:
The webhook
import type { RequestHandler } from './$types'
import swell from 'swell-node'
import type { ShippingWebhookPayload } from '$lib/types/ShippingWebhookPayload'
import { SWELL_STORE_ID, SWELL_SECRET_KEY } from '$env/static/private'
import { json } from '@sveltejs/kit'
/**
* Real time order webhook handles getting and returning shipping options based on products
* in cart at checkout.
*/
swell.init(SWELL_STORE_ID, SWELL_SECRET_KEY)
export const POST: RequestHandler = async ({ request }) => {
const payload: ShippingWebhookPayload = await request.json()
// check for bottle products - which are bulky items we don't want to offer rush shipping on.
const { shipment_rating, items } = payload.data
const includesBottles = items.some((el) => el.product_name.includes("Bottle"))
// return empty response with 200 status if no bottles found (the default shipping services will be available)
if (!includesBottles) {
return json({ message: "nothing to do"});
}
// filter out rush shipping
const filteredServices = shipment_rating.services.filter(
(service) => !service.name.includes('Rush')
)
return json({
$set: {
shipment_rating: {
services: filteredServices
}
}
})
} Bonus, here's the typings for the webhook payload data from swell: /**
* Types for the real time shipping webhook from swell.
*/
export interface ShippingWebhookPayload {
type: string;
model: string;
data: Data;
}
export interface Data {
draft: boolean;
billing: Billing;
shipping: Shipping;
currency: string;
checkout_id: string;
active: boolean;
sub_total: number;
item_discount: number;
item_tax: number;
item_quantity: number;
item_shipment_weight: number;
discount_total: number;
shipment_price: number;
shipment_total: number;
tax_total: number;
tax_included_total: number;
grand_total: number;
auth_total: number;
giftcard_total: number;
capture_total: number;
guest: boolean;
date_created: string;
status: string;
number: string;
date_updated: string;
items: Item[];
shipment_delivery: boolean;
taxes: Tax2[];
account_id: string;
account_info_saved: boolean;
account_logged_in: any;
shipment_tax: number;
date_webhook_last_succeeded: string;
shipment_rating: ShipmentRating;
webhook_status: number;
id: string;
}
export interface Billing {
name: string;
first_name: string;
last_name: string;
address1: string;
address2: any;
city: string;
state: string;
zip: string;
country: string;
phone: string;
odoo_id: number;
}
export interface Shipping {
account_address_id: string;
name: string;
first_name: string;
last_name: string;
address1: string;
address2: any;
city: string;
state: string;
zip: string;
country: string;
phone: string;
company: string;
odoo_id: number;
price: any;
service: any;
service_name: any;
}
export interface Item {
product_id: string;
options: Option[];
quantity: number;
price: number;
purchase_option: PurchaseOption;
id: string;
variant_id: string;
orig_price: number;
delivery: string;
shipment_weight: number;
product_name: string;
price_total: number;
discount_total: number;
discount_each: number;
tax_total: number;
tax_each: number;
taxes: Tax[];
}
export interface Option {
id: string;
name: string;
value: string;
value_id: string;
variant: boolean;
}
export interface PurchaseOption {
type: string;
price: number;
}
export interface Tax {
id: string;
amount: number;
}
export interface Tax2 {
id: string;
name: string;
shipping: boolean;
amount: number;
}
export interface ShipmentRating {
date_created: string;
fingerprint: string;
services: Service[];
}
export interface Service {
id: string;
name: string;
price: number;
description: string;
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@vettloffah Thanks for the detailed guide. I will ensure that our team will work on improving the documentation along these lines. |
Beta Was this translation helpful? Give feedback.
-
This is great, appreciate the guide! Would love to share it more widely |
Beta Was this translation helpful? Give feedback.
This is great, appreciate the guide! Would love to share it more widely