Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fpbrault committed Nov 21, 2024
1 parent a88d877 commit 087509d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export default function InstantProducts() {
<button onClick={() => clickProduct(product)}>
{product.ec_name} ({product.ec_product_id})
</button>
<button onClick={() => addToCart(cartMethods!, cartState, product)}>
<button
onClick={() => addToCart(cartMethods!, cartState, product, methods)}
>
Add to cart
</button>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export default function ProductList() {
/>
</button>
<button
onClick={() =>
addToCart(cartMethods!, cartState, product, state.responseId)
}
onClick={() => addToCart(cartMethods!, cartState, product, methods)}
>
Add to cart
</button>
Expand Down
43 changes: 10 additions & 33 deletions packages/samples/headless-ssr-commerce/utils/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import {
CartItem as HeadlessCartItem,
Product as HeadlessProduct,
CartState as HeadlessCartState,
getSampleCommerceEngineConfiguration,
InstantProducts,
ProductList,
} from '@coveo/headless-react/ssr-commerce';
import {createRelay} from '@coveo/relay';
import {defaultContext} from './context';

type HeadlessSSRCart = Omit<HeadlessCart, 'state' | 'subscribe'>;

Expand All @@ -26,40 +25,18 @@ export async function adjustQuantity(
await externalCartAPI.updateItemQuantity(updatedItem);
}

// When sending cart events directly from the search result page, product listing pages, or from recommendation slots, you must send an additional click event along with the cart event.
//
// See https://docs.coveo.com/en/o1n93466/coveo-for-commerce/capture-cart-events#send-an-additional-click-event
//
// This is an example of how to send the product click event.
function sendProductClickEvent(product: HeadlessProduct, responseId: string) {
const config = getSampleCommerceEngineConfiguration();
const relay = createRelay({
token: config.accessToken,
trackingId: config.analytics.trackingId ?? '',
url: `https://${config.organizationId}.analytics.org.coveo.com/rest/organizations/${config.organizationId}/events/v1`,
});

relay.emit('ec.productClick', {
position: product.position,
currency: defaultContext.currency,
product: {
productId: product.ec_product_id,
name: product.ec_name,
price: product.ec_price,
},
responseId: responseId,
});
}

export async function addToCart(
headlessCart: HeadlessSSRCart,
headlessCartState: HeadlessCartState,
product: HeadlessProduct,
responseId?: string
methods:
| Omit<InstantProducts | ProductList, 'state' | 'subscribe'>
| undefined
) {
const existingItem = headlessCartState.items.find(
(item) => item.productId === product.ec_product_id
);

const quantity = existingItem ? existingItem.quantity + 1 : 1;
const item = {
name: product.ec_name!,
Expand All @@ -72,10 +49,10 @@ export async function addToCart(
// Add the item to the external service
await externalCartAPI.addItemToCart(item);

if (responseId) {
// Send the product click event
sendProductClickEvent(product, responseId);
}
// When sending cart events directly from the search result page, product listing pages, or from recommendation slots, you must send an additional click event along with the cart event.
//
// See https://docs.coveo.com/en/o1n93466/coveo-for-commerce/capture-cart-events#send-an-additional-click-event
methods?.interactiveProduct({options: {product}}).select();
}

export async function purchase(
Expand Down

0 comments on commit 087509d

Please sign in to comment.