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

Enrich our OG tags with some extra info to help with Synerise integration #43

Merged
merged 7 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- `product:retailer_part_no` and `product:category` to the ProductOpenGraph.

## [1.3.0] - 2024-02-09

### Added
Expand Down
31 changes: 26 additions & 5 deletions react/ProductOpenGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
RenderContext,
canUseDOM,
} from 'vtex.render-runtime'
import { ProductContext, SKU } from 'vtex.product-context'
import { ProductContext, SKU, Product } from 'vtex.product-context'

import useAppSettings from './hooks/useAppSettings'

Expand All @@ -21,6 +21,7 @@ interface MetaTag {
}

function ProductOpenGraph() {
const { disableOffers } = useAppSettings()
iago1501 marked this conversation as resolved.
Show resolved Hide resolved
const productContext = useContext(ProductContext) as ProductContext
const runtime = useRuntime() as RenderContext
const hasValue = productContext?.product
Expand Down Expand Up @@ -67,13 +68,17 @@ function ProductOpenGraph() {
selectedItem
? { property: 'product:sku', content: selectedItem.itemId }
: null,
selectedItem
? { property: 'product:retailer_part_no', content: product.productId } // Used in the Synerise integration
: null,
{ property: 'product:retailer_item_id', content: product.productReference },
{ property: 'product:condition', content: 'new' },
{ property: 'product:brand', content: product.brand },
{ property: 'product:retailer_item_id', content: product.productReference },
{ property: 'product:price:currency', content: `${currency}` },
...productCategories(product),
...productImage(selectedItem),
productPrice({ selectedItem, disableOffers }),
productAvailability(selectedItem),
productPrice(selectedItem),
].filter(Boolean) as MetaTag[]

return (
Expand Down Expand Up @@ -111,8 +116,13 @@ function productAvailability(selectedItem?: SKU): MetaTag {
return { property: 'product:availability', content: `${availability}` }
}

function productPrice(selectedItem?: SKU): MetaTag | null {
const { disableOffers } = useAppSettings()
function productPrice({
selectedItem,
disableOffers,
}: {
selectedItem?: SKU
disableOffers: boolean
}): MetaTag | null {
const seller = selectedItem?.sellers.find(
({ commertialOffer }) => commertialOffer.AvailableQuantity > 0
)
Expand All @@ -131,4 +141,15 @@ function productPrice(selectedItem?: SKU): MetaTag | null {
}
}

function productCategories(product?: Product) {
if (!product?.categories?.length) {
return []
}

return product.categories.map(category => ({
property: 'product:category',
content: category,
}))
}

export default ProductOpenGraph
Loading